예제 #1
0
def income(api_key):
    if api_key != API_KEY:
        error_msg = "Wrong API key: {wrong_key}".format(wrong_key=api_key)
        logger.warning(error_msg)
        return error_msg, 403
    from pprint import pformat
    logger.info("INCOME:\n{}\n\nHEADER:\n{}".format(
        pformat(request.get_json()),
        request.headers if hasattr(request, "headers") else None))
    update = Update.from_array(request.get_json())
    logger.debug("UPDATE: {}".format(update))
    # assert isinstance(msg, Message)
    if update.message and update.message.text:
        receiver = update.message.chat.id
        if update.message.text == "/start":
            bot.send_message(receiver,
                             mlfw.START_TEXT.format(username=username),
                             parse_mode="html")
            return "ok"
        # end if
        if update.message.text == "/help":
            bot.send_message(receiver,
                             mlfw.HELP_TEXT.format(username=username),
                             parse_mode="html")
            return "ok"
        # end if
    if not update.inline_query:
        logger.debug("Skipping update without inline query.")
        return "ok"
    inline_query_id = update.inline_query.id
    query = update.inline_query.query
    query_offset = update.inline_query.offset
    mlfw.search(query, inline_query_id, offset=query_offset)
    return "ok"
예제 #2
0
 def test__process_update(self):
     self.assertNotIn("process_update", self.callbacks_status,
                      "no data => not executed yet")
     from pytgbot.api_types.receivable.updates import Update
     update = Update(1000)
     self.mixin.process_update(update)
     self.assertIn("process_update", self.callbacks_status,
                   "has data => executed")
     self.assertEqual(self.callbacks_status["process_update"], update,
                      "same => data correct")
