From d8255f039b9f62d6baeb09c6ceff7dc2425cec67 Mon Sep 17 00:00:00 2001 From: Geyang Xie Date: Tue, 2 Jan 2018 10:28:58 -0600 Subject: [PATCH 1/3] turned off auto reply to group chart --- wxbot_demo_py3/weixin.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/wxbot_demo_py3/weixin.py b/wxbot_demo_py3/weixin.py index 09c438c..683d9fa 100755 --- a/wxbot_demo_py3/weixin.py +++ b/wxbot_demo_py3/weixin.py @@ -108,7 +108,7 @@ def __init__(self): self.autoReplyMode = False self.syncHost = '' self.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36' - self.interactive = False + self.interactive = True self.autoOpen = False self.saveFolder = os.path.join(os.getcwd(), 'saved') self.saveSubFolders = {'webwxgeticon': 'icons', 'webwxgetheadimg': 'headimgs', 'webwxgetmsgimg': 'msgimgs', @@ -798,13 +798,14 @@ def handleMsg(self, r): # store #自己加的代码-------------------------------------------# if self.autoReplyMode: - ans = self._xiaodoubi(content) + '\n[微信机器人自动回复]' - if self.webwxsendmsg(ans, msg['FromUserName']): - print('自动回复: ' + ans) - logging.info('自动回复: ' + ans) - else: - print('自动回复失败') - logging.info('自动回复失败') + if msg['FromUserName'][:2] != '@@': #if it is not group chart + ans = self._simsimi(content)+ '\n[微信机器人自动回复]' + if self.webwxsendmsg(ans, msg['FromUserName']): + print('自动回复: ' + ans) + logging.info('自动回复: ' + ans) + else: + print('自动回复失败') + logging.info('自动回复失败') elif msgType == 3: image = self.webwxgetmsgimg(msgid) raw_msg = {'raw_msg': msg, @@ -1005,6 +1006,7 @@ def start(self): print('[*] 自动回复模式 ... 开启') logging.debug('[*] 自动回复模式 ... 开启') else: + self.autoReplyMode = False print('[*] 自动回复模式 ... 关闭') logging.debug('[*] 自动回复模式 ... 关闭') @@ -1128,7 +1130,6 @@ def _get(self, url: object, api: object = None, timeout: object = None) -> objec def _post(self, url: object, params: object, jsonfmt: object = True) -> object: if jsonfmt: data = (json.dumps(params)).encode() - request = urllib.request.Request(url=url, data=data) request.add_header( 'ContentType', 'application/json; charset=UTF-8') @@ -1163,12 +1164,13 @@ def _xiaodoubi(self, word): return "让我一个人静静 T_T..." def _simsimi(self, word): - key = '' + key = "a7d7deb5-18a6-4260-98e3-7fd803dd495b" url = 'http://sandbox.api.simsimi.com/request.p?key=%s&lc=ch&ft=0.0&text=%s' % ( key, word) r = requests.get(url) - ans = r.json() - if ans['result'] == '100': + text = r.text + ans = json.loads(text) + if ans['result'] == 100: return ans['response'] else: return '你在说什么,风太大听不清列' From fe5e01d27f433ba7132725e6d9a69b34cb3d5adc Mon Sep 17 00:00:00 2001 From: Geyang Xie Date: Tue, 2 Jan 2018 13:50:49 -0600 Subject: [PATCH 2/3] add qingyunke robot api --- wxbot_demo_py3/weixin.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/wxbot_demo_py3/weixin.py b/wxbot_demo_py3/weixin.py index 683d9fa..72be820 100755 --- a/wxbot_demo_py3/weixin.py +++ b/wxbot_demo_py3/weixin.py @@ -799,7 +799,9 @@ def handleMsg(self, r): #自己加的代码-------------------------------------------# if self.autoReplyMode: if msg['FromUserName'][:2] != '@@': #if it is not group chart - ans = self._simsimi(content)+ '\n[微信机器人自动回复]' + ans = self._simsimi(content) + ans = self._qingyunke(content) + ans += '\n[我人不在,机器人回复-_-!]' if self.webwxsendmsg(ans, msg['FromUserName']): print('自动回复: ' + ans) logging.info('自动回复: ' + ans) @@ -1163,6 +1165,18 @@ def _xiaodoubi(self, word): except: return "让我一个人静静 T_T..." + def _qingyunke(self, word): + key = "free" + url = 'http://api.qingyunke.com/api.php?key=%s&appid=0&msg=%s' % ( + key, word) + r = requests.get(url) + text = r.text + ans = json.loads(text) + if ans['result'] == 0: + return ans['content'] + else: + return '让我一个人静静 T_T...' + def _simsimi(self, word): key = "a7d7deb5-18a6-4260-98e3-7fd803dd495b" url = 'http://sandbox.api.simsimi.com/request.p?key=%s&lc=ch&ft=0.0&text=%s' % ( From 0f8e3d4c8fd1ea2852dff466722bf873602c2ae1 Mon Sep 17 00:00:00 2001 From: Geyang Xie Date: Tue, 2 Jan 2018 13:51:04 -0600 Subject: [PATCH 3/3] add comment --- wxbot_demo_py3/weixin.py | 1 + 1 file changed, 1 insertion(+) diff --git a/wxbot_demo_py3/weixin.py b/wxbot_demo_py3/weixin.py index 72be820..bc7cc76 100755 --- a/wxbot_demo_py3/weixin.py +++ b/wxbot_demo_py3/weixin.py @@ -800,6 +800,7 @@ def handleMsg(self, r): if self.autoReplyMode: if msg['FromUserName'][:2] != '@@': #if it is not group chart ans = self._simsimi(content) + if ans == '你在说什么,风太大听不清列': #if the simsimi api does not resonse ans = self._qingyunke(content) ans += '\n[我人不在,机器人回复-_-!]' if self.webwxsendmsg(ans, msg['FromUserName']):