|
| 1 | +''' |
| 2 | +This module makes the bot actually run |
| 3 | +''' |
| 4 | + |
| 5 | +from telegram.ext import CommandHandler, MessageHandler, Filters, Updater |
| 6 | +# python-telegram-bot is a Pythonic Wrapper to the core Telegram API |
| 7 | +# it helps us to be DRY by giving us convinient wrapper functions to deal with Telegram API |
| 8 | +# you can install it by pip install python-telegram-bot --upgrade |
| 9 | +# learn more about it here https://github.com/python-telegram-bot/python-telegram-bot |
| 10 | + |
| 11 | + |
| 12 | +from .execute_code import eval_py, run |
| 13 | + |
| 14 | +# read the token for authenticating our bot |
| 15 | +with open('token.txt') as f: |
| 16 | + tok = f.readline().strip() |
| 17 | + |
| 18 | +with open('docs/start.txt') as f: |
| 19 | + start_text = f.read() |
| 20 | + |
| 21 | +with open('docs/help.txt') as f: |
| 22 | + help_text = f.read() |
| 23 | + |
| 24 | +with open('docs/code.txt') as f: |
| 25 | + code_text = f.read() |
| 26 | + |
| 27 | + |
| 28 | +def handle_long_message(msg): |
| 29 | + '''Telegram does not support messages over 4096 characters. |
| 30 | + This handler handles all messages above 2000 characters |
| 31 | + ''' |
| 32 | + |
| 33 | + if msg: |
| 34 | + if len(msg) > 2000: |
| 35 | + return msg[:2000]+'\n\n 😟 Output was too long, truncated to 2000 characters' |
| 36 | + return msg |
| 37 | + return 'handle_long_message recieved an empty message' |
| 38 | + |
| 39 | + |
| 40 | +def bot(): |
| 41 | + ''' |
| 42 | + Running this function runs the bot |
| 43 | + You may learn more from this tutorial |
| 44 | + https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-Your-first-Bot |
| 45 | + ''' |
| 46 | + |
| 47 | + updater = Updater(token=tok) |
| 48 | + |
| 49 | + dispatcher = updater.dispatcher |
| 50 | + |
| 51 | + def start(update, context): |
| 52 | + '''This fuction replies to the start command''' |
| 53 | + |
| 54 | + context.bot.send_message( |
| 55 | + chat_id=update.effective_chat.id, text=start_text, parse_mode='Markdown') |
| 56 | + # for more info on parse modes |
| 57 | + # see https://python-telegram-bot.readthedocs.io/en/stable/telegram.parsemode.html |
| 58 | + |
| 59 | + def bot_help(update, context): |
| 60 | + '''This function replies to the help command''' |
| 61 | + context.bot.send_message( |
| 62 | + chat_id=update.effective_chat.id, text=help_text, parse_mode='Markdown') |
| 63 | + |
| 64 | + def code_info(update, context): |
| 65 | + '''This function replies to the code command.''' |
| 66 | + context.bot.send_message( |
| 67 | + chat_id=update.effective_chat.id, text=code_text, parse_mode='Markdown') |
| 68 | + |
| 69 | + def reply_execute(update, context): |
| 70 | + ''' |
| 71 | + This function replies to any non-command messages. |
| 72 | + ''' |
| 73 | + input_text = str(update.message.text) |
| 74 | + # allowing usage of /e at the end of expressions |
| 75 | + |
| 76 | + if input_text == 'hi': |
| 77 | + user = update.message.from_user |
| 78 | + context.bot.send_message(chat_id=update.effective_chat.id, |
| 79 | + text=f'Hi! {user["username"]} 🥰') |
| 80 | + |
| 81 | + if input_text.endswith('/e'): |
| 82 | + message = handle_long_message(eval_py(input_text.strip('/e'))) |
| 83 | + update.message.reply_text(message, quote=True) |
| 84 | + else: |
| 85 | + returned_val = run(update) |
| 86 | + if not returned_val: |
| 87 | + update.message.reply_text( |
| 88 | + '''*No output. No error.* |
| 89 | + \n> Try using a `print` statement. |
| 90 | + \n > To evaluate an expression use the /e command.''', |
| 91 | + quote=True, parse_mode='Markdown') |
| 92 | + else: |
| 93 | + message = handle_long_message(returned_val) |
| 94 | + update.message.reply_text(message, quote=True) |
| 95 | + |
| 96 | + def reply_eval(update, context): |
| 97 | + ''' This function handles the /e command''' |
| 98 | + if context.args: |
| 99 | + input_text = '' |
| 100 | + for string in context.args: |
| 101 | + input_text += string + ' ' |
| 102 | + out = eval_py(input_text) |
| 103 | + message = handle_long_message(out) |
| 104 | + update.message.reply_text(message, quote=True) |
| 105 | + else: |
| 106 | + update.message.reply_text( |
| 107 | + '''*No expression provided to eval*. |
| 108 | + \nUse command /e before or after your expression like |
| 109 | + \n/e `4 >= 5` \n \t or \n`4 >= 5` /e ''', quote=True, parse_mode='Markdown') |
| 110 | + |
| 111 | + _handlers = {} |
| 112 | + |
| 113 | + _handlers['start_handler'] = CommandHandler('start', start) |
| 114 | + _handlers['help_handler'] = CommandHandler('help', bot_help) |
| 115 | + _handlers['code_info_handler'] = CommandHandler('code', code_info) |
| 116 | + _handlers['message_handler'] = MessageHandler( |
| 117 | + Filters.text & (~Filters.command), reply_execute) |
| 118 | + _handlers['eval_handler'] = CommandHandler('e', reply_eval) |
| 119 | + |
| 120 | + for name, _handler in _handlers.items(): |
| 121 | + print(f'Adding {name}') |
| 122 | + dispatcher.add_handler(_handler) |
| 123 | + |
| 124 | + updater.start_polling() |
| 125 | + updater.idle() |
| 126 | + # Search google to know what is polling... |
| 127 | + # i came to know while building this project |
| 128 | + # this can be stopped by interrupting the terminal by `Ctrl+C` |
0 commit comments