版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/xc_zhou/article/details/101628100
注意:wechat-sender基于wxpy,wxpy基于itchat,目前腾讯以大批量关闭微信网页版接口,所以wechat-sender当前已不能使用,以下仅做学习记录
wechat-sender 是基于 wxpy 和 tornado 实现的一个可以将你的网站、爬虫、脚本等其他应用中各种消息 (日志、报警、运行结果等) 发送到微信的工具。
安装
pip install wechat_sender
复制
使用
登录微信并启动 wechat_sender 服务.
from wxpy import *
from wechat_sender import *
bot = Bot()
listen(bot)
# 之后 wechat_sender 将持续运行等待接收外部消息
复制
在外部向微信发送消息.
from wechat_sender import Sender
Sender().send('Hello From Wechat Sender')
# Hello From Wechat Sender 这条消息将通过 1 中登录微信的文件助手发送给你
复制
如果你是 wxpy 的使用者,只需更改一句即可使用 wechat_sender:
例如这是你本来的代码:
# coding: utf-8
from __future__ import unicode_literals
from wxpy import *
bot = Bot('bot.pkl')
my_friend = bot.friends().search('xxx')[0]
my_friend.send('Hello WeChat!')
@bot.register(Friend)
def reply_test(msg):
msg.reply('test')
bot.join()
复制
使用 wechat_sender:
# coding: utf-8
from __future__ import unicode_literals
from wxpy import *
from wechat_sender import listen
bot = Bot('bot.pkl')
my_friend = bot.friends().search('xxx')[0]
my_friend.send('Hello WeChat!')
@bot.register(Friend)
def reply_test(msg):
msg.reply('test')
listen(bot) # 只需改变最后一行代码
复制
之后如果你想在其他地方发送微信消息给你自己,只需要:
# coding: utf-8
from wechat_sender import Sender
Sender().send("Hello From Wechat Sender")
复制
API
wechat_sender.listen(bot, receiver, token, port)
- bot(必填|Bot对象)-wxpy 的 Bot 对象实例
- receiver(可选|Chat 对象)-接收消息,wxpy 的 Chat 对象实例, 不填为当前 bot 对象的文件接收者
- token(可选|string)- 信令,防止 receiver 被非法滥用,建议加上 token 防止非法使用,如果使用 token 请在 send 时也使用统一 token,否则无法发送。token 建议为 32 位及以上的无规律字符串
- port(可选|integer)- 监听端口, 监听端口默认为 10245 ,如有冲突或特殊需要请自行指定,需要和 send 处统一
wechat_sender.send(message, token, port)
- message(必填|string)-需要发送的消息,目前只支持文本消息
- token(可选|string)-信令,如果不为空请保持和 listen 中的 token 一致
- port(可选|integer)-发送端口,如果不为空请保持和 listen 中的 port 一致
wechat-sender:https://pypi.org/project/wechat-sender/0.1.4/
参考:https://blog.csdn.net/sunny_future/article/details/81545327