Telegram version of schneiderbot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.0 KiB

6 years ago
  1. import twitter
  2. from telegram import Update
  3. from telegram.ext import CallbackContext
  4. pepito_id = 333923305
  5. last_status = None
  6. pepito_groups = []
  7. def get_twitter_instance():
  8. twitter_config = {}
  9. with open("config/twitter_config") as tmp_file:
  10. for line in tmp_file:
  11. (key, val) = line.split('=')
  12. twitter_config[key] = val.replace('\n', '')
  13. return twitter.Api(consumer_key=twitter_config['consumer_key'],
  14. consumer_secret=twitter_config['consumer_secret'],
  15. access_token_key=twitter_config['access_token_key'],
  16. access_token_secret=twitter_config['access_token_secret'])
  17. def toggle_pepito_for_group(update: Update, context: CallbackContext):
  18. global pepito_groups
  19. chat_id = update.message.chat_id
  20. if chat_id in pepito_groups:
  21. pepito_groups.remove(chat_id)
  22. update.message.reply_text("Pepito is now disabled!")
  23. else:
  24. pepito_groups.append(update.message.chat_id)
  25. update.message.reply_text("Pepito is now enabled!")
  26. def check_pepito_and_post_auto(context: CallbackContext):
  27. api = get_twitter_instance()
  28. current_status = api.GetUserTimeline(pepito_id)[0]
  29. global last_status
  30. if last_status is None or last_status.AsDict()['id'] != current_status.AsDict()['id']:
  31. for group_id in pepito_groups:
  32. context.bot.send_message.send_message(chat_id=group_id, text=current_status.AsDict()['text'])
  33. last_status = current_status
  34. def check_pepito_and_post_manually(update: Update, context: CallbackContext):
  35. api = get_twitter_instance()
  36. current_status = api.GetUserTimeline(pepito_id)[0]
  37. global last_status
  38. if last_status.AsDict()['id'] != current_status.AsDict()['id']:
  39. for group_id in pepito_groups:
  40. context.bot.send_message(chat_id=group_id, text=current_status.AsDict()['text'])
  41. last_status = current_status
  42. else:
  43. update.message.reply_text(current_status.AsDict()['text'])