Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telegram.ext import Updater
- from telegram.ext import CommandHandler
- from telegram.ext import MessageHandler
- from telegram.ext import BaseFilter
- from comms import Communicate
- import logging
- import json
- class SetupFilter(BaseFilter):
- """ Custom filter checking for command initializer """
- def filter(self, message):
- try:
- return config['INITCOMMAND'] == message.text.split()[0]
- except AttributeError:
- return False
- with open('config.json') as config_file:
- try:
- config = json.load(config_file)
- except (KeyError, ValueError):
- raise
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
- def start(bot, update):
- """ Initializing bot """
- bot.send_message(chat_id=update.message.chat_id, text=config['MESSAGE']['START'])
- def get_message_by_type(msg_type):
- """ Get message from config based on given type """
- types = config['MESSAGE']
- if msg_type == 1:
- return types['START']
- elif msg_type == 2:
- return types['ERROR']
- elif msg_type == 3:
- return types['NOTIFYSET']
- else:
- return types['ITEMSOLD']
- def exception_message(bot, chat_id, message_type):
- """ Error message handler """
- message = get_message_by_type(message_type)
- return bot.send_message(chat_id=chat_id, text=message)
- def set_reminder(bot, update):
- """ Calculate and find any bad data that might have been typed in """
- args = update.message.text.split()
- chat_id = update.message.chat_id
- if len(args) != 1 and len(args) != 2:
- return exception_message(bot, chat_id, Communicate.ERROR)
- if len(args) == 1:
- return sched_message(bot, update, config['SENDIN'])
- msg_sent_at = update.message['date'].minute
- try:
- init_minute = int(args[1])
- except ValueError:
- return exception_message(bot, chat_id, Communicate.ERROR)
- if init_minute < 0 or init_minute > 59:
- return exception_message(bot, chat_id, Communicate.ERROR)
- if msg_sent_at < init_minute:
- msg_sent_at += 60
- if msg_sent_at - init_minute >= 15:
- return exception_message(bot, chat_id, Communicate.SOLD)
- notify_minute = msg_sent_at - init_minute - config['SENDIN']
- return sched_message(bot, update, abs(notify_minute))
- def notify_subscriber(bot, job):
- """ Send message to a subscriber """
- return bot.send_message(chat_id=job.context['chat_id'], text=job.context['message'])
- def sched_message(bot, update, minutes):
- """ Create and schedule a message """
- message = f'Item will be there soon! @{update.message.from_user.username}'
- notify_in = minutes*60
- schedule.run_once(notify_subscriber, notify_in,
- context={
- 'chat_id': update.message.chat_id,
- 'message': message
- })
- print(f'Subscription set. Notification in {minutes}m ~~ {notify_in}s.')
- bot.send_message(chat_id=update.message.chat_id, text=config['MESSAGE']['NOTIFYSET'])
- if __name__ == '__main__':
- updater = Updater(token=config['TGTOKEN'])
- dispatcher = updater.dispatcher
- schedule = updater.job_queue
- setup_filter = SetupFilter()
- start_handler = CommandHandler('start', start)
- rm_handler = MessageHandler(setup_filter, set_reminder)
- dispatcher.add_handler(start_handler)
- dispatcher.add_handler(rm_handler)
- updater.start_polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement