第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > python 实现微信机器人自动回复

python 实现微信机器人自动回复

时间:2018-05-07 09:15:57

相关推荐

python 实现微信机器人自动回复

1.python 实现微信机器人自动回复

# 源代码如下:import jsonimport itchatimport requestsimport re# 机器人接口调用def getHtmlText(url):try:r = requests.get(url, timeout=30)r.raise_for_status()r.encoding = r.apparent_encodingreturn r.textexcept:return ""# 自动回复# 封装好的装饰器,当接收到的消息是Text,即文字消息@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing', 'Picture'])def text_reply(msg):# 当消息不是由自己发出的时候print(msg)if msg['FromUserName'] != Name["自己的微信昵称"]:# 回复给好友url = "/api.php?key=free&appid=0&msg="url = url + msg['Text']html = getHtmlText(url)object = json.loads(html)re = object['content']print("auto message--->" + re)return reelse:print("no auto send--->")if __name__ == '__main__':itchat.auto_login()qr = itchat.get_QR;# 获取自己的UserNamefriends = itchat.get_friends(update=True)[0:]Name = {}Nic = []User = []for i in range(len(friends)):Nic.append(friends[i]["NickName"])User.append(friends[i]["UserName"])for i in range(len(friends)):Name[Nic[i]] = User[i]itchat.run()

2.启动后看到如下,表示启动成功,会弹出一张二维码图

3.扫码成功后登录成功!看到Start auto replying 即成功啦!

4.完成机器人聊天啦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。