在這裏默認你們以及安裝好了pip,咱們須要安裝wxpy 以及wechat_sender 兩個包,這裏推薦使用國內的豆瓣源,若是你們網速過硬 請忽略。。html
1
2
|
pip install wxpy -i
"https://pypi.doubanio.com/simple/"
pip install wechat_sender -i
"https://pypi.doubanio.com/simple/"
|
wxpy 使用起來很是簡單,咱們只須要建立一個bot 對象,程序運行後,會彈出二維碼,掃描二維碼後顯示登錄成功。python
下述代碼在登錄完成後,會向咱們的文件傳輸助手發送一個「hello world!」。(每一個程序都須要一個hello world)redis
from wxpy import * bot = Bot() bot.file_helper.send('hello world!') print("ending")
關於Bot()對象的相關參數說明,咱們能夠在源碼中的註釋中看到:api
"""
:param cache_path:
* 設置當前會話的緩存路徑,並開啓緩存功能;爲 `None` (默認) 則不開啓緩存功能。
* 開啓緩存後可在短期內避免重複掃碼,緩存失效時會從新要求登錄。
* 設爲 `True` 時,使用默認的緩存路徑 'wxpy.pkl'。
:param console_qr:
* 在終端中顯示登錄二維碼,須要安裝 pillow 模塊 (`pip3 install pillow`)。
* 可爲整數(int),表示二維碼單元格的寬度,一般爲 2 (當被設爲 `True` 時,也將在內部看成 2)。
* 也可爲負數,表示以反色顯示二維碼,適用於淺底深字的命令行界面。
* 例如: 在大部分 Linux 系統中可設爲 `True` 或 2,而在 macOS Terminal 的默認白底配色中,應設爲 -2。
:param qr_path: 保存二維碼的路徑
:param qr_callback: 得到二維碼後的回調,能夠用來定義二維碼的處理方式,接收參數: uuid, status, qrcode
:param login_callback: 登錄成功後的回調,若不指定,將進行清屏操做,並刪除二維碼文件
:param logout_callback: 登出時的回調
"""
這裏介紹一下兩個主要使用到的參數:緩存
cache_path: 在開發過程當中能夠設置爲True 避免每次登錄都須要從新掃描,具備緩存的做用。 qr_path:用於保存二維碼生成圖片,主要解決服務器圖片展現不方便的問題
如代碼所示,咱們能夠經過Bot.friends 以及Bot.groups 來獲取到全部的好友以及聊天羣,這裏須要注意的是,聊天羣須要保存到通信錄中,否則可能會出現找不到聊天羣的狀況。服務器
在搜索方法中,能夠提供的參數有:姓名,city,province,sex 等相關變量。微信
關於好友的詳細API文檔,能夠參考---》 微信好友APIasync
from wxpy import * bot = Bot() # 獲取全部好友 friends = bot.friends() # 遍歷輸出好友名稱 for friend in friends: print(friend) # 找到好友 friend = bot.friends().search('被單')[0] print(friend) friend.send("hello world!") # 獲取全部聊天羣 groups = bot.groups() for group in groups: print(group) # 找到目標羣 group = groups.search("409")[0] group.send("hello world!")
接下來主要介紹一下用戶發送消息的類型,目前wxpy 支持發送文本,圖片,視頻以及文件。主要的發送方式如代碼所示:ide
這裏比較重要的就是關於 @bot.register() 的使用,該註釋主要用於註冊消息接收器,咱們能夠根據特定的需求,配置不同的消息接收器。工具
Bot.
register
(chats=None, msg_types=None, except_self=True, run_async=True, enabled=True) 詳情能夠查看源碼中的介紹
關於消息處理API,讀者能夠在該地址下查看詳細的配置,這裏不作過多的描述。
代碼中有使用到:embed() 這個方法, 主要用於阻塞進程,避免因爲程序運行結束致使沒法接收消息。
from wxpy import * bot = Bot() # 獲取好友 my_friend = bot.friends().search('被單')[0] # 搜索信息 messages = bot.messages.search(keywords='測試', sender=bot.self) for message in messages: print(message) # 發送文本 my_friend.send('Hello, WeChat!') # 發送圖片 my_friend.send_image('my_picture.png') # 發送視頻 my_friend.send_video('my_video.mov') # 發送文件 my_friend.send_file('my_file.zip') # 以動態的方式發送圖片 my_friend.send('@img@my_picture.png') # 發送公衆號 my_friend.send_raw_msg( # 名片的原始消息類型 raw_type=42, # 注意 `username` 在這裏應爲微信 ID,且被髮送的名片必須爲本身的好友 raw_content='<msg username="wxpy_bot" nickname="wxpy 機器人"/>' ) # 消息接收監聽器 @bot.register() def print_others(msg): # 輸出監聽到的消息 print(msg) # 回覆消息 msg.reply("hello world") embed()
wxpy 接入圖靈機器人至關方便,咱們首先須要到圖靈近期人官網進行註冊,哆啦A夢的任意門。
經過註冊Tuling 對象,當咱們接收到消息的時候,能夠直接使用tuling機器人來幫咱們進行答覆。其餘的業務需求各位能夠根據本身的需求來完成相應的邏輯。
from wxpy import * bot = Bot() # 獲取好友 dear = bot.friends().search('被單')[0] # 註冊得到我的的圖靈機器人key 填入 tuling = Tuling(api_key='******') # 使用圖靈機器人自動與指定好友聊天 @bot.register(dear) def reply_my_friend(msg): print(msg) tuling.do_reply(msg) embed()
在熟悉了wxpy 的相關操做後,咱們接下來介紹一下一個主要使用到的工具。因爲wxpy 的設計,致使了一些業務操做並很差進行實現。所以咱們在這裏引入一個工具類:wechat_sender 。
首先咱們須要像往常同樣進行微信登錄,而後使用 listen() 進行對咱們的 bot() 對象進行監聽。
在這裏咱們能夠看到了和上面代碼的區別,這裏使用的是listen(),上面是使用embed()進行監聽。 咱們再這裏使用listen 進行監聽對象後,能夠設置相應的配置。監聽默認設置的接收對象爲self.file_helper,經過設置receivers 能夠配置消息的接收者。
# login.py
from wxpy import * from wechat_sender import * bot = Bot() friend = bot.friends().search('被單')[0] listen(bot, token='test', receivers=[friend])
# sender.py coding: utf-8 from wechat_sender import Sender sender = Sender(token='test') sender.send('hello world!')
在別的python 文件中,咱們只須要建立一個Sender() 對象,而後調用Sender.send()方法,便可對咱們設定好的消息接收者發送消息。
Sender()在建立的時候能夠經過特定的參數設定,好比這裏使用了 token 用於避免多個listen 致使sender 混淆。還能夠在sender中設置receiver 從listen 中選取須要接收消息的對象。
微信登錄模塊:
from wechat_sender import * from wxpy import * bot = Bot(qr_path="qr.png") group = bot.groups().search('監控報警')[0] print("微信登錄成功!進行監控報警功能!") print(group) # listen(bot, token='test', receivers=[group])
業務處理模塊:
import redis from wechat_sender import * sender = Sender(token='test', receivers='監控報警') while true: # do anything sender.send(message=data) # do anything p.unsubscribe('cardniu-monitor') print('取消訂閱')