首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >[665]微信之wechat-sender(基于wxpy库)

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

作者头像
周小董
修改2025-08-22 17:17:01
修改2025-08-22 17:17:01
1.8K00
代码可运行
举报
文章被收录于专栏:爬虫精选爬虫精选
运行总次数:0
代码可运行

注意:wechat-sender基于wxpy,wxpy基于itchat,目前腾讯以大批量关闭微信网页版接口,所以wechat-sender当前已不能使用,以下仅做学习记录

wechat-sender 是基于 wxpy 和 tornado 实现的一个可以将你的网站、爬虫、脚本等其他应用中各种消息 (日志、报警、运行结果等) 发送到微信的工具。

安装

代码语言:javascript
代码运行次数:0
运行
复制
pip install wechat_sender

使用

登录微信并启动 wechat_sender 服务.

代码语言:javascript
代码运行次数:0
运行
复制
from wxpy import *
from wechat_sender import *
bot = Bot()
listen(bot)
# 之后 wechat_sender 将持续运行等待接收外部消息

在外部向微信发送消息.

代码语言:javascript
代码运行次数:0
运行
复制
from wechat_sender import Sender
Sender().send('Hello From Wechat Sender')
# Hello From Wechat Sender 这条消息将通过 1 中登录微信的文件助手发送给你

如果你是 wxpy 的使用者,只需更改一句即可使用 wechat_sender:

例如这是你本来的代码:

代码语言:javascript
代码运行次数:0
运行
复制
# 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:

代码语言:javascript
代码运行次数:0
运行
复制
# 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) # 只需改变最后一行代码

之后如果你想在其他地方发送微信消息给你自己,只需要:

代码语言:javascript
代码运行次数:0
运行
复制
# 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/

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/09/28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用
  • API
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档