Action

Import Twitter Search

Posted by agiletortoise, Last update 10 months ago

NOTE: This action no longer functions due to policy changes at Twitter that are out of our control. Visit the Twitter integration guide for details.

Prompt for a Twitter search query. Can be for any text string and supports the same syntax used in Twitter web search.

Use Twitter API to get a list of tweets matching the search and import them into a new draft as a Markdown list, with screen name and link to the tweet included.

Posted as an example of Twitter API use in Drafts.

Steps

  • script

    // set identifier for Twitter credentials you wish to use
    const twitterIdentifier = "";
    let q = "";
    
    let p = Prompt.create();
    p.title = "Twitter Search";
    p.message = "Entry search text below. Results will be imported into new draft in Markdown list format.";
    
    p.addTextField("q", "Query", q);
    p.addButton("Search");
    
    if (!p.show()) {
    	context.cancel();
    }
    else {
    	q = p.fieldValues["q"];
    }
    
    
  • script

    // a few base values
    const apiURL = "https://api.twitter.com/1.1/search/tweets.json";
    
    let f = () => {
    	let twitter = Twitter.create(twitterIdentifier);
    	let content = [`### Twitter Search: ${q}`,""];
    	
    	let params = {
    		"q": q
    	}
    	let response = twitter.request({
    		"url": apiURL,
    		"method": "GET",
    		"parameters": params
    	});
    	if (response.statusCode == 200 || response.statusCode == 201) {
    		let tweets = response.responseData.statuses;
    		if (!tweets || tweets.length == 0) {
    			alert("No tweets found.");
    			context.cancel();
    			return;
    		}
    		for (let tweet of tweets) {
    			let id = tweet.id_str;
    			let sn = tweet.user.screen_name;
    			let tweetURL = `http://twitter.com/${sn}/status/${id}`;
    			let text = tweet.text; 
    			content.push(`- **@${sn}**: ${text} [link](${tweetURL})`);
    		}
    		let d = Draft.create();
    		d.content = content.join("\n");
    		d.update();
    		editor.load(d);
    		console.log("Twitter search imported");
    	}
    	else {
    		console.log("Error: " + response.error);
    		context.fail();
    	}
    }
    f();

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.