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

yipeiwu_com5年前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设计】。

相关文章

pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率

pytorch绘制并显示loss曲线和acc曲线,LeNet5识别图像准确率

我用的是Anaconda3 ,用spyder编写pytorch的代码,在Anaconda3中新建了一个pytorch的虚拟环境(虚拟环境的名字就叫pytorch)。 以下内容仅供参考哦~...

使用Python实现windows下的抓包与解析

使用Python实现windows下的抓包与解析

系统环境:windows7,选择windows系统是因为我对自己平时日常机器上的流量比较感兴趣 python环境:python2.7 ,这里不选择python3的原因,是因为接下来要用到...

Python3使用xml.dom.minidom和xml.etree模块儿解析xml文件封装函数的方法

总结了一下使用Python对xml文件的解析,用到的模块儿如下: 分别从xml字符串和xml文件转换为xml对象,然后解析xml内容,查询指定信息字段。 from xml.dom.m...

python模拟事件触发机制详解

本文实例为大家分享了python模拟事件触发机制的具体代码,供大家参考,具体内容如下 EventManager.py # -*- encoding: UTF-8 -*- # 系统模...

Python Tkinter基础控件用法

Python Tkinter基础控件用法

本文实例展示了Python Tkinter基础控件的用法,分享给大家供大家参考之用。具体方法如下: # -*- coding: utf-8 -*- from Tkinter impo...