Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #245 solved #246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions wxbot_demo_py3/weixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -798,13 +798,17 @@ 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)
if ans == '你在说什么,风太大听不清列': #if the simsimi api does not resonse
ans = self._qingyunke(content)
ans += '\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,
Expand Down Expand Up @@ -1005,6 +1009,7 @@ def start(self):
print('[*] 自动回复模式 ... 开启')
logging.debug('[*] 自动回复模式 ... 开启')
else:
self.autoReplyMode = False
print('[*] 自动回复模式 ... 关闭')
logging.debug('[*] 自动回复模式 ... 关闭')

Expand Down Expand Up @@ -1128,7 +1133,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')
Expand Down Expand Up @@ -1162,13 +1166,26 @@ 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 = ''
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 '你在说什么,风太大听不清列'
Expand Down