Skip to content

Commit 2f0e554

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 2f0e554

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

zulip/integrations/twitter/twitter-bot

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,17 @@ 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+
# If the tweet is a retweet then check if the original tweet has been sent before
229+
# and try to edit it, else post new message with the original tweet.
230+
message_id = 0
231+
edit_message = False
232+
if status.retweeted_status is not None:
233+
try:
234+
message_id = config_internal.getint('twitter', str(status.retweeted_status.id))
235+
edit_message = True
236+
except (NoOptionError, NoSectionError):
237+
edit_message = False
238+
url = (status.retweeted_status.urls[0].expanded_url + "\n") + ("Retweets: %s" % (status.retweeted_status.retweet_count,))
228239
# This contains all strings that could have caused the tweet to match our query.
229240
text_to_check = [status.text, status.user.screen_name]
230241
text_to_check.extend(url.expanded_url for url in status.urls)
@@ -258,15 +269,33 @@ for status in statuses[::-1][:opts.limit_tweets]:
258269
subject = search_term_used
259270
elif opts.twitter_name:
260271
subject = composed
261-
262-
message = {
263-
"type": "stream",
264-
"to": [opts.stream],
265-
"subject": subject,
266-
"content": url
267-
}
268-
269-
ret = client.send_message(message)
272+
# If we are editing message it implies the current tweet is a retweet
273+
# and has been posted before. Hence try to edit the older tweet and
274+
# change its number of retweets but if it has passed its edit time
275+
# do nothing.
276+
if edit_message is True:
277+
message_data = {
278+
"message_id": message_id,
279+
"content": url,
280+
}
281+
ret = client.update_message(message_data)
282+
283+
if edit_message is False:
284+
message = {
285+
"type": "stream",
286+
"to": [opts.stream],
287+
"subject": subject,
288+
"content": url
289+
}
290+
291+
ret = client.send_message(message)
292+
if ret['result'] == 'success':
293+
if 'twitter' not in config_internal.sections():
294+
config_internal.add_section('twitter')
295+
if status.retweeted_status is None:
296+
config_internal.set('twitter', str(status.id), str(ret['id']))
297+
else:
298+
config_internal.set('twitter', str(status.retweeted_status.id), str(ret['id']))
270299

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

0 commit comments

Comments
 (0)