Exemplo n.º 1
0
 def get_id_by_token(token: str, loop: asyncio.AbstractEventLoop) -> int:
     """
     Get group id from token
     :param token:
     :return:
     """
     logger.debug("Making API request users.get to get user_id")
     response = loop.run_until_complete(request("users.get", {}, token))
     if "error" in response:
         raise VKError(0, "Token is invalid")
     return response["response"][0]["id"]
Exemplo n.º 2
0
 def get_id_by_token(token: str, throw_exc: bool = True) -> typing.Union[int, bool]:
     """
     Get group id from token
     :param token:
     :param throw_exc:
     :return:
     """
     logger.debug("Making API request groups.getById to get group_id")
     response = asyncio.get_event_loop().run_until_complete(
         request("groups.getById", {}, token, throw_errors=False)
     )
     if "error" in response:
         if throw_exc:
             raise VKError(0, "Token is invalid")
         return False
     return response["response"][0]["id"]
Exemplo n.º 3
0
    async def emulate(
        self, event: dict, confirmation_token: str = None, secret: str = None,
    ) -> typing.Union[str, None]:
        """
        Process all types of events
        :param event: VK Event (LP or CB)
        :param confirmation_token: code which confirm VK callback
        :param secret: secret key for callback
        :return: "ok"
        """
        if not self.status.dispatched:
            self.middleware.add_middleware(self.on.pre)
            await self.on.dispatch(self.get_current_rest)
            self.status.dispatched = True

        logger.debug("Event: {event}", event=event)

        if event is None:
            return

        if event.get("type") == "confirmation":
            if event.get("group_id") == self.group_id:
                return confirmation_token or "dissatisfied"

        updates = event.get("updates", [event])
        if not self._check_secret(event, secret=secret):
            logger.debug("Aborted. Secret is invalid")
            return "access denied"

        for update in updates:
            try:
                if not update.get("object"):
                    continue

                obj = update["object"]

                if update["type"] == EventList.MESSAGE_NEW:

                    # VK API v5.103
                    client_info = obj.get("client_info")
                    if client_info is None:
                        raise VKError(
                            0, "Change API version to 5.103 or later"
                        ) from None
                    obj = obj["message"]
                    await self._processor(obj, client_info)

                else:
                    await (
                        self._event_processor(obj=obj, event_type=update["type"])
                    )  # noqa

            except VKError as e:
                await self.error_handler.handle_error(e)

            except:
                logger.error(
                    "While event was emulating error occurred \n\n{traceback}",
                    traceback=traceback.format_exc(),
                )

        return "ok"