Fork me on GitHub

daydayup863

人生就像一杯茶,不会苦一辈子,但总会苦一阵子。

0%

微信聊天自动回复机器人代码

微信聊天自动回复小程序, 输入指定内容控制开关,以及添加附加内容等小功能,

完整代码

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
#!/usr/bin/python
# -*- coding: utf-8 -*-

import itchat
import re,requests
from urllib.parse import quote,unquote

AUTO_REPLAY=0
BASEMSG = ''

def robot(data):
global AUTO_REPLAY
ini="{'sessionId':'09e2aca4d0a541f88eecc77c03a8b393','robotId':'webbot','userId':'462d49d3742745bb98f7538c42f9f874','body':{'content':'" + data + "'},'type':'txt'}&ts=1529917589648"
url = "http://i.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=" + quote(ini)
cookie = {"cnonce": "808116", "sig": "0c3021aa5552fe597bb55448b40ad2a90d2dead5","XISESSIONID": "hlbnd1oiwar01dfje825gavcn", "nonce": "273765", "hibext_instdsigdip2": "1"}
r = requests.get(url, cookies=cookie)
#print(type(r))
#json_r = r.json()
#print(json_r)
pattern = re.compile(r'\"fontColor\":0,\"content\":\"(.*?)\"')
result = pattern.findall(r.text)
#return result[1].replace("\\r\\n",'')
#return result[1].replace("\\n",'')
return result[1].replace("\\r\\n",'')

import re,requests
try:
from urllib import quote,unquote
except ImportError:
from urllib.parse import quote,unquote

import json


def qingke_robot(msg):
#ssl._create_default_https_context = ssl._create_unverified_context
url = r"http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s" % (quote(msg))

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen

result = urlopen(url)
response = json.loads(result.read().decode("utf-8"))
code = response['result']
if code != 0:
return "error"
content = response['content'].replace('{br}','\n')
return content


# 自动回复
# 封装好的装饰器,当接收到的消息是Text,即文字消息
@itchat.msg_register('Text')
def text_reply(msg):
global BASEMSG
global AUTO_REPLAY
# 当消息不是由自己发出的时候
#return u"[主人暂时不在,我是周小秘]{}".format(tulin_robot(msg['Text']))
#return u"[主人暂时不在,我是周小秘]{}".format(robot(msg['Text']))

if msg['Text'].lower() == "open":
AUTO_REPLAY=1
return "自动聊天功能已打开."
elif msg['Text'].lower() == "close":
AUTO_REPLAY=0
return "自动聊天功能已关闭."
elif msg['Text'].lower() == "add":
BASEMSG = "[主人暂时不在,我是周小秘 ]:"
return "添加附加内容成功"
elif msg['Text'].lower() == "del":
BASEMSG = ""
AUTO_REPLAY=0
return "取消附加内容成功"

# 回复给好友
if AUTO_REPLAY > 0 :
if BASEMSG != "":
return BASEMSG + qingke_robot(msg['Text'])
return qingke_robot(msg['Text'])

if __name__ == '__main__':
itchat.auto_login(enableCmdQR=2)

# 获取自己的UserName
myUserName = itchat.get_friends(update=True)[0]["UserName"]
itchat.run(debug=True)
#print(qingke_robot("你好"))

-------------本文结束感谢您的阅读-------------
听说,打赏我的人都找到了真爱

欢迎关注我的其它发布渠道