使用python接入微信聊天机器人

在放大镜下,你可以看到每一片雪花都是一幅幅精美的图案:有的是晶莹的薄片,有的像白亮的银针,有的像一把张开的小扇,有的像夜空的星星……

本文实例为大家分享了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 玩微信

以上就是使用python接入微信聊天机器人。恋情就像一捧沙,抓得越紧,消散得越多。更多关于使用python接入微信聊天机器人请关注haodaima.com其它相关文章!

您可能有感兴趣的文章
Python自动化运维-使用Python脚本监控华为AR路由器关键路由变化

Python自动化运维-netmiko模块设备自动发现

Python自动化运维—netmiko模块连接并配置华为交换机

Python自动化运维-利用Python-netmiko模块备份设备配置

Python自动化运维-Paramiko模块和堡垒机实战