-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wechat_auto_reply.py
150 lines (122 loc) · 4.34 KB
/
Wechat_auto_reply.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import time
import string
import random
# import re
import hashlib
from urllib import parse
from datetime import datetime
# from collections import OrderedDict
import platform
import itchat
from itchat.content import (
NOTE,
TEXT,
FRIENDS
)
from apscheduler.schedulers.blocking import BlockingScheduler
import requests
IS_OPEN_AUTO_REPLY = True
NLPCHAT_APP_ID = '2120067869'
NLPCHAT_APP_KEY = 'zLjTT94IhjYzO11v'
NLPCHAT_URL = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_textchat'
MSG_SUFFIX = " ——auto reply by MaguaAI"
LONG_TEXT = string.ascii_letters + string.digits + string.punctuation
wechat_nick_name = ''
HEART_BEAT_INTERVAL_MINUTES = 15
def init_info():
global wechat_nick_name
global IS_OPEN_EMAIL_NOTICE
wechat_nick_name = itchat.search_friends()['NickName'] # 获取此微信号的昵称
set_note('微信号『{}』登录成功!'.format(wechat_nick_name))
print('项目初始化已完成...开始正常工作。')
print('-' * 50)
@itchat.msg_register([TEXT])
def deal_with_msg(msg):
text = msg["Text"] # 获取好友发送的话
userid = msg['FromUserName'] # 获取好友的 uid
nickname = msg['User']['NickName']
if IS_OPEN_AUTO_REPLY: # 是否已开启 AI 自动回复
reply_text = get_nlp_textchat(text, userid)
reply_text = reply_text if reply_text else ''
reply_text = reply_text + MSG_SUFFIX
else:
reply_text = ''
itchat.send(reply_text, userid)
note = '\n{}发送来的:{}\n自动回复:{}'.format(nickname, text, reply_text)
set_note(note)
def is_online():
try:
if itchat.search_friends():
return True
except IndexError:
return False
return True
def heart_beat():
if is_online():
time.sleep(random.randint(1, 100))
time_ = datetime.now().strftime('%Y-%m-%d %H:%M:%S ')
d = ''.join(random.sample(LONG_TEXT, random.randint(10, 20)))
note = "定时心跳...{}-{}".format(time_, d)
set_note(note)
else:
exit_callback()
def exit_callback():
time_ = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
title = '您服务器上的微信「{}」已离线'.format(wechat_nick_name)
content = '离线时间:{} \n 离线原因:未知'.format(time_)
# send_mail(title, content)
set_note(title + content, True)
stop_scheduler()
stop_system()
def set_note(note, onle_log=False):
if not onle_log:
itchat.send(note, 'filehelper')
print(note)
def stop_scheduler():
""" 关闭定时器 """
if scheduler and scheduler.get_jobs():
scheduler.shutdown(wait=False)
def stop_system():
"""退出应用"""
exit(1)
def get_nlp_textchat(text, userId):
try:
hash_md5 = hashlib.md5(userId.encode("UTF-8"))
userId = hash_md5.hexdigest().upper()
nonce_str = ''.join(random.sample(LONG_TEXT, random.randint(10, 16)))
time_stamp = int(time.time())
params = {
'app_id': NLPCHAT_APP_ID,
'time_stamp': time_stamp,
'nonce_str': nonce_str,
'session': userId,
'question': text
}
params['sign'] = getReqSign(params, NLPCHAT_APP_KEY)
resp = requests.get(NLPCHAT_URL, params=params)
if resp.status_code == 200:
content_dict = resp.json()
if content_dict['ret'] == 0:
data_dict = content_dict['data']
return data_dict['answer']
else:
print('获取数据失败:{}'.format(content_dict['msg']))
except Exception as exception:
print(str(exception))
def getReqSign(parser, app_key):
params = sorted(parser.items())
uri_str = parse.urlencode(params, encoding="UTF-8")
sign_str = '{}&app_key={}'.format(uri_str, app_key)
hash_md5 = hashlib.md5(sign_str.encode("UTF-8"))
return hash_md5.hexdigest().upper()
if __name__ == '__main__':
if platform.system() in ('Windows', 'Darwin'):
itchat.auto_login(hotReload=True,
loginCallback=init_info, exitCallback=exit_callback)
else:
# 命令行显示登录二维码。
itchat.auto_login(enableCmdQR=2, loginCallback=init_info,
exitCallback=exit_callback)
itchat.run(blockThread=False)
scheduler = BlockingScheduler()
scheduler.add_job(heart_beat, 'interval', minutes=HEART_BEAT_INTERVAL_MINUTES)