예제 #3
0
    def test_commands(self):
        self.m.BEST_PONY = self.s
        self.assertEqual(self.m.CURRENT, self.m.DEFAULT)
        self.assertEqual(self.m.CURRENT.name, 'DEFAULT')

        update = Update(1,
                        message=Message(2,
                                        date=int(time.time()),
                                        from_peer=User(3, False, "Günter"),
                                        chat=Chat(4, 'supergroup',
                                                  'FAKE CHAT'),
                                        text='/start'))
        called = [False, False]

        def call_me(i):
            def call_me_inner(u, text):
                logger.info('called {i}.'.format(i=i))
                self.assertEqual(u, update)
                called[i] = True

            # end def
            return call_me_inner

        # end def

        self.m.DEFAULT.command('start')(call_me(0))
        self.m.BEST_PONY.command('start')(call_me(1))

        self.m.BEST_PONY.activate()
        self.assertEqual(self.m.CURRENT, self.s)
        self.assertEqual(self.m.CURRENT, self.m.BEST_PONY)
        self.assertEqual(self.m.CURRENT.name, 'BEST_PONY')

        self.m.process_update(update)
        self.assertEqual(
            self.m.CURRENT, self.m.DEFAULT,
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertEqual(
            self.m.CURRENT.name, 'DEFAULT',
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertTrue(
            called[0],
            'DEFAULT should have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
        self.assertFalse(
            called[1],
            'BEST_PONY should not have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
예제 #4
0
    def test_updates_parent_not_implemented(self):
        update = Update(1)
        m = TeleStateMachine('a', database_driver=TeleStateDatabaseDriver())
        with self.assertRaises(
                NotImplementedError,
                msg=
                "should require subclasses to implement load_state_for_chat_user"
        ):
            m.database_driver.load_state_for_chat_user(0, 0)
        # end with

        with self.assertRaises(
                NotImplementedError,
                msg=
                "should require subclasses to implement save_state_for_chat_user"
        ):
            m.database_driver.save_state_for_chat_user(0, 0, "", None)
 async def send_event(self, data: Update):
     json = data.to_array() if isinstance(data, TgBotApiObject) else data
     logger.debug(f"Processing event: {json!r}")
     if self.mode == UpdateModes.POLLING:
         # Store the updates
         logger.debug('storing event.')
         self.updates.append(data)
     elif self.mode == UpdateModes.WEBHOOK:
         # Send the updates
         logger.debug('sending to the webhook.')
         async with httpx.AsyncClient() as client:
             async with client.post(self.webhook_url,
                                    json=json) as response:
                 logger.info("Response: " + repr(await response.text()))
             # end with
         # end with
     elif self.mode == UpdateModes.SILENT:
         # Ignore the updates
         logger.debug('silently ignoring update.')
     else:
         raise AssertionError(f'self.mode ({self.mode!r}) is unknown.')
예제 #6
0
    def test_updates(self):
        self.m.BEST_PONY = self.s
        self.assertEqual(self.m.CURRENT, self.m.DEFAULT)
        self.assertEqual(self.m.CURRENT.name, 'DEFAULT')
        update = Update(1)
        called = [False, False]

        def call_me(i):
            def call_me_inner(u):
                self.assertEqual(u, update)
                called[i] = True

            # end def
            return call_me_inner

        # end def

        self.m.DEFAULT.on_update()(call_me(0))
        self.m.BEST_PONY.on_update()(call_me(1))

        self.m.BEST_PONY.activate()
        self.assertEqual(self.m.CURRENT, self.s)
        self.assertEqual(self.m.CURRENT, self.m.BEST_PONY)
        self.assertEqual(self.m.CURRENT.name, 'BEST_PONY')

        self.m.process_update(update)
        self.assertEqual(
            self.m.CURRENT, self.m.DEFAULT,
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertEqual(
            self.m.CURRENT.name, 'DEFAULT',
            "load_state_for_chat_user should set DEFAULT (None) state again.")
        self.assertTrue(
            called[0],
            'DEFAULT should have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
        self.assertFalse(
            called[1],
            'BEST_PONY should not have been called: load_state_for_chat_user set DEFAULT (None) state again.'
        )
예제 #7
0
    def view_updates(self):
        """
        This processes incoming telegram updates.

        :return:
        """
        from pprint import pformat
        from flask import request

        logger.debug("INCOME:\n{}\n\nHEADER:\n{}".format(
            pformat(request.get_json()),
            request.headers if hasattr(request, "headers") else None
        ))
        update = TGUpdate.from_array(request.get_json())
        try:
            result = self.process_update(update)
        except Exception as e:
            logger.exception("process_update()")
            result = {"status": "error", "message": str(e)}
        result = result if result else {"status": "probably ok"}
        logger.info("returning result: {}".format(result))
        return result
예제 #8
0
파일: base.py 프로젝트: TheRinger/teleflask
    def view_updates(self):
        """
        This processes incoming telegram updates.

        :return:
        """
        from pprint import pformat
        from flask import request
        from pytgbot.api_types.receivable.updates import Update

        logger.debug("INCOME:\n{}\n\nHEADER:\n{}".format(
            pformat(request.get_json()),
            request.headers if hasattr(request, "headers") else None
        ))
        update = Update.from_array(request.get_json())
        try:
            result = self.process_update(update)
        except Exception as e:
            logger.exception("process_update()")
            result = {"status": "error", "message": str(e)}
        result = result if result else {"status": "probably ok"}
        logger.info("returning result: {}".format(result))
        return result
예제 #9
0
    def get_updates(self, offset=None, limit=100, poll_timeout=0, allowed_updates=None, request_timeout=None, delta=timedelta(milliseconds=100), error_as_empty=False):
        """
        Use this method to receive incoming updates using long polling. An Array of Update objects is returned.

        You can choose to set `error_as_empty` to `True` or `False`.
        If `error_as_empty` is set to `True`, it will log that exception as warning, and fake an empty result,
        intended for use in for loops. In case of such error (and only in such case) it contains an "exception" field.
        Ìt will look like this: `{"result": [], "exception": e}`
        This is useful if you want to use a for loop, but ignore Network related burps.

        If `error_as_empty` is set to `False` however, all `requests.RequestException` exceptions are normally raised.

        :keyword offset: (Optional)	Identifier of the first update to be returned.
                 Must be greater by one than the highest among the identifiers of previously received updates.
                 By default, updates starting with the earliest unconfirmed update are returned.
                 An update is considered confirmed as soon as :func:`get_updates` is called with
                 an offset higher than its `update_id`.
        :type offset: int

        :param limit: Limits the number of updates to be retrieved. Values between 1—100 are accepted. Defaults to 100
        :type  limit: int

        :param poll_timeout: Timeout in seconds for long polling, e.g. how long we want to wait maximum.
                               Defaults to 0, i.e. usual short polling.
        :type  poll_timeout: int

        :param allowed_updates: List the types of updates you want your bot to receive.
                                  For example, specify [“message”, “edited_channel_post”, “callback_query”] to only
                                  receive updates of these types. See Update for a complete list of available update
                                  types. Specify an empty list to receive all updates regardless of type (default).
                                  If not specified, the previous setting will be used. Please note that this parameter
                                  doesn't affect updates created before the call to the get_updates,
                                  so unwanted updates may be received for a short period of time.
        :type allowed_updates: list of str


        :param request_timeout: Timeout of the request. Not the long polling server side timeout.
                                  If not specified, it is set to `poll_timeout`+2.
        :type request_timeout: int

        :param delta: Wait minimal 'delta' seconds, between requests. Useful in a loop.
        :type  delta: datetime.

        :param error_as_empty: If errors which subclasses `requests.RequestException` will be logged but not raised.
                 Instead the returned DictObject will contain an "exception" field containing the exception occured,
                 the "result" field will be an empty list `[]`. Defaults to `False`.
        :type  error_as_empty: bool


        Returns:

        :return: An Array of Update objects is returned,
                 or an empty array if there was an requests.RequestException and error_as_empty is set to True.
        :rtype: list of pytgbot.api_types.receivable.updates.Update
        """
        assert(offset is None or isinstance(offset, int))
        assert(limit is None or isinstance(limit, int))
        assert(poll_timeout is None or isinstance(poll_timeout, int))
        assert(allowed_updates is None or isinstance(allowed_updates, list))
        if poll_timeout and not request_timeout is None:
            request_timeout = poll_timeout + 2
        # end if

        if delta.total_seconds() > poll_timeout:
            now = datetime.now()
            if now - self._last_update < delta:
                wait = ((now - self._last_update) - delta).total_seconds()  # can be 0.2
                wait = 0 if wait < 0 else wait
                if wait != 0:
                    logger.debug("Sleeping {i} seconds.".format(i=wait))
                # end if
                sleep(wait)
            # end if
        # end if
        self._last_update = datetime.now()
        use_long_polling = poll_timeout != 0
        try:
            result = self.do(
                "getUpdates", offset=offset, limit=limit, timeout=poll_timeout, allowed_updates=allowed_updates,
                use_long_polling=use_long_polling, request_timeout=request_timeout
            )
            if self.return_python_objects:
                logger.debug("Trying to parse {data}".format(data=repr(result)))
                from pytgbot.api_types.receivable.updates import Update
                try:
                    return Update.from_array_list(result, list_level=1)
                except TgApiParseException:
                    logger.debug("Failed parsing as api_type Update", exc_info=True)
                # end try
                # no valid parsing so far
                raise TgApiParseException("Could not parse result.")  # See debug log for details!
            # end if return_python_objects
            return result
        except (requests.RequestException, TgApiException) as e:
            if error_as_empty:
                if not isinstance(e, requests.exceptions.Timeout) or not use_long_polling:
                    logger.warn(
                        "Network related error happened in get_updates(), but will be ignored: " + str(e),
                        exc_info=True
                    )
                # end if
                self._last_update = datetime.now()
                if self.return_python_objects:
                    return []
                else:
                    return DictObject(result=[], exception=e)
                # end if
            else:
                raise
예제 #10
0
class SomeUpdatesMixinTestCase(unittest.TestCase):
    """
    `@app.on_update` decorator
    `app.add_update_listener(func)`
    `app.remove_update_listener(func)`
    """

    def setUp(self):
        print("setUp")
        # Array to hold information about the called callbacks:
        self.callbacks_status = {}
        self.mixin = BotCommandsMixinMockup(callback_status=self.callbacks_status)
        print(self.mixin)
    # end def

    def tearDown(self):
        print("tearDown")
        del self.callbacks_status
        del self.mixin.commands
        del self.mixin
    # end def

    command_test = Update.from_array({
        "message_id": 878,
        "from": {
            "id": 2,
            "first_name": "Test User",
            "username": "******",
            "language_code": "en"
        },
        "chat": {
            "id": 10717954,
            "first_name": "Luckydonald",
            "username": "******",
            "type": "private"
        },
        "date": 1495133903,
        "reply_to_message": {
            "message_id": 874,
            "from": {
                "id": 10717954,
                "first_name": "Luckydonald",
                "username": "******",
                "language_code": "en"
            },
            "chat": {
                "id": 10717954,
                "first_name": "Luckydonald",
                "username": "******",
                "type": "private"
            },
            "date": 1493146624,
            "text": "⁠⁠"
        },
        "text": "/test",
        "entities": [
            {
                "type": "bot_command",
                "offset": 0,
                "length": 5
            }
        ]
    })

    data_msg_with_reply = Update.from_array({
        "update_id": 10000,
        "message": {
            "date": 1441645532,
            "chat": {
                "last_name": "Test Lastname",
                "type": "private",
                "id": 1111111,
                "first_name": "Test Firstname",
                "username": "******"
            },
            "message_id": 1365,
            "from": {
                "last_name": "Test Lastname",
                "id": 1111111,
                "first_name": "Test Firstname",
                "username": "******"
            },
            "text": "start",
            "reply_to_message": {
                "date": 1441645000,
                "chat": {
                    "last_name": "Reply Lastname",
                    "type": "private",
                    "id": 1111112,
                    "first_name": "Reply Firstname",
                    "username": "******"
                },
                "message_id": 1334,
                "text": "Original"
            }
        }
    })

    def test__on_update__all(self):
        self.assertNotIn("on_update", self.callbacks_status, "no data => not executed yet")
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        @self.mixin.on_update
        def on_update__callback(update):
            self.callbacks_status["on_update"] = update
            return update
        # end def

        self.assertIsNotNone(on_update__callback, "function is not None => decorator returned something")
        self.assertIn(on_update__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertNotIn("on_update", self.callbacks_status, "no data => not executed yet")

        self.mixin.process_update(self.data_msg_with_reply)

        self.assertIn("on_update", self.callbacks_status, "has data => did execute")
        self.assertEqual(self.callbacks_status["on_update"], self.data_msg_with_reply,
                         "has update => successfully executed given function")
        self.assertIn("processed_update", self.callbacks_status, "executed result collection")
        self.assertEqual(self.callbacks_status["processed_update"],
                         (self.data_msg_with_reply, self.data_msg_with_reply))  # update, result

        self.mixin.process_update(self.data_edit)

        self.assertIn("on_update", self.callbacks_status, "has data => did execute")
        self.assertEqual(self.callbacks_status["on_update"], self.data_edit,
                         "has update => successfully executed given function")
        self.assertIn("processed_update", self.callbacks_status, "executed result collection")
        self.assertEqual(self.callbacks_status["processed_update"], (self.data_edit, self.data_edit))  # update, result

    # end def

    def test__on_update__exception__single(self):
        self.assertNotIn("on_update", self.callbacks_status, "no data => not executed yet")
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        @self.mixin.on_update
        def on_update__callback(update):
            self.callbacks_status["on_update_e1"] = update
            raise ArithmeticError("Exception Test")

        # end def

        self.assertIsNotNone(on_update__callback, "function is not None => decorator returned something")
        self.assertIn(on_update__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertNotIn("on_update_e1", self.callbacks_status, "no data => not executed yet")

        self.mixin.process_update(self.data_msg_with_reply)

        self.assertIn("on_update_e1", self.callbacks_status, "has data => did execute")
        self.assertEqual(self.callbacks_status["on_update_e1"], self.data_msg_with_reply,
                         "has update => successfully called given function")
        self.assertNotIn("processed_update", self.callbacks_status,
                         "did not execute processing updates => exeption raised sucessfully")

    # end def

    def test__on_update__exception(self):
        self.assertNotIn("on_update", self.callbacks_status, "no data => not executed yet")
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")
        self.callbacks_status["on_update_es"] = list()
        self.assertIn("on_update_es", self.callbacks_status, "just test setup")
        self.assertListEqual(self.callbacks_status["on_update_es"], [], "just test setup")

        @self.mixin.on_update
        def on_update__callback1(update):
            self.callbacks_status["on_update_e1"] = update
            self.callbacks_status["on_update_es"].append(1)
            return 1

        # end def

        @self.mixin.on_update
        def on_update__callback2(update):
            self.callbacks_status["on_update_e2"] = update
            self.callbacks_status["on_update_es"].append(2)
            raise ArithmeticError("Exception Test")

        # end def

        @self.mixin.on_update
        def on_update__callback3(update):
            self.callbacks_status["on_update_e3"] = update
            self.callbacks_status["on_update_es"].append(3)
            return 3

        # end def

        self.assertIsNotNone(on_update__callback1, "function 1 is not None => decorator returned something")
        self.assertIsNotNone(on_update__callback2, "function 2 is not None => decorator returned something")
        self.assertIsNotNone(on_update__callback3, "function 3 is not None => decorator returned something")
        self.assertIn(on_update__callback1, self.mixin.update_listeners, "1 in list => listener added")
        self.assertIn(on_update__callback2, self.mixin.update_listeners, "2 in list => listener added")
        self.assertIn(on_update__callback3, self.mixin.update_listeners, "3 in list => listener added")
        self.assertNotIn("on_update_e1", self.callbacks_status, "1 no data => 1 not executed yet")
        self.assertNotIn("on_update_e2", self.callbacks_status, "2 no data => 2 not executed yet")
        self.assertNotIn("on_update_e3", self.callbacks_status, "3 no data => 3 not executed yet")
        self.assertListEqual(self.callbacks_status["on_update_es"], [], "no data => all not executed yet")

        self.mixin.process_update(self.data_msg_with_reply)

        self.assertIn("on_update_e1", self.callbacks_status, "1 has data => 1 did execute")
        self.assertIn("on_update_e2", self.callbacks_status, "2 has data => 2 did execute")
        self.assertIn("on_update_e3", self.callbacks_status, "3 has data => 3 did execute")
        self.assertEqual(self.callbacks_status["on_update_e1"], self.data_msg_with_reply,
                         "1 has update => successfully called given function")
        self.assertEqual(self.callbacks_status["on_update_e2"], self.data_msg_with_reply,
                         "2 has update => successfully called given function")
        self.assertEqual(self.callbacks_status["on_update_e3"], self.data_msg_with_reply,
                         "3 has update => successfully called given function")
        self.assertIn("processed_update", self.callbacks_status,
                      "did execute processing updates => some function executed")
        self.assertListEqual(self.callbacks_status["on_update_es"], [1, 2, 3], "=> successfully executed")

    # end def

    def test__on_update__selective(self):
        self.assertNotIn("on_update2", self.callbacks_status, "no data => not executed yet")
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        @self.mixin.on_update("edited_message")
        def on_update2__callback(update):
            self.callbacks_status["on_update2"] = update
            return update

        # end def

        self.assertIsNotNone(on_update2__callback, "function is not None => decorator returned something")
        self.assertIn(on_update2__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertNotIn("on_update2", self.callbacks_status, "no data => not executed yet")

        self.mixin.process_update(self.data_msg_with_reply)

        self.assertNotIn("on_update2", self.callbacks_status,
                         "no data => not executed => filtered non-'edited_message'")
        self.assertNotIn("processed_update", self.callbacks_status,
                         "no result collected => filtered non-'edited_message'")

        self.mixin.process_update(self.data_edit)

        self.assertIn("on_update2", self.callbacks_status, "has data => did execute")
        self.assertEqual(self.callbacks_status["on_update2"], self.data_edit,
                         "has update => successfully executed given function")
        self.assertIn("processed_update", self.callbacks_status, "executed result collection")
        self.assertEqual(self.callbacks_status["processed_update"], (self.data_edit, self.data_edit))  # update, result

    # end def

    def test__add_update_listener__all(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback)

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners,
                      "function in list => adding worked")

    # end def

    def test__add_update_listener__selective(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback, ["edited_message"])

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners,
                      "function in list => adding worked")

    # end def

    def test__add_update_listener__no_duplicates(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback)

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "=> listener list correct")

        self.mixin.add_update_listener(add_update_listener__callback)

        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "listener list correct => no duplicates")

    # end def

    def test__add_update_listener__no_duplicates__add_keywords(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback, ["edited_message"])

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "=> listener list correct")
        self.assertListEqual(self.mixin.update_listeners[add_update_listener__callback], ["edited_message"],
                             "listener filter list correct")

        self.mixin.add_update_listener(add_update_listener__callback, ["message"])

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener still added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "listener list still correct => no duplicates")
        self.assertListEqual(self.mixin.update_listeners[add_update_listener__callback], ["edited_message", "message"],
                             "listener filter list correct => added keyword")

    # end def

    def test__add_update_listener__no_duplicates__overwrite_unfiltered(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback)

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "=> listener added to list correctly")
        self.assertEqual(self.mixin.update_listeners[add_update_listener__callback], None,
                         "None => listener unfiltered")

        self.mixin.add_update_listener(add_update_listener__callback, ["message"])

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener still added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "listener list still correct => no duplicates")
        self.assertEqual(self.mixin.update_listeners[add_update_listener__callback], None,
                         "listener filter list still None => filter did not overwrite unfiltered")

    # end def

    def test__add_update_listener__no_duplicates__unfiltered_overwrites(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        def add_update_listener__callback(update):
            self.callbacks_status["add_update_listener"] = update
            return update

        # end def

        self.assertFalse(self.mixin.update_listeners, "empty listener list => still not added")

        self.mixin.add_update_listener(add_update_listener__callback, ["message"])

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "listener list is correct")
        self.assertListEqual(self.mixin.update_listeners[add_update_listener__callback], ["message"],
                             "listener filter list correct")

        self.mixin.add_update_listener(add_update_listener__callback)

        self.assertIn(add_update_listener__callback, self.mixin.update_listeners, "in list => listener still added")
        self.assertListEqual(list(self.mixin.update_listeners.keys()), [add_update_listener__callback],
                             "listener list still correct => no duplicates")
        self.assertEqual(self.mixin.update_listeners[add_update_listener__callback], None,
                         "listener filter list now None => filter overwritten with unfiltered")

    # end def

    def test__remove_update_listener(self):
        self.assertFalse(self.mixin.update_listeners, "empty listener list => not added yet")

        @self.mixin.on_update
        def remove_update_listener__callback(update):
            pass

        # end def

        self.assertIn(remove_update_listener__callback, self.mixin.update_listeners, "in list => listener added")

        self.mixin.remove_update_listener(remove_update_listener__callback)

        self.assertFalse(self.mixin.update_listeners, "not in list => removed successfully")
예제 #11
0
class SomeUpdatesMixinTestCase(unittest.TestCase):
    """
    `@app.on_update` decorator
    `app.add_update_listener(func)`
    `app.remove_update_listener(func)`
    """
    def setUp(self):
        print("setUp")
        # Array to hold information about the called callbacks:
        self.callbacks_status = {}
        self.mixin = BotCommandsMixinMockup(
            callback_status=self.callbacks_status)
        print(self.mixin)

    # end def

    def tearDown(self):
        print("tearDown")
        del self.callbacks_status
        del self.mixin.commands
        del self.mixin

    # end def

    command_test = Update.from_array({
        "update_id": 4458,
        "message": {
            "message_id": 878,
            "from": {
                "id": 2,
                "first_name": "Test User",
                "username": "******",
                "language_code": "en"
            },
            "chat": {
                "id": 10717954,
                "first_name": "Luckydonald",
                "username": "******",
                "type": "private"
            },
            "date": 1495133903,
            "reply_to_message": {
                "message_id": 874,
                "from": {
                    "id": 10717954,
                    "first_name": "Luckydonald",
                    "username": "******",
                    "language_code": "en"
                },
                "chat": {
                    "id": 10717954,
                    "first_name": "Luckydonald",
                    "username": "******",
                    "type": "private"
                },
                "date": 1493146624,
                "text": "⁠⁠"
            },
            "text": "/test",
            "entities": [{
                "type": "bot_command",
                "offset": 0,
                "length": 5
            }]
        }
    })

    data_cmd_with_reply = Update.from_array({
        "update_id": 10000,
        "message": {
            "date": 1441645532,
            "chat": {
                "last_name": "Test Lastname",
                "type": "private",
                "id": 1111111,
                "first_name": "Test Firstname",
                "username": "******"
            },
            "message_id": 1365,
            "from": {
                "last_name": "Test Lastname",
                "id": 1111111,
                "first_name": "Test Firstname",
                "username": "******"
            },
            "text": "/test",
            "entities": [{
                "type": "bot_command",
                "offset": 0,
                "length": 5
            }],
            "reply_to_message": {
                "date": 1441645000,
                "chat": {
                    "last_name": "Reply Lastname",
                    "type": "private",
                    "id": 1111112,
                    "first_name": "Reply Firstname",
                    "username": "******"
                },
                "message_id": 1334,
                "text": "Original"
            }
        }
    })

    def test__on_command__command(self):
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

        @self.mixin.command('test')
        def on_command__callback(update, text):
            self.callbacks_status["on_command"] = update
            return update

        # end def

        self.assertIsNotNone(
            on_command__callback,
            "function is not None => decorator returned something")
        self.assertIn('/test', self.mixin.commands.keys(),
                      'command /test in dict keys => listener added')
        self.assertIn('/test@UnitTest', self.mixin.commands,
                      'command /test@{bot} in dict keys => listener added')
        self.assertEqual(self.mixin.commands['/test'],
                         (on_command__callback, False),
                         'command /test has correct function')
        self.assertEqual(self.mixin.commands['/test@UnitTest'],
                         (on_command__callback, False),
                         'command /test has correct function')
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")

        self.mixin.process_update(self.command_test)

        self.assertIn("on_command", self.callbacks_status,
                      "has data => did execute")
        self.assertEqual(self.callbacks_status["on_command"],
                         self.command_test,
                         "has update => successfully executed given function")
        self.assertIn("processed_update", self.callbacks_status,
                      "executed result collection")
        self.assertEqual(
            self.callbacks_status["processed_update"],
            (self.command_test, self.command_test))  # update, result

    # end def

    def test__on_command__command_reply(self):
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

        @self.mixin.command('test')
        def on_command__callback(update, text):
            self.callbacks_status["on_command"] = update
            return update

        # end def

        self.assertIsNotNone(
            on_command__callback,
            "function is not None => decorator returned something")
        self.assertIn('/test', self.mixin.commands.keys(),
                      'command /test in dict keys => listener added')
        self.assertIn('/test@UnitTest', self.mixin.commands,
                      'command /test@{bot} in dict keys => listener added')
        self.assertEqual(self.mixin.commands['/test'],
                         (on_command__callback, False),
                         'command /test has correct function')
        self.assertEqual(self.mixin.commands['/test@UnitTest'],
                         (on_command__callback, False),
                         'command /test has correct function')
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")

        self.mixin.process_update(self.data_cmd_with_reply)

        self.assertIn("on_command", self.callbacks_status,
                      "has data => did execute")
        self.assertEqual(self.callbacks_status["on_command"],
                         self.data_cmd_with_reply,
                         "has update => successfully executed given function")
        self.assertIn("processed_update", self.callbacks_status,
                      "executed result collection")
        self.assertEqual(self.callbacks_status["processed_update"],
                         (self.data_cmd_with_reply,
                          self.data_cmd_with_reply))  # update, result

    # end def

    def test__on_command__no_duplicates(self):
        with self.assertRaises(AssertionError) as e:

            @self.mixin.command('start')
            def on_command__callback1(update):
                pass

            # end def

            @self.mixin.command('start')
            def on_command__callback2(update):
                pass

            # end def
        # end with

    # end def

    def test__on_command__exception(self):
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")
        self.callbacks_status["on_command_es"] = list()
        self.assertIn("on_command_es", self.callbacks_status,
                      "just test setup, fail = broken test")
        self.assertListEqual(self.callbacks_status["on_command_es"], [],
                             "just test setup, fail = broken test")

        @self.mixin.command('start')
        def on_command__callback1(update, text):
            self.callbacks_status["on_command_e1"] = update
            self.callbacks_status["on_command_es"].append(1)
            return 1

        # end def

        @self.mixin.command('start2')
        def on_command__callback2(update, text):
            self.callbacks_status["on_command_e2"] = update
            self.callbacks_status["on_command_es"].append(2)
            raise ArithmeticError("Exception Test")

        # end def

        @self.mixin.command('start23')
        def on_command__callback3(update, text):
            self.callbacks_status["on_command_e3"] = update
            self.callbacks_status["on_command_es"].append(3)
            return 3

        # end def

        self.assertIsNotNone(
            on_command__callback1,
            "function 1 is not None => decorator returned something")
        self.assertIsNotNone(
            on_command__callback2,
            "function 2 is not None => decorator returned something")
        self.assertIsNotNone(
            on_command__callback3,
            "function 3 is not None => decorator returned something")
        listeners = [(k, v) for k, v in self.mixin.commands.items()]

        self.assertIn(('/start', (on_command__callback1, False)), listeners,
                      "1 in list => listener added")
        self.assertIn(('/start2', (on_command__callback2, False)), listeners,
                      "2 in list => listener added")
        self.assertIn(('/start23', (on_command__callback3, False)), listeners,
                      "3 in list => listener added")

        self.assertNotIn("on_command_e1", self.callbacks_status,
                         "1 no data => 1 not executed yet")
        self.assertNotIn("on_command_e2", self.callbacks_status,
                         "2 no data => 2 not executed yet")
        self.assertNotIn("on_command_e3", self.callbacks_status,
                         "3 no data => 3 not executed yet")

        self.assertListEqual(self.callbacks_status["on_command_es"], [],
                             "no data => all not executed yet")

        cmd2_update = Update.from_array(self.data_cmd_with_reply.to_array())
        cmd2_update.message.text = '/start2'
        cmd2_update.message.entities[
            0].length = cmd2_update.message.entities[0].length + 1

        print('>', str(cmd2_update.message))
        print('>', str(cmd2_update))
        print('>', str(cmd2_update.message.text))
        self.mixin.process_update(cmd2_update)

        print('c:', repr(self.callbacks_status))

        self.assertNotIn("on_command_e1", self.callbacks_status,
                         "1 has data => 1 did execute")
        self.assertIn("on_command_e2", self.callbacks_status,
                      "2 has data => 2 did execute")
        self.assertNotIn("on_command_e3", self.callbacks_status,
                         "3 has data => 3 did execute")
        self.assertEqual(self.callbacks_status["on_command_e2"], cmd2_update,
                         "2 has update => successfully called given function")
        self.assertNotIn(
            "processed_update", self.callbacks_status,
            "did not execute commands, because exception => good")
        self.assertListEqual(self.callbacks_status["on_command_es"], [2],
                             "=> successfully executed")

    # end def

    def test__remove_command_listener__by_command(self):
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

        @self.mixin.command('foobar')
        def remove_command_listener__callback(update, text):
            pass

        # end def

        commands = [(k, v) for k, v in self.mixin.commands.items()]
        self.assertIn(('/foobar', (remove_command_listener__callback, False)),
                      commands, "in list => listener added")
        self.assertIn(
            ('/foobar@UnitTest', (remove_command_listener__callback, False)),
            commands, "in list => listener added")

        self.mixin.remove_command(command='foobar')

        commands = [(k, v) for k, v in self.mixin.commands.items()]
        self.assertNotIn(
            ('/foobar', (remove_command_listener__callback, False)), commands,
            "not in list => listener removed")
        self.assertNotIn(
            ('/foobar@UnitTest', (remove_command_listener__callback, False)),
            commands, "in list => only /foobar removed")

        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => removed successfully")

    # end def

    def test__remove_command_listener__by_function(self):
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

        @self.mixin.command('foobar')
        def remove_command_listener__callback(update, text):
            pass

        # end def

        commands = [(k, v) for k, v in self.mixin.commands.items()]
        self.assertIn(('/foobar', (remove_command_listener__callback, False)),
                      commands, "in list => listener added")
        self.assertIn(
            ('/foobar@UnitTest', (remove_command_listener__callback, False)),
            commands, "in list => listener added")

        self.mixin.remove_command(function=remove_command_listener__callback)

        commands = [(k, v) for k, v in self.mixin.commands.items()]
        self.assertNotIn(
            ('/foobar', (remove_command_listener__callback, False)), commands,
            "not in list => listener removed")
        self.assertNotIn(
            ('/foobar@UnitTest', (remove_command_listener__callback, False)),
            commands, "in list => only /foobar removed")

        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => removed successfully")

    # end def

    def test__remove_nonexistent_command_listener__by_command(self):
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")
        self.mixin.remove_command(command="nothing")
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

    # end def

    def test__remove_nonexistent_command_listener__by_function(self):
        def some_unregistered_func():
            pass

        # end def
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")
        self.mixin.remove_command(command=some_unregistered_func)
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")

    # end def

    def test__remove_command_listener__by_none(self):
        with self.assertRaises(ValueError) as e:
            self.mixin.remove_command()
예제 #12
0
    def test__on_command__exception(self):
        self.assertNotIn("on_command", self.callbacks_status,
                         "no data => not executed yet")
        self.assertDictEqual(self.mixin.commands, {},
                             "empty listener list => not added yet")
        self.callbacks_status["on_command_es"] = list()
        self.assertIn("on_command_es", self.callbacks_status,
                      "just test setup, fail = broken test")
        self.assertListEqual(self.callbacks_status["on_command_es"], [],
                             "just test setup, fail = broken test")

        @self.mixin.command('start')
        def on_command__callback1(update, text):
            self.callbacks_status["on_command_e1"] = update
            self.callbacks_status["on_command_es"].append(1)
            return 1

        # end def

        @self.mixin.command('start2')
        def on_command__callback2(update, text):
            self.callbacks_status["on_command_e2"] = update
            self.callbacks_status["on_command_es"].append(2)
            raise ArithmeticError("Exception Test")

        # end def

        @self.mixin.command('start23')
        def on_command__callback3(update, text):
            self.callbacks_status["on_command_e3"] = update
            self.callbacks_status["on_command_es"].append(3)
            return 3

        # end def

        self.assertIsNotNone(
            on_command__callback1,
            "function 1 is not None => decorator returned something")
        self.assertIsNotNone(
            on_command__callback2,
            "function 2 is not None => decorator returned something")
        self.assertIsNotNone(
            on_command__callback3,
            "function 3 is not None => decorator returned something")
        listeners = [(k, v) for k, v in self.mixin.commands.items()]

        self.assertIn(('/start', (on_command__callback1, False)), listeners,
                      "1 in list => listener added")
        self.assertIn(('/start2', (on_command__callback2, False)), listeners,
                      "2 in list => listener added")
        self.assertIn(('/start23', (on_command__callback3, False)), listeners,
                      "3 in list => listener added")

        self.assertNotIn("on_command_e1", self.callbacks_status,
                         "1 no data => 1 not executed yet")
        self.assertNotIn("on_command_e2", self.callbacks_status,
                         "2 no data => 2 not executed yet")
        self.assertNotIn("on_command_e3", self.callbacks_status,
                         "3 no data => 3 not executed yet")

        self.assertListEqual(self.callbacks_status["on_command_es"], [],
                             "no data => all not executed yet")

        cmd2_update = Update.from_array(self.data_cmd_with_reply.to_array())
        cmd2_update.message.text = '/start2'
        cmd2_update.message.entities[
            0].length = cmd2_update.message.entities[0].length + 1

        print('>', str(cmd2_update.message))
        print('>', str(cmd2_update))
        print('>', str(cmd2_update.message.text))
        self.mixin.process_update(cmd2_update)

        print('c:', repr(self.callbacks_status))

        self.assertNotIn("on_command_e1", self.callbacks_status,
                         "1 has data => 1 did execute")
        self.assertIn("on_command_e2", self.callbacks_status,
                      "2 has data => 2 did execute")
        self.assertNotIn("on_command_e3", self.callbacks_status,
                         "3 has data => 3 did execute")
        self.assertEqual(self.callbacks_status["on_command_e2"], cmd2_update,
                         "2 has update => successfully called given function")
        self.assertNotIn(
            "processed_update", self.callbacks_status,
            "did not execute commands, because exception => good")
        self.assertListEqual(self.callbacks_status["on_command_es"], [2],
                             "=> successfully executed")