Skip to content

Commit 2cfdf4c

Browse files
committed
twitter_bot: Solve retweet issue.
The twitter bot initially posted all kind of tweets. Change it so that on encountering a retweet either post the original tweet if it hasn't been posted before. Else try to edit the number of retweets in the message of the original tweet.
1 parent 8fc5b77 commit 2cfdf4c

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

zulip/integrations/twitter/twitter-bot

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ for status in statuses[::-1][:opts.limit_tweets]:
225225
# https://twitter.com/eatevilpenguins/status/309995853408530432
226226
composed = "%s (%s)" % (status.user.name, status.user.screen_name)
227227
url = "https://twitter.com/%s/status/%s" % (status.user.screen_name, status.id)
228-
# This contains all strings that could have caused the tweet to match our query.
229228
text_to_check = [status.text, status.user.screen_name]
230229
text_to_check.extend(url.expanded_url for url in status.urls)
231230

@@ -259,14 +258,47 @@ for status in statuses[::-1][:opts.limit_tweets]:
259258
elif opts.twitter_name:
260259
subject = composed
261260

262-
message = {
263-
"type": "stream",
264-
"to": [opts.stream],
265-
"subject": subject,
266-
"content": url
267-
}
268-
269-
ret = client.send_message(message)
261+
# If the tweet is a retweet then check if the original tweet has been sent before
262+
# and try to edit it, else post new message with the original tweet.
263+
# This contains all strings that could have caused the tweet to match our query.
264+
message_id = 0
265+
edit_message = False
266+
if status.retweeted_status is not None:
267+
try:
268+
message_id = config_internal.getint('twitter', str(status.retweeted_status.id))
269+
edit_message = True
270+
except (NoOptionError, NoSectionError):
271+
edit_message = False
272+
url = (status.retweeted_status.urls[0].expanded_url + "\n") + ("Retweets: %s" % (status.retweeted_status.retweet_count,))
273+
else:
274+
url = url + "\nRetweets: " + str(status.retweet_count)
275+
# If we are editing message it implies the current tweet is a retweet
276+
# and has been posted before. Hence try to edit the older tweet and
277+
# change its number of retweets but if it has passed its edit time
278+
# do nothing.
279+
if edit_message is True:
280+
message_data = {
281+
"message_id": message_id,
282+
"content": url,
283+
}
284+
ret = client.update_message(message_data)
285+
286+
if edit_message is False:
287+
message = {
288+
"type": "stream",
289+
"to": [opts.stream],
290+
"subject": subject,
291+
"content": url
292+
}
293+
294+
ret = client.send_message(message)
295+
if ret['result'] == 'success':
296+
if 'twitter' not in config_internal.sections():
297+
config_internal.add_section('twitter')
298+
if status.retweeted_status is None:
299+
config_internal.set('twitter', str(status.id), str(ret['id']))
300+
else:
301+
config_internal.set('twitter', str(status.retweeted_status.id), str(ret['id']))
270302

271303
if ret['result'] == 'error':
272304
# If sending failed (e.g. no such stream), abort and retry next time

0 commit comments

Comments
 (0)