|
| 1 | +import typing |
| 2 | +import quart |
| 3 | + |
| 4 | + |
| 5 | +import traceback |
| 6 | +import typing |
| 7 | +import asyncio |
| 8 | +import re |
| 9 | +import base64 |
| 10 | +import uuid |
| 11 | +import json |
| 12 | +import datetime |
| 13 | +import hashlib |
| 14 | +from Crypto.Cipher import AES |
| 15 | + |
| 16 | + |
| 17 | +from ...core import app |
| 18 | +import langbot_plugin.api.definition.abstract.platform.adapter as abstract_platform_adapter |
| 19 | +import langbot_plugin.api.entities.builtin.platform.message as platform_message |
| 20 | +import langbot_plugin.api.entities.builtin.platform.events as platform_events |
| 21 | +import langbot_plugin.api.entities.builtin.platform.entities as platform_entities |
| 22 | +from ..logger import EventLogger |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +from linebot.v3 import ( |
| 27 | + WebhookHandler |
| 28 | +) |
| 29 | +from linebot.v3.exceptions import ( |
| 30 | + InvalidSignatureError |
| 31 | +) |
| 32 | +from linebot.v3.messaging import ( |
| 33 | + Configuration, |
| 34 | + ApiClient, |
| 35 | + MessagingApi, |
| 36 | + ReplyMessageRequest, |
| 37 | + TextMessage, |
| 38 | + ImageMessage |
| 39 | +) |
| 40 | +from linebot.v3.webhooks import ( |
| 41 | + MessageEvent, |
| 42 | + TextMessageContent, |
| 43 | + ImageMessageContent, |
| 44 | + VideoMessageContent, |
| 45 | + AudioMessageContent, |
| 46 | + FileMessageContent, |
| 47 | + LocationMessageContent, |
| 48 | + StickerMessageContent |
| 49 | +) |
| 50 | + |
| 51 | +# from linebot import WebhookParser |
| 52 | +from linebot.v3.webhook import WebhookParser |
| 53 | +from linebot.v3.messaging import MessagingApiBlob |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +class LINEMessageConverter(abstract_platform_adapter.AbstractMessageConverter): |
| 58 | + @staticmethod |
| 59 | + async def yiri2target( |
| 60 | + message_chain: platform_message.MessageChain, api_client: ApiClient |
| 61 | + ) -> typing.Tuple[list]: |
| 62 | + content_list = [] |
| 63 | + for component in message_chain: |
| 64 | + if isinstance(component, platform_message.At): |
| 65 | + content_list.append({'type': 'at', 'target': component.target}) |
| 66 | + elif isinstance(component, platform_message.Plain): |
| 67 | + content_list.append({'type': 'text', 'content': component.text}) |
| 68 | + elif isinstance(component, platform_message.Image): |
| 69 | + if not component.url: |
| 70 | + pass |
| 71 | + content_list.append({'type': 'image', 'image': component.url}) |
| 72 | + |
| 73 | + elif isinstance(component, platform_message.Voice): |
| 74 | + content_list.append({'type': 'voice', 'url': component.url, 'length': component.length}) |
| 75 | + |
| 76 | + |
| 77 | + return content_list |
| 78 | + |
| 79 | + @staticmethod |
| 80 | + async def target2yiri( |
| 81 | + message, |
| 82 | + bot_client |
| 83 | + ) -> platform_message.MessageChain: |
| 84 | + lb_msg_list = [] |
| 85 | + msg_create_time = datetime.datetime.fromtimestamp(int(message.timestamp) / 1000) |
| 86 | + |
| 87 | + lb_msg_list.append(platform_message.Source(id=message.webhook_event_id, time=msg_create_time)) |
| 88 | + |
| 89 | + if isinstance(message.message, TextMessageContent): |
| 90 | + lb_msg_list.append(platform_message.Plain(text=message.message.text)) |
| 91 | + elif isinstance(message.message, AudioMessageContent): |
| 92 | + pass |
| 93 | + elif isinstance(message.message, VideoMessageContent): |
| 94 | + pass |
| 95 | + elif isinstance(message.message, ImageMessageContent): |
| 96 | + message_content = MessagingApiBlob(bot_client).get_message_content(message.message.id) |
| 97 | + |
| 98 | + base64_string = base64.b64encode(message_content).decode('utf-8') |
| 99 | + |
| 100 | + # 如果需要Data URI格式(用于直接嵌入HTML等) |
| 101 | + # 首先需要知道图片类型,LINE图片通常是JPEG |
| 102 | + data_uri = f"data:image/jpeg;base64,{base64_string}" |
| 103 | + lb_msg_list.append(platform_message.Image(base64 = data_uri)) |
| 104 | + return platform_message.MessageChain(lb_msg_list) |
| 105 | + |
| 106 | + |
| 107 | +class LINEEventConverter(abstract_platform_adapter.AbstractEventConverter): |
| 108 | + @staticmethod |
| 109 | + async def yiri2target( |
| 110 | + event: platform_events.MessageEvent, |
| 111 | + ) -> MessageEvent: |
| 112 | + pass |
| 113 | + |
| 114 | + @staticmethod |
| 115 | + async def target2yiri( |
| 116 | + event, |
| 117 | + bot_client |
| 118 | + ) -> platform_events.Event: |
| 119 | + message_chain = await LINEMessageConverter.target2yiri(event, bot_client) |
| 120 | + |
| 121 | + if event.source.type== 'user': |
| 122 | + return platform_events.FriendMessage( |
| 123 | + sender=platform_entities.Friend( |
| 124 | + id=event.message.id, |
| 125 | + nickname=event.source.user_id, |
| 126 | + remark='', |
| 127 | + ), |
| 128 | + message_chain=message_chain, |
| 129 | + time=event.timestamp, |
| 130 | + source_platform_object=event, |
| 131 | + ) |
| 132 | + else: |
| 133 | + return platform_events.GroupMessage( |
| 134 | + sender=platform_entities.GroupMember( |
| 135 | + id=event.event.sender.sender_id.open_id, |
| 136 | + member_name=event.event.sender.sender_id.union_id, |
| 137 | + permission=platform_entities.Permission.Member, |
| 138 | + group=platform_entities.Group( |
| 139 | + id=event.message.id, |
| 140 | + name='', |
| 141 | + permission=platform_entities.Permission.Member, |
| 142 | + ), |
| 143 | + special_title='', |
| 144 | + join_timestamp=0, |
| 145 | + last_speak_timestamp=0, |
| 146 | + mute_time_remaining=0, |
| 147 | + ), |
| 148 | + message_chain=message_chain, |
| 149 | + time=event.timestamp, |
| 150 | + source_platform_object=event, |
| 151 | + ) |
| 152 | + |
| 153 | +class LINEAdapter(abstract_platform_adapter.AbstractMessagePlatformAdapter): |
| 154 | + bot: MessagingApi |
| 155 | + api_client: ApiClient |
| 156 | + |
| 157 | + bot_account_id: str # 用于在流水线中识别at是否是本bot,直接以bot_name作为标识 |
| 158 | + message_converter: LINEMessageConverter |
| 159 | + event_converter: LINEEventConverter |
| 160 | + |
| 161 | + listeners: typing.Dict[ |
| 162 | + typing.Type[platform_events.Event], |
| 163 | + typing.Callable[[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None], |
| 164 | + ] |
| 165 | + |
| 166 | + config: dict |
| 167 | + quart_app: quart.Quart |
| 168 | + |
| 169 | + |
| 170 | + card_id_dict: dict[str, str] # 消息id到卡片id的映射,便于创建卡片后的发送消息到指定卡片 |
| 171 | + |
| 172 | + seq: int # 用于在发送卡片消息中识别消息顺序,直接以seq作为标识 |
| 173 | + |
| 174 | + def __init__(self, config: dict, logger: EventLogger): |
| 175 | + configuration = Configuration(access_token=config['channel_access_token']) |
| 176 | + line_webhook = WebhookHandler(config['channel_secret']) |
| 177 | + parser = WebhookParser(config['channel_secret']) |
| 178 | + api_client = ApiClient(configuration) |
| 179 | + |
| 180 | + bot_account_id = config.get('bot_account_id', 'langbot') |
| 181 | + |
| 182 | + |
| 183 | + super().__init__( |
| 184 | + config = config, |
| 185 | + logger = logger, |
| 186 | + quart_app = quart.Quart(__name__), |
| 187 | + listeners = {}, |
| 188 | + card_id_dict = {}, |
| 189 | + seq = 1, |
| 190 | + event_converter = LINEEventConverter(), |
| 191 | + message_converter = LINEMessageConverter(), |
| 192 | + line_webhook = line_webhook, |
| 193 | + parser = parser, |
| 194 | + configuration=configuration, |
| 195 | + api_client = api_client, |
| 196 | + bot = MessagingApi(api_client), |
| 197 | + bot_account_id = bot_account_id, |
| 198 | + ) |
| 199 | + |
| 200 | + @self.quart_app.route('/line/callback', methods=['POST']) |
| 201 | + async def line_callback(): |
| 202 | + try: |
| 203 | + signature = quart.request.headers.get('X-Line-Signature') |
| 204 | + body = await quart.request.get_data(as_text=True) |
| 205 | + events = parser.parse(body, signature) # 解密解析消息 |
| 206 | + |
| 207 | + try: |
| 208 | + |
| 209 | + # print(events) |
| 210 | + lb_event = await self.event_converter.target2yiri(events[0], self.api_client) |
| 211 | + if lb_event.__class__ in self.listeners: |
| 212 | + await self.listeners[lb_event.__class__](lb_event, self) |
| 213 | + except InvalidSignatureError: |
| 214 | + self.logger.info(f"Invalid signature. Please check your channel access token/channel secret.{traceback.format_exc()}") |
| 215 | + return quart.Response('Invalid signature', status=400) |
| 216 | + |
| 217 | + |
| 218 | + return {'code': 200, 'message': 'ok'} |
| 219 | + except Exception: |
| 220 | + await self.logger.error(f'Error in LINE callback: {traceback.format_exc()}') |
| 221 | + return {'code': 500, 'message': 'error'} |
| 222 | + |
| 223 | + |
| 224 | + |
| 225 | + |
| 226 | + |
| 227 | + |
| 228 | + async def send_message(self, target_type: str, target_id: str, message: platform_message.MessageChain): |
| 229 | + |
| 230 | + pass |
| 231 | + |
| 232 | + async def reply_message( |
| 233 | + self, |
| 234 | + message_source: platform_events.MessageEvent, |
| 235 | + message: platform_message.MessageChain, |
| 236 | + quote_origin: bool = False, |
| 237 | + ): |
| 238 | + content_list = await self.message_converter.yiri2target(message, self.api_client) |
| 239 | + |
| 240 | + for content in content_list: |
| 241 | + if content['type'] == 'text': |
| 242 | + self.bot.reply_message_with_http_info( |
| 243 | + ReplyMessageRequest( |
| 244 | + reply_token=message_source.source_platform_object.reply_token, |
| 245 | + messages=[TextMessage(text=content['content'])] |
| 246 | + ) |
| 247 | + ) |
| 248 | + elif content['type'] == 'image': |
| 249 | + self.bot.reply_message_with_http_info( |
| 250 | + ReplyMessageRequest( |
| 251 | + reply_token=message_source.source_platform_object.reply_token, |
| 252 | + messages=[ImageMessage(text=content['content'])] |
| 253 | + ) |
| 254 | + ) |
| 255 | + |
| 256 | + async def is_muted(self, group_id: int) -> bool: |
| 257 | + return False |
| 258 | + |
| 259 | + def register_listener( |
| 260 | + self, |
| 261 | + event_type: typing.Type[platform_events.Event], |
| 262 | + callback: typing.Callable[[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None], |
| 263 | + ): |
| 264 | + self.listeners[event_type] = callback |
| 265 | + |
| 266 | + def unregister_listener( |
| 267 | + self, |
| 268 | + event_type: typing.Type[platform_events.Event], |
| 269 | + callback: typing.Callable[[platform_events.Event, abstract_platform_adapter.AbstractMessagePlatformAdapter], None], |
| 270 | + ): |
| 271 | + self.listeners.pop(event_type) |
| 272 | + |
| 273 | + async def run_async(self): |
| 274 | + port = self.config['port'] |
| 275 | + |
| 276 | + async def shutdown_trigger_placeholder(): |
| 277 | + while True: |
| 278 | + await asyncio.sleep(1) |
| 279 | + await self.quart_app.run_task( |
| 280 | + host='0.0.0.0', |
| 281 | + port=port, |
| 282 | + shutdown_trigger=shutdown_trigger_placeholder, |
| 283 | + ) |
| 284 | + |
| 285 | + async def kill(self) -> bool: |
| 286 | + pass |
0 commit comments