def test_pass_job_or_update_queue(self, dp): handler = StringCommandHandler('test', self.callback_queue_1, pass_job_queue=True) dp.add_handler(handler) dp.process_update('/test') assert self.test_flag dp.remove_handler(handler) handler = StringCommandHandler('test', self.callback_queue_1, pass_update_queue=True) dp.add_handler(handler) self.test_flag = False dp.process_update('/test') assert self.test_flag dp.remove_handler(handler) handler = StringCommandHandler('test', self.callback_queue_2, pass_job_queue=True, pass_update_queue=True) dp.add_handler(handler) self.test_flag = False dp.process_update('/test') assert self.test_flag
def test_slot_behaviour(self, recwarn, mro_slots): inst = StringCommandHandler('sleepy', self.callback_basic) for attr in inst.__slots__: assert getattr(inst, attr, 'err') != 'err', f"got extra slot '{attr}'" assert not inst.__dict__, f"got missing slot(s): {inst.__dict__}" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot" inst.custom, inst.callback = 'should give warning', self.callback_basic assert len(recwarn) == 1 and 'custom' in str( recwarn[0].message), recwarn.list
async def test_context(self, app): handler = StringCommandHandler("test", self.callback) app.add_handler(handler) async with app: await app.process_update("/test") assert self.test_flag
def test_slot_behaviour(self, mro_slots): inst = StringCommandHandler("sleepy", self.callback) for attr in inst.__slots__: assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'" assert len(mro_slots(inst)) == len(set( mro_slots(inst))), "duplicate slot"
def main(): # Create the EventHandler and pass it your bot's token. token = 'TOKEN' updater = Updater(token, workers=10) # Get the dispatcher to register handlers dp = updater.dispatcher # This is how we add handlers for Telegram messages dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) # Message handlers only receive updates that don't contain commands dp.add_handler(MessageHandler([Filters.text], message)) # Regex handlers will receive all updates on which their regex matches, # but we have to add it in a separate group, since in one group, # only one handler will be executed dp.add_handler(RegexHandler('.*', any_message), group=1) # String handlers work pretty much the same. Note that we have to tell # the handler to pass the args or update_queue parameter dp.add_handler(StringCommandHandler('reply', cli_reply, pass_args=True)) dp.add_handler( StringRegexHandler('[^/].*', cli_noncommand, pass_update_queue=True)) # All TelegramErrors are caught for you and delivered to the error # handler(s). Other types of Errors are not caught. dp.add_error_handler(error) # Start the Bot and store the update Queue, so we can insert updates update_queue = updater.start_polling(timeout=10) ''' # Alternatively, run with webhook: update_queue = updater.start_webhook('0.0.0.0', 443, url_path=token, cert='cert.pem', key='key.key', webhook_url='https://example.com/%s' % token) # Or, if SSL is handled by a reverse proxy, the webhook URL is already set # and the reverse proxy is configured to deliver directly to port 6000: update_queue = updater.start_webhook('0.0.0.0', 6000) ''' # Start CLI-Loop while True: text = input() # Gracefully stop the event handler if text == 'stop': updater.stop() break # else, put the text into the update queue to be handled by our handlers elif len(text) > 0: update_queue.put(text)
def test_context_args(self, cdp): handler = StringCommandHandler('test', self.callback_context_args) cdp.add_handler(handler) cdp.process_update('/test') assert not self.test_flag cdp.process_update('/test one two') assert self.test_flag
def test_pass_args(self, dp): handler = StringCommandHandler('test', self.sch_callback_args, pass_args=True) dp.add_handler(handler) dp.process_update('/test') assert self.test_flag self.test_flag = False dp.process_update('/test one two') assert self.test_flag
def test_basic(self, dp): handler = StringCommandHandler('test', self.callback_basic) dp.add_handler(handler) assert handler.check_update('/test') dp.process_update('/test') assert self.test_flag assert not handler.check_update('/nottest') assert not handler.check_update('not /test in front') assert handler.check_update('/test followed by text')
def test_basic(self, dp): handler = StringCommandHandler('test', self.callback_basic) dp.add_handler(handler) check = handler.check_update('/test') assert check is not None and check is not False dp.process_update('/test') assert self.test_flag check = handler.check_update('/nottest') assert check is None or check is False check = handler.check_update('not /test in front') assert check is None or check is False check = handler.check_update('/test followed by text') assert check is not None and check is not False
def test_other_update_types(self, false_update): handler = StringCommandHandler('test', self.callback_basic) assert not handler.check_update(false_update)