Esempio n. 1
0
    async def _start(self, ignore_errors: bool = True):
        options = ProcessEventOptions(do_not_handle=False)
        api_version = (await
                       self.dp.api.get_context().groups.get_long_poll_settings(
                           self.lp.data.group_id)).response.api_version

        api_version_for_check = int(api_version.split(".")[-1])
        if api_version_for_check < 103:
            warnings.warn(
                f"LongPoll API versions less than 5.103 shall not work. \nYou are using {api_version}"
            )
        if not ignore_errors:
            while True:
                events = await self.lp.get_updates()
                for event in events:
                    get_running_loop().create_task(
                        self.dp.process_event(
                            ExtensionEvent(BotType.BOT, event), options))
        else:
            while True:
                try:
                    events = await self.lp.get_updates()
                    for event in events:
                        get_running_loop().create_task(
                            self.dp.process_event(
                                ExtensionEvent(BotType.BOT, event), options))
                except Exception as e:
                    logger.error(
                        f"Error in Longpoll ({e}): {traceback.format_exc()}")
                    await sleep(0.33)
                    continue
Esempio n. 2
0
 async def _start(self):
     options = ProcessEventOptions(do_not_handle=False)
     while True:
         events = await self.lp.get_updates()
         for event in events:
             get_running_loop().create_task(
                 self.dp.process_event(ExtensionEvent(BotType.BOT, event), options)
             )
Esempio n. 3
0
    async def process_event(
        self, revent: ExtensionEvent, options: ProcessEventOptions
    ) -> ProcessingResult:
        event: BaseEvent

        logger.debug(f"ProcessEventOptions:\n{options}")
        logger.debug(f"New event! Raw:\n{revent}")

        if options.do_not_handle:
            logger.debug("ProcessEventOptions.do_not_handle is True")
            logger.debug("Event was skipped")
            return ProcessingResult(False)

        if revent.bot_type is BotType.BOT:
            revent.raw_event = cast(dict, revent.raw_event)
            group_id = revent.raw_event["group_id"]
            token = await self.token_storage.get_token(GroupId(group_id))
            event = BotEvent(get_event_object(revent.raw_event), self.api.with_token(token))
        else:
            revent.raw_event = cast(list, revent.raw_event)
            obj = user_get_event_object(revent.raw_event)

            # TODO: fix it
            user_id = obj.peer_id

            token = await self.token_storage.get_token(UserId(user_id))
            event = UserEvent(obj, self.api.with_token(token))

        logger.debug(f"New event! Formatted:\n{event}")

        if not await self.middleware_manager.execute_pre_process_event(event):
            return ProcessingResult(False)
        for router in self.routers:
            if await router.is_suitable(event):
                result = await router.process_event(event)
                if result is HANDLER_NOT_FOUND:
                    continue
                await self.result_caster.cast(result, event)
                logger.debug("Event was succesfully handled")
                return ProcessingResult(True)
        logger.debug("Event wasn't handled")
        return ProcessingResult(False)
Esempio n. 4
0
 async def _start(self, ignore_errors: bool = True):
     options = ProcessEventOptions(do_not_handle=False)
     if not ignore_errors:
         while True:
             events = await self.lp.get_updates()
             for event in events:
                 get_running_loop().create_task(
                     self.dp.process_event(ExtensionEvent(BotType.USER, event), options)
                 )
     else:
         while True:
             try:
                 events = await self.lp.get_updates()
                 for event in events:
                     get_running_loop().create_task(
                         self.dp.process_event(ExtensionEvent(BotType.USER, event), options)
                     )
             except Exception as e:
                 logger.error(f"Error in Longpoll ({e}): {traceback.format_exc()}")
                 continue
Esempio n. 5
0
    async def post(self):
        event: dict = await self.request.json()
        e_type = event.get("type")
        if not e_type:
            raise web.HTTPForbidden()
        group_id = event["group_id"]

        if e_type == "confirmation":
            return web.Response(
                body=await self.request.app["storage"].get_confirmation(
                    GroupId(group_id)))

        if self.request.app["support_secret"]:
            if not event["secret"] == self.request.app["secret"]:
                raise web.HTTPForbidden()

        options = ProcessEventOptions(do_not_handle=False)
        revent = ExtensionEvent(BotType.BOT, event)

        get_running_loop().create_task(self.request.app["dp"].process_event(
            revent, options))

        return web.Response(body="ok")