Advertisement
emesten

Untitled

Aug 31st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. from telegram.ext import Updater
  2. from telegram.ext import CommandHandler
  3. from telegram.ext import MessageHandler
  4. from telegram.ext import BaseFilter
  5. from comms import Communicate
  6. import logging
  7. import json
  8.  
  9.  
  10. class SetupFilter(BaseFilter):
  11. """ Custom filter checking for command initializer """
  12. def filter(self, message):
  13. try:
  14. return config['INITCOMMAND'] == message.text.split()[0]
  15. except AttributeError:
  16. return False
  17.  
  18.  
  19. with open('config.json') as config_file:
  20. try:
  21. config = json.load(config_file)
  22. except (KeyError, ValueError):
  23. raise
  24. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
  25.  
  26.  
  27. def start(bot, update):
  28. """ Initializing bot """
  29. bot.send_message(chat_id=update.message.chat_id, text=config['MESSAGE']['START'])
  30.  
  31.  
  32. def get_message_by_type(msg_type):
  33. """ Get message from config based on given type """
  34. types = config['MESSAGE']
  35. if msg_type == 1:
  36. return types['START']
  37. elif msg_type == 2:
  38. return types['ERROR']
  39. elif msg_type == 3:
  40. return types['NOTIFYSET']
  41. else:
  42. return types['ITEMSOLD']
  43.  
  44.  
  45. def exception_message(bot, chat_id, message_type):
  46. """ Error message handler """
  47. message = get_message_by_type(message_type)
  48. return bot.send_message(chat_id=chat_id, text=message)
  49.  
  50.  
  51. def set_reminder(bot, update):
  52. """ Calculate and find any bad data that might have been typed in """
  53. args = update.message.text.split()
  54. chat_id = update.message.chat_id
  55.  
  56. if len(args) != 1 and len(args) != 2:
  57. return exception_message(bot, chat_id, Communicate.ERROR)
  58.  
  59. if len(args) == 1:
  60. return sched_message(bot, update, config['SENDIN'])
  61.  
  62. msg_sent_at = update.message['date'].minute
  63. try:
  64. init_minute = int(args[1])
  65. except ValueError:
  66. return exception_message(bot, chat_id, Communicate.ERROR)
  67. if init_minute < 0 or init_minute > 59:
  68. return exception_message(bot, chat_id, Communicate.ERROR)
  69.  
  70. if msg_sent_at < init_minute:
  71. msg_sent_at += 60
  72. if msg_sent_at - init_minute >= 15:
  73. return exception_message(bot, chat_id, Communicate.SOLD)
  74.  
  75. notify_minute = msg_sent_at - init_minute - config['SENDIN']
  76. return sched_message(bot, update, abs(notify_minute))
  77.  
  78.  
  79. def notify_subscriber(bot, job):
  80. """ Send message to a subscriber """
  81. return bot.send_message(chat_id=job.context['chat_id'], text=job.context['message'])
  82.  
  83.  
  84. def sched_message(bot, update, minutes):
  85. """ Create and schedule a message """
  86. message = f'Item will be there soon! @{update.message.from_user.username}'
  87. notify_in = minutes*60
  88. schedule.run_once(notify_subscriber, notify_in,
  89. context={
  90. 'chat_id': update.message.chat_id,
  91. 'message': message
  92. })
  93.  
  94. print(f'Subscription set. Notification in {minutes}m ~~ {notify_in}s.')
  95. bot.send_message(chat_id=update.message.chat_id, text=config['MESSAGE']['NOTIFYSET'])
  96.  
  97.  
  98. if __name__ == '__main__':
  99. updater = Updater(token=config['TGTOKEN'])
  100. dispatcher = updater.dispatcher
  101. schedule = updater.job_queue
  102. setup_filter = SetupFilter()
  103.  
  104. start_handler = CommandHandler('start', start)
  105. rm_handler = MessageHandler(setup_filter, set_reminder)
  106. dispatcher.add_handler(start_handler)
  107. dispatcher.add_handler(rm_handler)
  108.  
  109. updater.start_polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement