zoukankan      html  css  js  c++  java
  • wxpy使用

    一 简介

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

    总而言之,可用来实现各种微信个人号的自动化操作。(官网文档:http://wxpy.readthedocs.io/zh/latest/bot.html)

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

    二 登录微信

    from wxpy import *
    bot=Bot(cache_path=True) #  一个Bot 对象可被理解为一个 Web 微信客户端。cache_path 提供了缓存的选项,用于将登录信息保存下来,就不用每次都扫二维码

    三 案例

     1 from pyecharts import Map
     2 import webbrowser   # 文档https://docs.python.org/3/library/webbrowser.html
     3 from wxpy import *
     4 from collections import defaultdict
     5 
     6 bot = Bot(cache_path=True)
     7 friends = bot.friends()
     8 
     9 areas = defaultdict(lambda :0)
    10 for item in friends:
    11     areas[item.province] += 1
    12 
    13 attr = areas.keys()
    14 value = areas.values()
    15 
    16 map = Map("好友分布图", width=1400, height=700)
    17 map.add("", attr, value, maptype='china',is_visualmap=True,is_label_show=True)
    18 map.render('province.html')
    19 webbrowser.open('province.html')
    01.微信好友地域分布
     1 # 自动给指定的人回复
     2 from wxpy import *
     3 
     4 bot = Bot(cache_path=True)
     5 
     6 # 从好友中查找名字叫:林 的人,模糊匹配,可能查出多个人,取出第0个
     7 father = bot.search('')[0]
     8 
     9 
    10 @bot.register()  # 用于注册消息配置
    11 def recv_send_msg(recv_msg):
    12     print('收到的消息:', recv_msg.text)  # recv_msg.text取得文本
    13     # recv_msg.sender 就是谁给我发的消息这个人
    14     if recv_msg.sender == father:
    15         recv_msg.forward(bot.file_helper,prefix='老爸留言: ') # 在文件传输助手里留一份,方便自己忙完了回头查看
    16         ms = '祝早日康复!'
    17         return ms  # 回复
    18 
    19 
    20 embed() # 进入交互式的 Python 命令行界面,并堵塞当前线程支持使用 ipython, bpython 以及原生 python
    02.自动给某人回复
     1 from wxpy import *
     2 bot=Bot(cache_path=True)
     3 # 查找群
     4 company_group=bot.groups().search('群名字')[0]
     5 # 查找群里的某个人
     6 boss=company_group.search('老板名字')[0]
     7 
     8 # 只有company_group这个群发的消息,才会调用这个函数,其他的不会调用
     9 @bot.register(chats=company_group) #接收从指定群发来的消息,发送者即recv_msg.sender为组
    10 def recv_send_msg(recv_msg):
    11     print('收到的消息:',recv_msg.text)
    12     # recv_msg.sender   是群这个对象
    13     if recv_msg.member == boss:
    14         #这里不用recv_msg.render 因为render是群的名字
    15         # recv_msg.forward(bot.file_helper,prefix='老板发言: ')
    16         return '老板说的好有道理,深受启发'
    17 
    18 embed()
    03.给群中某个人回复
     1 import json
     2 import requests
     3 from wxpy import *
     4 
     5 bot = Bot(cache_path=True)
     6 
     7 
     8 # 调用图灵机器人API,发送消息并获得机器人的回复
     9 def auto_reply(text):
    10     url = "http://www.tuling123.com/openapi/api"
    11     api_key = "9df516a74fc443769b233b01e8536a42"
    12     payload = {
    13         "key": api_key,
    14         "info": text,
    15     }
    16     # 接口要求传json格式字符串,返回数据是json格式
    17     result= requests.post(url, data=json.dumps(payload)).json()
    18     # result = json.loads(r.text)
    19     return "[来自智能机器人] " + result["text"]
    20 
    21 
    22 @bot.register()
    23 def forward_message(msg):
    24     print(msg.tex)
    25     return auto_reply(msg.text)
    26 
    27 
    28 embed()
    04.调用图灵机器人api自动回复

    参考:

    1.https://www.cnblogs.com/liuqingzheng/articles/9079192.html

  • 相关阅读:
    c++ 对象作为参数传递
    LeetCode总结 -- 树的性质篇
    【Bug笔记】The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    poj3642 Charm Bracelet(0-1背包)
    [笔试题] 阿里巴巴2013技术类笔试题(完整版手工记录+具体解析)
    swift-switch使用方法
    Android OpenGL ES 应用(二) 纹理
    【VBA研究】查找目录以下全部文件的名称
    解决com.ibatis.sqlmap.client.SqlMapException: There is no statement named in this SqlMap
    Android菜鸟的成长笔记(27)——ViewPager的使用
  • 原文地址:https://www.cnblogs.com/carlous/p/10643125.html
Copyright © 2011-2022 走看看