使用python接入微信聊天机器人
作者:Luke__Zhang 时间:2022-04-08 10:07:23
本文实例为大家分享了python接入微信聊天机器人的具体代码,供大家参考,具体内容如下
1.安装库wxpy:
pip install -U wxpy
or
pip install -U wxpy -i https://pypi.doubanio.com/simple/
2.简单操作上手:
from wxpy import *
bot = Bot(cache_path=True) #扫码登录验证
friends_stat = bot.friends().stats()
friend_loc = [] # 每一个元素是一个二元列表,分别存储地区和人数信息
for province, count in friends_stat["province"].items():
if province != "":
friend_loc.append([province, count])
# 对好友人数倒序排序
friend_loc.sort(key=lambda x: x[1], reverse=True)
# 打印好友人数最多的10个地区:
for item in friend_loc[:10]:
print(item[0], item[1])
#打印好友男女比例:
for sex, count in friends_stat["sex"].items():
# 1代表MALE, 2代表FEMALE
if sex == 1:
print("MALE %d" % count)
elif sex == 2:
print("FEMALE %d" % count)
3.聊天机器人,一起来调戏好友吧
from wxpy import *
bot = Bot(cache_path=True)
my_friend = bot.friends().search('好友昵称')[0] #定位好友
my_friend.send('Hello!') #发送“Hello!”测试一下对接是否成功。
group = bot.groups().search('群名')[0] #定位群
#接入图灵api:需要去下述网址申请:
tuling = Tuling(api_key='在http://www.tuling123.com/申请')
# 使用图灵机器人自动与指定好友聊天
@bot.register(my_friend)
def reply_my_friend(msg):
tuling.do_reply(msg)
Reference:
[1] wxpy: 用 Python 玩微信
来源:https://blog.csdn.net/Maverick_7/article/details/79686700
标签:python,微信,机器人
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Pytorch中的Tensorboard与Transforms搭配使用
2023-07-09 08:27:05
![](https://img.aspxhome.com/file/2023/2/62022_0s.png)
sqlserver数据库主键的生成方式小结(sqlserver,mysql)
2012-08-21 10:25:45
Symfony2框架创建项目与模板设置实例详解
2023-11-20 23:36:39
![](https://img.aspxhome.com/file/2023/9/92809_0s.jpg)
Go中strings的常用方法详解
2023-06-27 01:58:32
asp之日期和时间函数示例
2008-04-13 06:50:00
FrontPage XP设计教程4——Css样式表的应用
2008-10-11 12:25:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017123534164s.jpg)
go实现文件的创建、删除与读取示例代码
2023-06-17 05:10:50
JavaScript 编程引入命名空间
2007-10-11 19:00:00
桌面中心(二)数据库写入
2023-11-18 12:26:15
ASP分页技术详解
2007-08-13 15:25:00
![](https://img.aspxhome.com/file/UploadPic/20078/13/2007813153154844s.jpg)
编码问题引起的折腾
2009-07-03 12:43:00
提高ASP性能的最佳选择第一部分
2007-08-15 12:31:00
详细讲解SQL Server数据库的文件恢复技术
2009-01-15 12:54:00
Django压缩静态文件的实现方法详析
2023-06-15 05:31:33
ASP 禁用清除页面缓存
2008-07-21 12:43:00
php查询mysql数据库并将结果保存到数组的方法
2023-07-20 17:47:32
利用CSS属性实现进度条的方式
2010-02-25 12:31:00
Win10下Python3.7.3安装教程图解
2023-09-02 18:16:54
![](https://img.aspxhome.com/file/2023/6/62316_0s.jpg)
修改新云CMS底部版权信息字数限制
2008-07-31 18:00:00
![](https://img.aspxhome.com/file/UploadPic/20087/31/200873118219668s.gif)
firefox扩展插件制作方法
2007-10-12 13:50:00
![](https://img.aspxhome.com/file/UploadPic/200710/12/20071012135210684s.jpg)