[665]微信之wechat-sender(基于wxpy库)

注意: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周小董

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值