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

一 简单介绍

wxpy基于itchat,使用了 Web 微信的通信协议,,经过大量接口优化提高了模块的易用性,并进行丰富的功能扩展。实现了微信登陆、收发消息、搜索好友、数据统计等功能。

总而言之,可用来实现各类微信我的号的自动化操做。(http://wxpy.readthedocs.io/zh/latest/bot.html)

安装:wxpy 支持 Python 3.4-3.6,以及 2.7 版本

pip3 install -U wxpyhtml

安装 pillow模块web

pip3 install pillowjson

安装 pyecharts模块api

pip3 install pyecharts浏览器

二 登陆微信

1 、 扫码登陆微信缓存

from wxpy import *

bot = Bot()

二、cache_path=True微信

运行上面的程序,会弹出二维码,用手机微信扫一扫便可实现登陆。

但上面的程序有一个缺点,每次运行都要扫二维码。不过wxpy很是贴心地提供了缓存的选项,用于将登陆信息保存下来,就不用每次都扫二维码,以下echarts

bot = Bot(cache_path=True) # 必须先登陆过一次之后才可使用缓存

三 微信好友男女比例占比展现

from wxpy import *
from pyecharts import Pie import webbrowser bot=Bot(cache_path=True) #注意手机确认登陆
 friends=bot.friends() #拿到全部朋友对象,放到列表里
attr=['男友','女友','未知性别'] value=[0,0,0] for friend in friends: if friend.sex == 1: # 等于1表明男性
        value[0]+=1
    elif friend.sex == 2: #等于2表明女性
        value[1]+=1
    else: value[2]+=1 pie = Pie("朋友男女比例") pie.add("", attr, value, is_label_show=True) #图表名称str,属性名称list,属性所对应的值list,is_label_show是否如今标签
pie.render('sex.html')#生成html页面 # 打开浏览器
webbrowser.open("sex.html")

 

四 微信好友地域分布

显示中国地图,须要装中国地图模块:post

全球国家地图: echarts-countries-pypkg (1.9MB): 世界地图和 213 个国家,包括中国地图
中国省级地图: echarts-china-provinces-pypkg (730KB):23 个省,5 个自治区
中国市级地图: echarts-china-cities-pypkg (3.8MB):370 个中国城市
中国县区级地图: echarts-china-counties-pypkg (4.1MB):2882 个中国县·区
中国区域地图: echarts-china-misc-pypkg (148KB):11 个中国区域地图,好比华南、华北。

特别注明,中国地图在 echarts-countries-pypkg 里。须要这些地图的朋友,能够装 pip 命令行:

$ pip3 install echarts-countries-pypkg
$ pip3 install echarts-china-provinces-pypkg
$ pip3 install echarts-china-cities-pypkg
$ pip3 install echarts-china-counties-pypkg
$ pip3 install echarts-china-misc-pypkg测试

 

from wxpy import *
from pyecharts import Map import webbrowser bot=Bot(cache_path=True) friends=bot.friends() area_dic={}#定义一个字典,用来存放省市以及省市人数
for friend in friends: if friend.province not in area_dic: area_dic[friend.province]=1
    else: area_dic[friend.province]+=1 attr = area_dic.keys() value = area_dic.values() map = Map("好朋友们的地域分布", width=1200, height=600) map.add( "好友地域分布", attr, value, maptype='china', is_visualmap=True, #结合体VisualMap
 ) #is_visualmap -> bool 是否使用视觉映射组件 # map.render('area.html') webbrowser.open("area.html")

 

 

五 微信聊天机器人

一、为微信传输助手传送消息

这里的file_helper就是微信的文件传输助手,咱们给文件传输助手发送一条消息,能够在手机端的文件传输助手中收到括号内的消息

bot.file_helper.send('lqz say hello')

二、收发消息@bot.register()

from wxpy import * bot=Bot(cache_path=True) @bot.register() def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) # recv_msg.text取得文本
    return '自动回复:%s' %recv_msg.text # 进入Python命令行,让程序保持运行
embed()

三、自动给老婆回复信息

当你在网吧吃着鸡,操做骚出天际时,你老婆打电话让你回家吃饭,此时你怎么办。。

from wxpy import * bot=Bot(cache_path=True) girl_friend=bot.search('刘刘刘')[0] print(girl_friend) @bot.register() # 接收从指定好友发来的消息,发送者即recv_msg.sender为指定好友girl_friend
def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) # recv_msg.text取得文本
    if recv_msg.sender == girl_friend: recv_msg.forward(bot.file_helper,prefix='老婆留言: ') #在文件传输助手里留一份,方便本身忙完了回头查看
        ms='老婆最美丽,我对老婆的爱如滔滔江水,连绵不绝'
        print('>>>给老婆回复的:', ms) return  ms#给老婆回一份
 embed()

四、从微信群里定位好友之拍老板马屁

from wxpy import * bot=Bot(cache_path=True) company_group=bot.groups().search('群名字')[0] boss=company_group.search('老板名字')[0] @bot.register(chats=company_group) #接收从指定群发来的消息,发送者即recv_msg.sender为组
def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) if recv_msg.member == boss: #这里不用recv_msg.render 由于render是群的名字
        recv_msg.forward(bot.file_helper,prefix='老板发言: ') return '老板说的好有道理,深受启发' embed()

五、聊天机器人

给全部人自动回复

import json import requests from wxpy import * bot = Bot(cache_path=True) # 调用图灵机器人API,发送消息并得到机器人的回复
def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "9df516a74fc443769b233b01e8536a42" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[来自智能机器人] " + result["text"] @bot.register() def forward_message(msg): return auto_reply(msg.text) embed()

给指定的群回复

import json import requests from wxpy import * bot = Bot(cache_path=False) group=bot.groups().search('群名字')[0] print(group) # 调用图灵机器人API,发送消息并得到机器人的回复
def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "9d602fe417464cd18beb2083d064bee6" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[来自智能机器人] " + result["text"] @bot.register(chats=group) def forward_message(msg): return auto_reply(msg.text) embed()

给指定的人回复

import requests from wxpy import * bot = Bot( cache_path=True) girl_friend=bot.search('名字r')[0] # 调用图灵机器人API,发送消息并得到机器人的回复
def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "申请图灵机器人获取key值放到这里" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[微信测试,请忽略] " + result["text"] @bot.register() def forward_message(msg): if msg.sender == girl_friend: return auto_reply(msg.text) embed()
相关文章
相关标签/搜索