first time here and just started investigating Twitter bots to retweet traffic info to my followers. Am using Amit Agarwal's labnol bot on Google Script and have changed very little (removed favoriting tweets) but I cannot get it to retweet anything using my search parameters despite there being matching tweets on Twitter, could someone check what I'm doing wrong, I don't know enough (yet) to spot the problem? Thanks in advance.
// Written by Amit Agarwal @labnol on 31/07/2015
// Fill the Twitter Keys and then choose Run -> Start Bot
TWITTER_CONSUMER_KEY = "removed";
TWITTER_CONSUMER_SECRET = "removed";
TWITTER_ACCESS_TOKEN = "removed";
TWITTER_ACCESS_SECRET = "removed";
TWITTER_SEARCH_PHRASE = 'A13 list:a13essex/traffic';
function Start_Bot() {
var props = PropertiesService.getScriptProperties();
props.setProperties({
TWITTER_CONSUMER_KEY: TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET: TWITTER_CONSUMER_SECRET,
TWITTER_ACCESS_TOKEN: TWITTER_ACCESS_TOKEN,
TWITTER_ACCESS_SECRET: TWITTER_ACCESS_SECRET,
SINCE_TWITTER_ID: 0
});
var twit = new Twitter.OAuth(props);
// Test Twitter authorization
if (!twit.favorite("628053456071192576")) {
throw new Error("Please check your Twitter access tokens");
return;
}
ScriptApp.newTrigger("labnol_twitterBot")
.timeBased()
.everyMinutes(10)
.create();
}
function labnol_twitterBot() {
try {
var props = PropertiesService.getScriptProperties(),
twit = new Twitter.OAuth(props);
if (twit.hasAccess()) {
var tweets = twit.fetchTweets(
TWITTER_SEARCH_PHRASE, function(tweet) {
// Skip tweets that contain sensitive content
if (!tweet.possibly_sensitive) {
return tweet.id_str;
}
}, {
multi: true,
lang: "en", // Process only English tweets
count: 5, // Process 5 tweets in a batch
since_id: props.getProperty("SINCE_TWITTER_ID")
});
if (tweets) {
props.setProperty("SINCE_TWITTER_ID", tweets[0]);
for (var i = tweets.length - 1; i >= 0; i--) {
twit.retweet(tweets[i]);
/* Wait between 10 seconds and 1 minute */
Utilities.sleep(Math.floor(Math.random()*50000) + 10000);
}
}
}
} catch (f) {
Logger.log("Error: " + f.toString());
}
}
// Email: amit@labnol.org
// Premium Support: http://ctrlq.org
This returns the following result in the log:
[16-09-30 11:25:32:289 BST] https://api.twitter.com/1.1/search/tweets.json?lang=en&count=5&since_id=undefined&include_entities=false&result_type=recent&q=A13%20list%3Aa13essex%252Ftraffic
[16-09-30 11:25:32:292 BST] lang=en
[16-09-30 11:25:32:293 BST] count=5
[16-09-30 11:25:32:293 BST] since_id=undefined
[16-09-30 11:25:32:294 BST] include_entities=false
[16-09-30 11:25:32:294 BST] result_type=recent
[16-09-30 11:25:32:295 BST] q=A13 list:a13essex%2Ftraffic
[16-09-30 11:25:32:375 BST] No matching tweets this go-round
Is https://api.twitter.com/1.1/search/tweets.json?lang=en&count=5&since_id=undefined&include_entities=false&result_type=recent&q=A13%20list%3Aa13essex%252Ftraffic correct? Thanks again, Paul
You've identified another special character encoding issue with fetchTweets. I did some API checks and the encoding of slash should be %2F on the URL to return the proper result set.
I've filed an issue on Github for the next release: https://github.com/airhadoken/twitter-lib/issues/16
In the meantime you can use twit.fetch("https://api.twitter.com/1.1/search/tweets.json?lang=en&count=5&since_id=undefined&include_entities=false&result_type=recent&q=A13%20list%3Aa13essex%2Ftraffic")
as a workaround; you will have to JSON.parse
the content text to get the tweets as objects. I'll report back when you can use fetchTweets()
again (i.e. when Twitter lib v23 is ready to release).
Thanks for letting me know! -- Bradley Momberger, creator/maintainer of Twitter lib.