def test_perChatMessageWithoutChat(self): handler = ConversationHandler( entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) user = User(first_name="Misses Test", id=123) cbq = CallbackQuery(0, user, None, None) update = Update(0, callback_query=cbq) handler.check_update(update)
def test_per_chat_message_without_chat(self, bot, user1): handler = ConversationHandler( entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) cbq = CallbackQuery(0, user1, None, None, bot=bot) update = Update(0, callback_query=cbq) assert not handler.check_update(update)
def test_channel_message_without_chat(self, bot): handler = ConversationHandler( entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, None, None, Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot) update = Update(0, message=message) assert not handler.check_update(update)
def test_all_update_types(self, dp, bot, user1): handler = ConversationHandler( entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, user1, None, self.group, text='ignore', bot=bot) callback_query = CallbackQuery(0, user1, None, message=message, data='data', bot=bot) chosen_inline_result = ChosenInlineResult(0, user1, 'query', bot=bot) inline_query = InlineQuery(0, user1, 'query', 0, bot=bot) pre_checkout_query = PreCheckoutQuery(0, user1, 'USD', 100, [], bot=bot) shipping_query = ShippingQuery(0, user1, [], None, bot=bot) assert not handler.check_update( Update(0, callback_query=callback_query)) assert not handler.check_update( Update(0, chosen_inline_result=chosen_inline_result)) assert not handler.check_update(Update(0, inline_query=inline_query)) assert not handler.check_update(Update(0, message=message)) assert not handler.check_update( Update(0, pre_checkout_query=pre_checkout_query)) assert not handler.check_update( Update(0, shipping_query=shipping_query))
def test_all_update_types(self, dp, bot, user1): handler = ConversationHandler(entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, user1, None, self.group, text='ignore', bot=bot) callback_query = CallbackQuery(0, user1, None, message=message, data='data', bot=bot) chosen_inline_result = ChosenInlineResult(0, user1, 'query', bot=bot) inline_query = InlineQuery(0, user1, 'query', 0, bot=bot) pre_checkout_query = PreCheckoutQuery(0, user1, 'USD', 100, [], bot=bot) shipping_query = ShippingQuery(0, user1, [], None, bot=bot) assert not handler.check_update(Update(0, callback_query=callback_query)) assert not handler.check_update(Update(0, chosen_inline_result=chosen_inline_result)) assert not handler.check_update(Update(0, inline_query=inline_query)) assert not handler.check_update(Update(0, message=message)) assert not handler.check_update(Update(0, pre_checkout_query=pre_checkout_query)) assert not handler.check_update(Update(0, shipping_query=shipping_query))
def test_channelMessageWithoutChat(self): handler = ConversationHandler(entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, None, None, Chat(0, Chat.CHANNEL, "Misses Test")) update = Update(0, message=message) handler.check_update(update)
class BaseConversations: """ Attributes: server - TelegramBotServer server object entry_cmd - List of commands for entry_points, will be binded to self.start eg. ["/start"] fallback_cmd - List of commands for fallbacks, will be binded to self.cancel eg. ["/cancelstart"] """ __slots__ = ("server", "entry_cmd", "fallback_cmd", "states", "handler") def __init__(self, server, entry_cmd, fallback_cmd, **kwargs): self.server = server self.entry_cmd = entry_cmd self.fallback_cmd = fallback_cmd self.register_conversations(**kwargs) def set_states(self): """ Implement setting self.states with conversations flow and various handlers / filters with the class's method eg. { 0: [RegexHandler('^(Boy|Girl|Other)$', self.gender)], 1: [MessageHandler(Filters.photo, self.photo), CommandHandler('skip', self.skip_photo)], 2: [MessageHandler(Filters.location, self.location), CommandHandler('skip', self.skip_location)], 3: [MessageHandler(Filters.text, self.bio)] } """ raise NotImplementedError def register_conversations(self, **kwargs): self.set_states() self.handler = ConversationHandler( entry_points=self.return_entry_points(), states=self.states, fallbacks=self.return_fallbacks(), **kwargs ) self.server.dp.add_handler(self.handler) def return_entry_points(self): return [ CommandHandler(command, self.start) for command in self.entry_cmd ] def return_fallbacks(self): return [ CommandHandler(command, self.cancel) for command in self.fallback_cmd ] def start(self, update, context, *args, **kwargs): pass def cancel(self, update, context, *args, **kwargs): pass def end(self, update, context, *args, **kwargs): check = self.handler.check_update(update) self.handler.update_state(ConversationHandler.END, check[0])
def test_channel_message_without_chat(self, bot): handler = ConversationHandler(entry_points=[CommandHandler('start', self.start_end)], states={}, fallbacks=[]) message = Message(0, None, None, Chat(0, Chat.CHANNEL, 'Misses Test'), bot=bot) update = Update(0, message=message) assert not handler.check_update(update)