Skip to content

Commit

Permalink
对echo发送’[’、’]’、’&’、’,’的内容进行转义处理
Browse files Browse the repository at this point in the history
  • Loading branch information
waylonwang committed May 1, 2017
1 parent a753dbd commit a83ecfe
Showing 1 changed file with 50 additions and 7 deletions.
57 changes: 50 additions & 7 deletions msg_src_adapters/coolq_http_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import re
from flask import request as flask_req

from msg_src_adapter import Adapter, as_adapter, ConfigurationError
Expand Down Expand Up @@ -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']
Expand All @@ -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', ''))
Expand All @@ -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']
Expand All @@ -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<pre>=)(?P<value>[^,]*)', 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('&', '&amp;').replace('[', '&#91;').replace(']', '&#93;')
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<pre>\[CQ:[^,]*,)(?P<args>[^\]]+)(?P<post>\])', repl_args, content)
# 正文中转义的字符的处理
content = re.sub(r'(?P<text>[^\[]*(?:\[(?!CQ:))*)(?P<post>(?:\[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'):
Expand Down

0 comments on commit a83ecfe

Please sign in to comment.