예제 #1
0
def test_get_handlers():
    core.BotRouter.clean()

    @dataclass
    class FakeHandler:
        name: str = ""

    example1_instance = None

    @core.bot_handler
    def example_factory():
        nonlocal example1_instance
        example1_instance = FakeHandler("example_factory")
        return example1_instance

    example2_instance = None

    @core.bot_handler
    def example_factory2():
        nonlocal example2_instance
        example2_instance = FakeHandler("example_factory2")
        return example2_instance

    dispatcher_mock = MagicMock()
    core.BotRouter.setup_handlers(dispatcher_mock)
    handlers = list(core.get_handlers())
    assert handlers == [example1_instance, example2_instance]
    assert [id(h) for h in handlers
            ] == [id(example1_instance),
                  id(example2_instance)]
    assert dispatcher_mock.add_handler.call_count == 2
예제 #2
0
 def create_bot(self, app):
     token = app.config.get("API_TOKEN")
     autodiscovery(app.config.get("APPS", []))
     self.bot = telegram.Bot(token=token)
     self.dispatcher = Dispatcher(self.bot, None, workers=0)
     for handler in get_handlers():
         self.dispatcher.add_handler(handler)
     # log all errors
     self.dispatcher.add_error_handler(self.error)
예제 #3
0
파일: test_core.py 프로젝트: vcali/bot
def test_get_handlers():
    core.BotRouter.clean()

    @core.bot_handler
    def example_factory():
        return "example_factory"

    @core.bot_handler
    def example_factory2():
        return "example_factory2"

    handlers = list(core.get_handlers())
    assert handlers == ["example_factory", "example_factory2"]
예제 #4
0
def main():
    if not settings.API_TOKEN:
        logger.critical("Telegram API Token is missing(TELEGRAM_API_TOKEN)")
        return 1

    updater = Updater(settings.API_TOKEN)
    autodiscovery(settings.APPS)
    dp = updater.dispatcher
    for handler in get_handlers():
        dp.add_handler(handler)
    # log all errors
    dp.add_error_handler(error)
    updater.start_polling()
    updater.idle()
    return 0
예제 #5
0
 def reload_state(self):
     if self.persistence.store_user_data:
         self.dispatcher.user_data = self.persistence.get_user_data()
         if not isinstance(self.dispatcher.user_data, defaultdict):
             raise ValueError("user_data must be of type defaultdict")
     if self.persistence.store_chat_data:
         self.dispatcher.chat_data = self.persistence.get_chat_data()
         if not isinstance(self.dispatcher.chat_data, defaultdict):
             raise ValueError("chat_data must be of type defaultdict")
     if self.persistence.store_bot_data:
         self.dispatcher.bot_data = self.persistence.get_bot_data()
         if not isinstance(self.dispatcher.bot_data, dict):
             raise ValueError("bot_data must be of type dict")
     for handler in get_handlers():
         if isinstance(handler, ConversationHandler) and handler.persistent:
             if not self.persistence:
                 raise ValueError(
                     "Conversationhandler {} can not be persistent if dispatcher "
                     "has no persistence".format(handler.name))
             handler.persistence = self.persistence
             handler.conversations = self.persistence.get_conversations(
                 handler.name)