async def emulate(self, event: dict) -> typing.Union[dict, str]: """ Process all signals from IRIS CM. :param event: New signal :return: "ok" (if possible) """ logger.debug("Event: {event}", event=event) if event is None: return {"response": "error", "error_code": NO_DATA} if event.get("secret") != self.secret: return {"response": "error", "error_code": INVALID_DATA} if event.get("user_id") != self.user_id: return {"response": "error", "error_code": INVALID_DATA} try: task = (await self._processor(event)) if task is not None: return task except Exception as e: processing = await self.error_processor(e, event) if processing is False: logger.error(traceback.format_exc(limit=5)) return traceback.format_exc(limit=5) if processing is not None: return processing return {"response": "ok"}
def set_blueprints(self, *blueprints: Blueprint): for bp in blueprints: bp.create(self.api, self.user_id) self.event.concatenate(bp.event) self.error_handler.update(bp.error_handler.processors) self.on.concatenate(bp.on) logger.debug("Blueprints have been successfully loaded")
async def emulate(self, event: dict) -> typing.Union[dict, str]: """ Process all signals from IRIS CM. :param event: New signal :return: "ok" (if possible) """ logger.debug("Event: {event}", event=event) if event is None: return errors[1] if event.get("secret") != self._secret: return errors[3] if event.get("user_id") != self._user_id: return errors[3] ev = await self.get_event_type(event) task = None try: task = (await self._processor(ev, self._patcher)) except (VKError, Exception): logger.exception(traceback.format_exc()) if task is not None: return {"response": task} return {"response": "ok"}
def update_fields(item: str, value: Any): default_data[item] = value try: with open(SETTINGS_PATH, mode="w") as file: file.write(json.dumps(default_data, indent=2)) logger.debug("{} == {}", item, value) except FileNotFoundError: return recreate()
async def delete_messages(peer_id: int, local_ids: list, spam: int): local_ids = ",".join(list(map(str, local_ids))) code = """return API.messages.delete({ "delete_for_all": 1, "spam": %s, "message_ids": API.messages.getByConversationMessageId({ "peer_id": %s, "conversation_message_ids": [%s] })[email protected] });""" % (spam, peer_id, local_ids) logger.debug("Formatting result:\n{}", code) return await execute(code)
async def msg_edit(peer_id: int, message: str, local_id: int, attachment: str = ""): """ Two methods in one request. messages.edit + messages.getByConversationMessageId """ text = message.replace("\n", "\\n") code = """return API.messages.edit({ "peer_id": %s, "message": "%s", "attachment": "%s", "message_id": API.messages.getByConversationMessageId({ "peer_id": %s, "conversation_message_ids": %s })[email protected] });""" % (peer_id, text, attachment, peer_id, local_id) logger.debug("Formatting result:\n{}", code) return await execute(code)
async def get_chat(date: int, q: str = "!связать"): code = """var a = 0; var chat_id; var title; var data = API.messages.search({ "q": "%s", "count": 5 }).items; while (a < data.length) { if (data[a].date == %s) { chat_id = data[a].peer_id - 2000000000; title = API.messages.getChat({"chat_id": chat_id}).title; API.messages.edit({ "peer_id": data[a].peer_id, "message": "✅ Беседа " + "«" + title + "»" + " распознана!", "message_id": data[a].id }); return [data[a].peer_id, title]; } a = a + 1; }""" % (q, date) logger.debug("Formatting result:\n{}", code) return await execute(code)
def set_web_blueprints(self, *blueprints: WebBlueprint): for blueprint in blueprints: blueprint.create(self.user_id, self.secret) logger.debug("Web-Blueprints have been successfully loaded")
def set_blueprints(self, *blueprints: Blueprint): for blueprint in blueprints: blueprint.api = self.api self.on.concatenate(blueprint.on) logger.debug("Blueprints have been successfully loaded")
async def execute(code: str): logger.debug("Formatting result:\n{}", code) return await bot.api.request("execute", {"code": code})
async def notify(self, error: Exception, event: dict, ignore: bool = True): name = Exception if ignore else error.__class__ handler = self.processors[name] logger.debug("{}! Processing it with handler <{}>", error.__class__.__name__, handler.__name__) return await handler(error, event)
def start(self): for k, v in self.metadata.items(): if workers_state[k]: self.loop.create_task(v()) logger.debug("Workers have been dispatched.")