微信 自动回复 机器人_python控制微信
2023-06-13 09:13:27 时间
大家好,又见面了,我是你们的朋友全栈君。
1.python 实现微信机器人自动回复
# 源代码如下:
import json
import itchat
import requests
import re
# 机器人接口调用
def getHtmlText(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
return r.text
except:
return ""
# 自动回复
# 封装好的装饰器,当接收到的消息是Text,即文字消息
@itchat.msg_register(['Text', 'Map', 'Card', 'Note', 'Sharing', 'Picture'])
def text_reply(msg):
# 当消息不是由自己发出的时候
print(msg)
if msg['FromUserName'] != Name["自己的微信昵称"]:
# 回复给好友
url = "http://api.qingyunke.com/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 re
else:
print("no auto send--->")
if __name__ == '__main__':
itchat.auto_login()
qr = itchat.get_QR;
# 获取自己的UserName
friends = 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.完成机器人聊天啦!
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/189146.html原文链接:https://javaforall.cn
相关文章
- python语法(二)——截取字符串的方法详解
- python的random()函数用法_Python随机函数random用法示例
- Python入门系列(五)一篇搞懂python语句
- Python报错:pandas.errors.ParserError: Error tokenizing data. C error: Expected 3……
- Python 入门指南第二节 | 使用 Python 解释器
- python从列表字典集合中筛选数据
- 【Python 局域网控制】——做一个超简单的局域网指令控制电脑
- python控制键盘鼠标实现窗口程序自动化2021.10.14
- 【说站】python列表如何传递到线程?
- 【说站】python循环遍历如何理解
- python读取pkl_Python 读取文件
- 上手python之json数据格式
- Python Socket套接字编程
- Python 使用sigthief签发证书
- python 分隔列队机制完美解决TCP粘包分包问题
- Python基础语法-基本数据类型-布尔值
- Python网络编程-socketserver
- python-Python与MySQL数据库-使用Python执行MySQL查询
- python-Python与SQLite数据库-使用Python执行SQLite查询(二)
- python-Python与SQLite数据库-处理SQLite查询结果(二)
- Linux下使用Python开发体验之旅(linux使用python)
- Python 突变测试介绍
- Python在MSSQL中的应用实践(python与mssql)
- Python流程控制实例代码
- 一个计算身份证号码校验位的Python小程序