From a83ecfe0dad5f7660191ddbfa04bcbdf861c5b5d Mon Sep 17 00:00:00 2001 From: waylon Date: Mon, 1 May 2017 23:17:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9echo=E5=8F=91=E9=80=81=E2=80=99[?= =?UTF-8?q?=E2=80=99=E3=80=81=E2=80=99]=E2=80=99=E3=80=81=E2=80=99&?= =?UTF-8?q?=E2=80=99=E3=80=81=E2=80=99,=E2=80=99=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AE=B9=E8=BF=9B=E8=A1=8C=E8=BD=AC=E4=B9=89=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- msg_src_adapters/coolq_http_api.py | 57 ++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/msg_src_adapters/coolq_http_api.py b/msg_src_adapters/coolq_http_api.py index df7afa60..b1e4e117 100644 --- a/msg_src_adapters/coolq_http_api.py +++ b/msg_src_adapters/coolq_http_api.py @@ -1,4 +1,5 @@ import requests +import re from flask import request as flask_req from msg_src_adapter import Adapter, as_adapter, ConfigurationError @@ -29,7 +30,8 @@ def unitize_context(self, ctx_msg: dict): new_ctx['time'] = ctx_msg['time'] new_ctx['msg_type'] = ctx_msg['message_type'] new_ctx['format'] = 'text' - new_ctx['content'] = ctx_msg['message'] + new_ctx['content'] = ctx_msg['message'].replace('&', '&').replace('[', '[').replace(']', ']')\ + .replace(',', ',') login_info = self.get_login_info() new_ctx['receiver_name'] = login_info['nickname'] @@ -38,14 +40,19 @@ def unitize_context(self, ctx_msg: dict): new_ctx['sender_id'] = str(ctx_msg.get('user_id', '')) new_ctx['sender_tid'] = new_ctx['sender_id'] - json = self.session.get(self.api_url + '/get_stranger_info', - params={'user_id': new_ctx['sender_id']}).json() - if json and json.get('data'): - new_ctx['sender_name'] = json['data']['nickname'] if new_ctx['msg_type'] == 'group': new_ctx['group_id'] = str(ctx_msg.get('group_id', '')) new_ctx['group_tid'] = new_ctx['group_id'] + json = self.session.get(self.api_url + '/get_group_member_info', + params={'group_id': new_ctx['group_id'] ,'user_id': new_ctx['sender_id']}).json() + if json and json.get('data'): + new_ctx['sender_name'] = json['data']['card'] + else: + json = self.session.get(self.api_url + '/get_stranger_info', + params={'user_id': new_ctx['sender_id']}).json() + if json and json.get('data'): + new_ctx['sender_name'] = json['data']['nickname'] if new_ctx['msg_type'] == 'discuss': new_ctx['discuss_id'] = str(ctx_msg.get('discuss_id', '')) @@ -69,7 +76,7 @@ def get_login_info(self): def get_sender_group_role(self, ctx_msg: dict): json = self.session.get( self.api_url + '/get_group_member_info', - params={'group_id': ctx_msg.get('group_id'), 'user_id': ctx_msg.get('sender_id')} + params={'group_id': ctx_msg.get('group_id'), 'user_id': ctx_msg.get('sender_id'),'no_cache':True} ).json() if json and json.get('data'): return json['data']['role'] @@ -86,15 +93,51 @@ def send_private_message(self, target: dict, content: str): self.session.get(self.api_url + '/send_private_msg', params=params) def send_group_message(self, target: dict, content: str): + def repl_args(matched): + def repl_value(matched): + pre = '' + value = '' + if matched.group("pre") != None: + pre = matched.group("pre") + if matched.group("value") != None: + value = matched.group("value").replace('&','&').replace('[','[').replace(']',']').replace(',',',') + return pre + value + pre = '' + args = '' + post = '' + if matched.group("pre") != None: + pre = matched.group("pre") + if matched.group("args") != None: + args = matched.group("args") + args = re.sub(r'(?P
=)(?P[^,]*)', repl_value, args)
+            if matched.group("post") != None:
+                post = matched.group("post")
+            return pre + args + post
+
+        def repl_text(matched):
+            text = ''
+            post = ''
+            if matched.group("text") != None:
+                text = matched.group("text")
+                text = text.replace('&', '&').replace('[', '[').replace(']', ']')
+            if matched.group("post") != None:
+                post = matched.group("post")
+            return text + post
+
         params = None
         if target.get('group_id'):
             params = {'group_id': target.get('group_id')}
-
         if params:
+            # 参数中转义的字符的处理
+            content = re.sub(r'(?P
\[CQ:[^,]*,)(?P[^\]]+)(?P\])', repl_args, content)
+            # 正文中转义的字符的处理
+            content = re.sub(r'(?P[^\[]*(?:\[(?!CQ:))*)(?P(?:\[CQ:[^\]]*\])*)', repl_text, content)
+
             params['message'] = content
             params['is_raw'] = True
             self.session.get(self.api_url + '/send_group_msg', params=params)
 
+
     def send_discuss_message(self, target: dict, content: str):
         params = None
         if target.get('discuss_id'):