def as_cq_el(self, message: MessageElement) -> cq_message.MessageSegment: if isinstance(message, Raw): return cq_message.MessageSegment(type_=message.type, data=message.data) elif isinstance(message, raw_message.Plain): return cq_message.MessageSegment.text(message.text) elif isinstance(message, raw_message.At): return cq_message.MessageSegment.at(message.target) elif isinstance(message, raw_message.AtAll): return cq_message.MessageSegment(type_='at', data={'qq': 'all'}) elif isinstance(message, raw_message.Face): return cq_message.MessageSegment.face(message.id) elif isinstance(message, Image) and message.file: return cq_message.MessageSegment.image(message.file) elif isinstance(message, raw_message.Image): return cq_message.MessageSegment.image(message.url) elif isinstance(message, Voice): return cq_message.MessageSegment.record(message.file) elif isinstance(message, raw_message.Voice): return cq_message.MessageSegment.record(message.url) elif isinstance(message, raw_message.App): return cq_message.MessageSegment(type_='json', data={ 'data': cq_message.escape((json.dumps( message.content))) }) elif isinstance(message, raw_message.Xml): return cq_message.MessageSegment( type_='xml', data={'data': cq_message.escape(message.content)}) else: logger.debug(f'Unknown message {message} of type {type(message)}') return cq_message.MessageSegment.text('')
def as_message_el(self, message: aiocqhttp.MessageSegment) -> MessageElement: type_, data = message.type, message.data if type_ == 'text': ret = raw_message.Plain(data['text']) elif type_ == 'face': ret = raw_message.Face(int(data['id'])) elif type_ == 'image': ret = Image(url=data['url'], file=data['file']) elif type_ == 'at': if data['qq'] == 'all': ret = raw_message.AtAll() else: ret = raw_message.At(int(data['qq'])) elif type_ == 'record': ret = Voice(file=data['file']) elif type_ == 'rich': ret = raw_message.App( content=json.loads(cq_message.unescape(data['content']))) elif type_ == 'reply': ret = Quote(id=int(data['id'])) elif type_ == 'json': ret = raw_message.App( content=json.loads(cq_message.unescape(data['data']))) elif type_ == 'xml': ret = raw_message.Xml(content=cq_message.unescape(data['data'])) else: logger.debug(f'Unknown message {message} of type {type_}') ret = raw_message.Unknown() ret.referer = self.qq return ret
async def handle_event(source: EventProvider, event: Event, **kwargs): """Handle a event received from protocol To avoid blocking awaiting, creating a task to run this :param source: :param event: :return: """ if not isinstance(event, Event): logger.error(f'A non-event {event} passed to handle_event!') return logger.debug(f'Handling {event.type} {event}') res = [] async for result in engine.forward(event=event, source=source, **kwargs): if isinstance(result, Exception): try: raise result except Exception as e: logger.error(f'Error handling event {event} {e}') logger.exception(e) elif result: res.append(result) return res
def _as_cq_forward_nodes(self, message: raw_message.Forward) -> List: """ Go-CQHTTP Forward Message Support Experimental """ ret = [] for node in message.content: if node.id: ret.append({ 'type': 'node', 'data': { 'id': node.id, }, }) elif node.qq: ret.append({ 'type': 'node', 'data': { 'name': node.name, 'uin': node.qq, 'content': self.as_cq_chain(node.content), }, }) logger.debug(repr(ret)) return ret
async def _ws(): async with aiohttp.ClientSession() as client: async with client.ws_connect( f"{self._api_root}all?sessionKey={self._session_key}", **kwargs) as ws: while True: try: event = await ws.receive_json() except TypeError: if ws.closed: logger.warning( 'Websocket closed, try relogin in 10s') await asyncio.sleep(10) await self.relogin() return logger.error(f"TypeError in parsing ws event") continue if not event: continue logger.debug(f"[event] {event}") try: event = self.as_event(event) if event: self.handle_event_nowait(event) except Exception as e: logger.critical(e) return
def as_message_el(self, msg: dict) -> MessageElement: # print(msg) type_: str = msg['type'] if type_ == 'Source': ret = Source(id=msg['id']) elif type_ == 'Plain': ret = raw_message.Plain(text=msg['text']) elif type_ == 'Quote': ret = Quote(id=msg['id'], origin=self.as_message_chain(msg['origin'])) elif type_ == 'At': ret = raw_message.At(msg['target']) elif type_ == 'AtAll': ret = raw_message.AtAll() elif type_ == 'Face': ret = raw_message.Face(msg['faceId']) elif type_ == 'Image': ret = Image(url=msg['url'], id=msg['imageId']) elif type_ == 'Voice': ret = Voice(url=msg['url'], id=msg['voiceId']) elif type_ == 'App': ret = raw_message.App(content=json.loads(msg['content'])) elif type_ == 'Xml': ret = raw_message.Xml(content=msg['xml']) elif type_ == 'File': ret = File(name=msg['name'], id=msg['id']) else: logger.debug(f'Unknown message {msg} of type {type_}') ret = raw_message.Unknown() ret.referer = self.qq return ret
def as_mirai_el(self, msg: MessageElement) -> dict: if isinstance(msg, raw_message.Plain): return { 'type': 'Plain', 'text': msg.text, } elif isinstance(msg, raw_message.At): return { 'type': 'At', 'target': msg.target, } elif isinstance(msg, raw_message.AtAll): return { 'type': 'AtAll', } elif isinstance(msg, Image): return { 'type': 'Image', 'imageId': msg.id, } elif isinstance(msg, raw_message.Face): return { 'type': 'Face', 'faceId': msg.id, } elif isinstance(msg, Voice): return { 'type': 'Voice', 'voiceId': msg.id, # 'json': msg.json, } elif isinstance(msg, raw_message.Voice): return { 'type': 'Voice', 'url': msg.url, } elif isinstance(msg, raw_message.App): return { 'type': 'App', 'content': json.dumps(msg.content), } elif isinstance(msg, raw_message.Xml): return { 'type': 'Xml', 'xml': msg.content, } elif isinstance(msg, File): return { 'type': 'File', 'id': msg.id, } else: logger.debug(f'Unknown message {msg} of type {type(msg)}') return {}
async def _on_event(event: aiocqhttp.Event): session: CQSession = get_session(event.self_id) if not session or not isinstance(session, CQSession): return logger.debug(f"[event] {event}") # For CQ Compact: session.handle_event_nowait( RawCQEvent(protocol="cqhttp", event=event)) event = session.as_event(event) if event: session.handle_event_nowait(event)
async def _prepare(self): if not self.content: logger.debug(f'Fetching image from url = {self.url}') url = urlparse(self.url) if url.scheme == 'file': async with aiofiles.open(url2pathname(url.path), 'rb') as f: self.set_content(await f.read()) else: async with aiohttp.request("GET", self.url) as resp: self.set_content(await resp.content.read()) bot: MiraiSession = app.get_session(self.referer) img = await bot._upload_image(self.method, self.content, f'{self.hash}.png') self.id = img.id