This is how you can write a Twitter bot with the help of Google Apps Script. Internally, it uses Wolfraph Alpha to get the answers.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
function start() { // REPLACE THESE DUMMY VALUES var TWITTER_CONSUMER_KEY = "AAAA"; var TWITTER_CONSUMER_SECRET = "BBBB"; var TWITTER_HANDLE = "CCCC"; var WOLFRAM_API_ID = "DDDD"; // Store user variables ScriptProperties.setProperty("CONSUMER_KEY", TWITTER_CONSUMER_KEY); ScriptProperties.setProperty("CONSUMER_SECRET", TWITTER_CONSUMER_SECRET); ScriptProperties.setProperty("TWITTER_HANDLE", TWITTER_HANDLE); ScriptProperties.setProperty("WOLFRAM_API_ID", WOLFRAM_API_ID); // The MAX_TWITTER_ID will store the ID of the last tweet read by the bot ScriptProperties.setProperty("MAX_TWITTER_ID", 0); // Delete exiting triggers, if any var triggers = ScriptApp.getScriptTriggers(); for(var i=0; i < triggers.length; i++) { ScriptApp.deleteTrigger(triggers[i]); } // Setup a time-based trigger for the Bot to fetch and process incoming Tweets // every minute. If your Google Script is running out of quota, change the // time to 5 or 10 minutes though the bot won't offer real-time answers then. ScriptApp.newTrigger("fetchTweets") .timeBased() .everyMinutes(1) .create(); } // We are using oAuth with Twitter API 1.1 to connect to Twitter function oAuth() { var oauthConfig = UrlFetchApp.addOAuthService("twitter"); oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token"); oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token"); oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authorize"); oauthConfig.setConsumerKey(ScriptProperties.getProperty("CONSUMER_KEY")); oauthConfig.setConsumerSecret(ScriptProperties.getProperty("CONSUMER_SECRET")); } // This is the bot that reads the tweets function fetchTweets() { oAuth(); var twitter_handle = ScriptProperties.getProperty("TWITTER_HANDLE"); var phrase = "lang:en+to:" + twitter_handle; var search = "https://api.twitter.com/1.1/search/tweets.json?q="; search = search + encodeString(phrase) + "&since_id=" + ScriptProperties.getProperty("MAX_TWITTER_ID"); // Make sure the URLs are RFC 3986 Compliant else OAuth may throw service // exceptions as Twitter will deny the oAuth request and you'll have to // authorize with Twitter every time your run the script. var options = { "method": "get", "oAuthServiceName":"twitter", "oAuthUseToken":"always" }; try { var result = UrlFetchApp.fetch(search, options); if (result.getResponseCode() === 200) { var data = Utilities.jsonParse(result.getContentText()); if (data) { var tweets = data.statuses; for (var i=tweets.length-1; i>=0; i--) { var question = tweets[i].text.replace ( new RegExp("\@" + twitter_handle, "ig"), ""); var answer = askWolframAlpha(question); sendTweet(tweets[i].user.screen_name, tweets[i].id_str, answer); } } } } catch (e) { // Log oAuth errors if any. // You can also use GmailApp to get email notifications of errors. Logger.log(e.toString()); } } function sendTweet(user, reply_id, tweet) { // Remember that we issue status updates with a POST method var options = { "method": "POST", "oAuthServiceName":"twitter", "oAuthUseToken":"always" }; var status = "https://api.twitter.com/1.1/statuses/update.json"; status = status + "?status=" + encodeString("@" + user + " " + tweet); status = status + "&in_reply_to_status_id=" + reply_id; try { var result = UrlFetchApp.fetch(status, options); ScriptProperties.setProperty("MAX_TWITTER_ID", reply_id); } catch (e) { Logger.log(e.toString()); } } // encodeURIComponent isn't enough for oAuth as it does encode certain // characters like !, *, (), etc. Thank you +Martin Hawksey - you are awesome function encodeString (q) { var str = encodeURIComponent(q); str = str.replace(/!/g,'%21'); str = str.replace(/\*/g,'%2A'); str = str.replace(/\(/g,'%28'); str = str.replace(/\)/g,'%29'); str = str.replace(/'/g,'%27'); return str; } function askWolframAlpha(q) { // We are using v2 of the Wolfram Alpha API var request = "http://api.wolframalpha.com/v2/query?" + "podindex=2&format=plaintext&appid=" + ScriptProperties.getProperty("WOLFRAM_API_ID") + "&input=" + encodeString(q); // Wolfram API returns results in an XML format // and these are easy to parse with Google Scripts var answer = Xml.parse(UrlFetchApp.fetch(request).getContentText(), true); if (answer.queryresult.success == "true") return answer.queryresult.pod.subpod.plaintext.Text; else return "Sorry but I do not have an answer to that question."; } |