def test_pass_job_or_update_queue(self, dp): handler = StringRegexHandler('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 = StringRegexHandler('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 = StringRegexHandler('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, mro_slots, recwarn): inst = StringRegexHandler('pfft', 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
def test_basic(self, dp): handler = StringRegexHandler('(?P<begin>.*)est(?P<end>.*)', self.callback_basic) dp.add_handler(handler) assert handler.check_update('test message') dp.process_update('test message') assert self.test_flag assert not handler.check_update('does not match')
async def test_basic(self, app, compile): pattern = "(?P<begin>.*)est(?P<end>.*)" if compile: pattern = re.compile("(?P<begin>.*)est(?P<end>.*)") handler = StringRegexHandler(pattern, self.callback) app.add_handler(handler) assert handler.check_update("test message") async with app: await app.process_update("test message") assert self.test_flag assert not handler.check_update("does not match")
def test_context_pattern(self, cdp): handler = StringRegexHandler(r'(t)est(.*)', self.callback_context_pattern) cdp.add_handler(handler) cdp.process_update('test message') assert self.test_flag cdp.remove_handler(handler) handler = StringRegexHandler(r'(t)est(.*)', self.callback_context_pattern) cdp.add_handler(handler) cdp.process_update('test message') assert self.test_flag
async def test_context_pattern(self, app): handler = StringRegexHandler(r"(t)est(.*)", self.callback_pattern) app.add_handler(handler) async with app: await app.process_update("test message") assert self.test_flag app.remove_handler(handler) handler = StringRegexHandler(r"(t)est(.*)", self.callback_pattern) app.add_handler(handler) await app.process_update("test message") assert self.test_flag
def test_with_passing_group_dict(self, dp): handler = StringRegexHandler('(?P<begin>.*)est(?P<end>.*)', self.callback_group, pass_groups=True) dp.add_handler(handler) dp.process_update('test message') assert self.test_flag dp.remove_handler(handler) handler = StringRegexHandler('(?P<begin>.*)est(?P<end>.*)', self.callback_group, pass_groupdict=True) dp.add_handler(handler) self.test_flag = False dp.process_update('test message') assert self.test_flag
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_other_update_types(self, false_update): handler = StringRegexHandler('test', self.callback_basic) assert not handler.check_update(false_update)
def test_slot_behaviour(self, mro_slots): inst = StringRegexHandler("pfft", 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(): print(token) # Create the EventHandler and pass it your bot's token. updater = Updater(token, workers=10) # Get the dispatcher to register handlers dp = updater.dispatcher inline_caps_handler = InlineQueryHandler(inline_caps) # This is how we add handlers for Telegram messages dp.addHandler(inline_caps_handler) dp.addHandler(CommandHandler(CommandsKeys.start, start)) dp.addHandler(CommandHandler(CommandsKeys.check_in, check_in)) dp.addHandler(CommandHandler(CommandsKeys.check_out, check_out)) dp.addHandler(CommandHandler(CommandsKeys.man, man)) dp.addHandler(CommandHandler(CommandsKeys.check_remove, check_remove)) dp.addHandler(CommandHandler(CommandsKeys.cheked_time, checked_time)) dp.addHandler(CommandHandler(CommandsKeys.esc, esc)) [dp.addHandler(CommandHandler(x, man)) for x in CommandsKeys.man_pool] # Message handlers only receive updates that don't contain commands # dp.addHandler(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.addHandler(RegexHandler('.*', any_message), group='log') # String handlers work pretty much the same. Note that we have to tell # the handler to pass the args or update_queue parameter # dp.addHandler(StringCommandHandler('reply', cli_reply, pass_args=True)) dp.addHandler( StringRegexHandler(r'/.*', 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.addErrorHandler(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: try: text = raw_input() except NameError: 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)