zoukankan      html  css  js  c++  java
  • 微信机器人(实现自动回复,数据统计) + 数据可视化

    微信机器人的使用

    安装:wxpy模块、pillow模块、pyecharts数据可视化模块(https://pyecharts.org # 官网)

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

    全球国家地图: 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

    用pkl模块保存的朋友数据,删除可以重扫 



    生成饼图统计好友性别
    from wxpy import * # 导入微信机器人模块所有
    from pyecharts import Pie,Map # Pie是饼图,map是地图
    import webbrowser

    '''实例化微信机器人'''
    bot = Bot(cache_path=True) # 参数True表示微信登陆后缓存好友信息

    friends = bot.friends() # 拿到所有好友对象

    '''生成饼图数据'''
    attr = ['男性','女性','未知',]
    value=[0,0,0]
    for friend in friends:
    if friend.sex == 1:
    value[0] += 1
    elif friend.sex == 2:
    value[1] += 1
    else:
    value[2] += 1

    pie = Pie('男女朋友比例') # 实例化对象
    pie.add('',attr,value,is_label_show=True) # is_label_show=True 让效果好看点
    pie.render('sex.html')
    webbrowser.open('sex.html') # 让浏览器打开一个地址

      

    生成地图统计微信好友分布
    from wxpy import * # 导入微信机器人模块所有
    from pyecharts import Map # Pie是饼图,map是地图
    import webbrowser

    bot = Bot(cache_path=True) # 参数True表示微信登陆后缓存好友信息
    bro = webbrowser.Chrome
    friends = bot.friends() # 拿到所有好友对象


    area_dic = {} # 用来统计
    for friend in friends:
    # province 就是对象里的省份字段
    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() # 人数

    maps = Map("好友地区分布",width=1200,height=600) # 生成对象
    maps.add( # 调用add方法传入参数
    "好友地区分布",
    attr,
    value,
    maptype='china',
    is_visualmap=True,) # 传入两个参数列表,地区china

    maps.render('area.html')

    webbrowser.open('area.html')


    自动回复消息的简单应用
    from wxpy import *
    bot=Bot(cache_path=True)

    @bot.register() # 装饰器装饰
    def recv_send_msg(recv_msg): # 对象传入参数
    print('收到的消息:',recv_msg.text) # 收到的消息
    return '自动回复:%s' %recv_msg.text # 回复消息直接return

    # 进入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取得文本
    # sender参数就是谁发给你的消息
    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] # 模糊匹配到的群名字是在列表里所以取第0个

    boss=company_group.search('老板名字')[0] # 从群对象里模糊匹配指定人的名字,名字是在列表里所以取第0个


    @bot.register(chats=company_group) # 这个装饰器要传入群对象,就是只有这个群的聊天才能走到后面的程序
    def recv_send_msg(recv_msg):
    print('收到的消息:',recv_msg.text)
    # .member出来的是个人,.sender出来的是群对象,而不是个人
    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)

    laopo = bot.search('XXX')[0] # 指定自动回复的人

    @bot.register()
    def forward_message(msg):
    if msg.sender == laopo:
    return auto_reply(msg.text)



    # 调用图灵机器人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)) # 向接口发送post请求,携带数据
    result = json.loads(r.content)
    print()
    return result["text"]

    embed()

    if __name__ == '__main__':
    forward_message()


  • 相关阅读:
    ajax发送请求
    Canvas与SVG的区别
    jquery中attr()和prop()方法的区别
    ab(Apache Benchmark)测试工具的安装
    Nginx与阻塞操作
    构造函数调用顺序
    对一个类求sizeof,虚继承
    代码清单16-4 服务器压力测试程序
    libevent源码分析-TCP服务端代码
    Linux内核源码之红黑树
  • 原文地址:https://www.cnblogs.com/shizhengquan/p/10735148.html
Copyright © 2011-2022 走看看