### Basic WxHelper Bot Setup and Event Handling
Source: https://github.com/miloira/wxhelper/blob/master/README.md
This example demonstrates the basic setup of a WxHelper bot, including event handlers for login, start, stop, and message processing. It shows how to send a text message to 'filehelper'. Ensure the bot is run after configuration.
```python
# import os
# os.environ["WXHELPER_LOG_LEVEL"] = "INFO" # 修改日志输出级别
# os.environ["WXHELPER_LOG_FORMAT"] = "{time:YYYY-MM-DD HH:mm:ss} | {message}" # 修改日志输出格式
from wxhelper import Bot
from wxhelper import events
from wxhelper.model import Event
def on_login(bot: Bot, event: Event):
print("登录成功之后会触发这个函数")
def on_start(bot: Bot):
print("微信客户端打开之后会触发这个函数")
def on_stop(bot: Bot):
print("关闭微信客户端之前会触发这个函数")
def on_before_message(bot: Bot, event: Event):
print("消息事件处理之前")
def on_after_message(bot: Bot, event: Event):
print("消息事件处理之后")
bot = Bot(
# faked_version="3.9.10.19", # 解除微信低版本限制
on_login=on_login,
on_start=on_start,
on_stop=on_stop,
on_before_message=on_before_message,
on_after_message=on_after_message
)
# 消息回调地址
# bot.set_webhook_url("http://127.0.0.1:8000")
@bot.handle(events.TEXT_MESSAGE)
def on_message(bot: Bot, event: Event):
bot.send_text("filehelper", "Hello, World!")
bot.run()
```
--------------------------------
### Install WxHelper
Source: https://github.com/miloira/wxhelper/blob/master/README.md
Install the WxHelper package using pip. This command installs the library and its dependencies.
```bash
pip install wxhelper
```
--------------------------------
### Get Login QR Code
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves the URL for the login QR code to facilitate scanning for authentication.
```python
from wxhelper import Bot
bot = Bot()
# 获取登录二维码
qrcode_response = bot.get_qrcode()
print(f"二维码URL: {qrcode_response.qrCodeUrl}")
print(f"状态: {qrcode_response.code}")
```
--------------------------------
### Get User Profile
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves detailed information about the currently logged-in WeChat user.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取用户信息
info = bot.info # 或 bot.get_self_info()
print(f"wxid: {info.wxid}")
print(f"昵称: {info.name}")
print(f"微信号: {info.account}")
print(f"手机号: {info.mobile}")
print(f"头像: {info.headImage}")
print(f"签名: {info.signature}")
print(f"城市: {info.city}")
print(f"省份: {info.province}")
print(f"国家: {info.country}")
print(f"数据路径: {info.dataSavePath}")
bot.run()
```
--------------------------------
### Get Room Member Details
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves detailed information for a specific member within a group chat. Requires the member's wxid.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取群成员资料
member = bot.get_room_member("wxid_member")
print(f"wxid: {member.wxid}")
print(f"昵称: {member.nickname}")
print(f"账号: {member.account}")
print(f"头像: {member.headImage}")
print(f"v3: {member.v3}")
bot.run()
```
--------------------------------
### Get Room Members
Source: https://context7.com/miloira/wxhelper/llms.txt
Fetches the list of all members within a group chat. The members are returned as a comma-separated string.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
# 获取群成员
room_members = bot.get_room_members(room_id)
print(f"群ID: {room_members.chatRoomId}")
print(f"群主: {room_members.admin}")
print(f"成员列表: {room_members.members}")
# 成员列表是以逗号分隔的字符串
member_list = room_members.members.split("^G")
print(f"成员数量: {len(member_list)}")
bot.run()
```
--------------------------------
### Get Room Details
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves detailed information about a group chat, including the owner and announcement. Requires the group's chatRoomId.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
# 获取群详情
room = bot.get_room(room_id)
print(f"群ID: {room.chatRoomId}")
print(f"群主: {room.admin}")
print(f"群公告: {room.notice}")
print(f"群信息XML: {room.xml}")
bot.run()
```
--------------------------------
### Get Contact Nickname
Source: https://context7.com/miloira/wxhelper/llms.txt
Fetches the nickname for a specified friend or group chat. Requires the contact's wxid.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取好友昵称
nickname_response = bot.get_contact_nickname("wxid_friend")
print(f"好友昵称: {nickname_response.name}")
# 获取群聊名称
group_response = bot.get_contact_nickname("12345678@chatroom")
print(f"群名称: {group_response.name}")
bot.run()
```
--------------------------------
### Get Contact List
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves a list of all friends and group chats for the currently logged-in account. Filters can be applied to distinguish between friends and groups.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取联系人列表
contacts = bot.get_contacts()
for contact in contacts:
print(f"wxid: {contact.wxid}")
print(f"用户名: {contact.userName}")
print(f"自定义账号: {contact.customAccount}")
print(f"类型: {contact.type}")
print("---")
# 筛选群聊(wxid 以 @chatroom 结尾)
groups = [c for c in contacts if c.wxid.endswith("@chatroom")]
print(f"群聊数量: {len(groups)}")
bot.run()
```
--------------------------------
### Get Room Member Nickname
Source: https://context7.com/miloira/wxhelper/llms.txt
Fetches a member's nickname within a specific group chat. This is often referred to as the group remark name. Requires both room_id and member_wxid.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
member_id = "wxid_member"
# 获取群成员昵称
response = bot.get_room_member_nickname(room_id, member_id)
print(f"群昵称: {response.nickname}")
print(f"结果: {response.code}")
bot.run()
```
--------------------------------
### 配置日志级别与格式
Source: https://context7.com/miloira/wxhelper/llms.txt
在初始化 Bot 之前,通过设置环境变量 WXHELPER_LOG_LEVEL 和 WXHELPER_LOG_FORMAT 自定义日志输出。
```python
import os
# 设置日志级别 (DEBUG, INFO, WARNING, ERROR)
os.environ["WXHELPER_LOG_LEVEL"] = "INFO"
# 设置日志格式
os.environ["WXHELPER_LOG_FORMAT"] = "{time:YYYY-MM-DD HH:mm:ss} | {message}"
from wxhelper import Bot
bot = Bot()
bot.run()
```
--------------------------------
### Initialize WxHelper Bot with Lifecycle Hooks
Source: https://context7.com/miloira/wxhelper/llms.txt
Configure Bot instance with lifecycle hooks for login, message processing, and client start/stop events. The faked_version parameter can bypass WeChat version restrictions.
```python
from wxhelper import Bot
from wxhelper import events
from wxhelper.model import Event
def on_login(bot: Bot, event: Event):
print(f"登录成功!用户: {bot.info.name}, wxid: {bot.info.wxid}")
def on_start(bot: Bot):
print("微信客户端已启动")
def on_stop(bot: Bot):
print("微信客户端即将关闭")
def on_before_message(bot: Bot, event: Event):
print(f"收到消息,类型: {event.type}")
def on_after_message(bot: Bot, event: Event):
print("消息处理完成")
# 初始化机器人
bot = Bot(
faked_version="3.9.10.19", # 可选:伪造版本号解除低版本限制
on_login=on_login,
on_start=on_start,
on_stop=on_stop,
on_before_message=on_before_message,
on_after_message=on_after_message
)
# 启动机器人
bot.run()
```
--------------------------------
### Send Image Messages with WxHelper
Source: https://context7.com/miloira/wxhelper/llms.txt
Send image files to users or groups by providing the absolute path to the image. The response object indicates the success or failure of the operation.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 发送图片
response = bot.send_image("filehelper", "C:/Users/test/Pictures/image.jpg")
print(f"图片发送结果: {response.code}")
# 发送图片到群聊
bot.send_image("12345678@chatroom", "D:/photos/photo.png")
bot.run()
```
--------------------------------
### 设置 Webhook 与消息处理
Source: https://context7.com/miloira/wxhelper/llms.txt
通过 set_webhook_url 配置回调地址,并使用 @bot.handle() 装饰器注册消息处理函数。
```python
bot.set_webhook_url("http://127.0.0.1:8000/webhook")
@bot.handle()
def on_message(bot: Bot, event):
# 消息会自动转发到 webhook URL
print(f"消息已处理: {event.content}")
bot.run()
```
--------------------------------
### Send Files Using WxHelper
Source: https://context7.com/miloira/wxhelper/llms.txt
Transmit various file types to contacts or group chats by specifying the file's absolute path. The response code indicates the transmission status.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 发送文件
response = bot.send_file("filehelper", "C:/Documents/report.pdf")
print(f"文件发送结果: {response.code}")
# 发送压缩包
bot.send_file("friend_wxid", "D:/backup/data.zip")
bot.run()
```
--------------------------------
### Retrieve Database Information
Source: https://context7.com/miloira/wxhelper/llms.txt
Lists all available WeChat local databases and their associated table structures.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取数据库信息
db_list = bot.get_db_info()
for db in db_list:
print(f"数据库: {db.databaseName}")
print(f"句柄: {db.handle}")
print("表:")
for table in db.tables:
print(f" - {table.tableName}")
bot.run()
```
--------------------------------
### Add Friend via wxhelper
Source: https://context7.com/miloira/wxhelper/llms.txt
Sends a friend request to a specified WeChat ID.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 添加好友
response = bot.add_friend("wxid_target", "你好,我想加你为好友")
print(f"添加好友结果: {response.code}, {response.result}")
bot.run()
```
--------------------------------
### Set Webhook
Source: https://context7.com/miloira/wxhelper/llms.txt
Configures a callback URL to forward received messages to an external HTTP service.
```python
from wxhelper import Bot
bot = Bot()
```
--------------------------------
### Forward Messages Using WxHelper
Source: https://context7.com/miloira/wxhelper/llms.txt
Forward received messages to other users or group chats using the message ID. The response object provides the status code of the forwarding operation.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 转发消息到其他用户
if event.msgId:
response = bot.forward("another_wxid", str(event.msgId))
print(f"转发结果: {response.code}")
bot.run()
```
--------------------------------
### Group Member Management
Source: https://context7.com/miloira/wxhelper/llms.txt
Manages group members by adding, inviting, or deleting them. For groups with less than 40 members, direct addition is used. For larger groups, invitations are sent.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
# 添加群成员(40人以下群聊直接拉人)
add_response = bot.add_room_members(room_id, ["wxid_new_member1", "wxid_new_member2"])
print(f"添加结果: {add_response.result}")
# 邀请群成员(40人以上群聊发送邀请)
invite_response = bot.invite_room_members(room_id, "wxid_invite_member")
print(f"邀请结果: {invite_response.result}")
# 删除群成员
delete_response = bot.delete_room_members(room_id, ["wxid_remove_member"])
print(f"删除结果: {delete_response.result}")
bot.run()
```
--------------------------------
### 消息事件类型常量
Source: https://context7.com/miloira/wxhelper/llms.txt
列举了框架中用于消息处理函数注册的事件类型常量及其对应的 ID。
```python
from wxhelper import events
# 可用的事件类型
events.ALL_MESSAGE # 99999 - 全部消息
events.TEXT_MESSAGE # 1 - 文本消息
events.IMAGE_MESSAGE # 3 - 图片消息
events.VOICE_MESSAGE # 34 - 语音消息
events.FRIEND_VERIFY_MESSAGE # 37 - 好友验证请求
events.CARD_MESSAGE # 42 - 卡片消息
events.VIDEO_MESSAGE # 43 - 视频消息
events.EMOJI_MESSAGE # 47 - 表情消息
events.LOCATION_MESSAGE # 48 - 位置消息
events.XML_MESSAGE # 49 - XML消息
events.VOIP_MESSAGE # 50 - 视频/语音通话
events.PHONE_MESSAGE # 51 - 手机端同步消息
events.NOTICE_MESSAGE # 10000 - 通知消息
events.SYSTEM_MESSAGE # 10002 - 系统消息
```
--------------------------------
### Send Text Messages via WxHelper
Source: https://context7.com/miloira/wxhelper/llms.txt
Send text messages to specific users, groups, or the 'filehelper'. The response object contains the sending status code and result. Can also be used to reply to incoming messages.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 发送文本到文件传输助手
response = bot.send_text("filehelper", "Hello, World!")
print(f"发送结果: {response.code}, {response.result}")
# 回复消息发送者
if event.fromUser:
bot.send_text(event.fromUser, f"收到你的消息: {event.content}")
# 发送到群聊
bot.send_text("12345678@chatroom", "这是群消息")
bot.run()
```
--------------------------------
### Query Local Database
Source: https://context7.com/miloira/wxhelper/llms.txt
Executes SQL queries against the WeChat local database and retrieves contact information or avatar URLs.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取数据库句柄
db_info = bot.get_db_info()
db_handle = db_info[0].handle
# 执行 SQL 查询
result = bot.exec_sql(db_handle, "SELECT * FROM Contact LIMIT 10;")
print(f"查询结果: {result.data}")
# 查询特定联系人
contact = bot.get_contact_by_db("wxid_target")
if contact:
print(f"联系人信息: {contact}")
# 获取头像 URL
avatar_url = bot.get_head_image_url("wxid_target")
print(f"头像URL: {avatar_url}")
bot.run()
```
--------------------------------
### Retrieve Voice Messages
Source: https://context7.com/miloira/wxhelper/llms.txt
Downloads and saves voice messages to a specified directory.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
if event.msgId:
# 获取语音消息
response = bot.get_voice(event.msgId, "C:/voice_save_dir")
print(f"获取语音结果: {response.result}")
bot.run()
```
--------------------------------
### Send 'Pat' Messages in Group Chats
Source: https://context7.com/miloira/wxhelper/llms.txt
Initiate a 'pat' interaction with a specific member within a group chat. The response indicates the success of sending the pat message.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
member_wxid = "wxid_target_member"
# 发送拍一拍
response = bot.send_pat(room_id, member_wxid)
print(f"拍一拍结果: {response.result}")
bot.run()
```
--------------------------------
### Check Login Status
Source: https://context7.com/miloira/wxhelper/llms.txt
Verifies the current authentication status of the WeChat client.
```python
from wxhelper import Bot
bot = Bot()
# 检查登录状态
login_status = bot.check_login()
print(f"登录状态码: {login_status.code}")
print(f"登录URL: {login_status.login_url}")
```
--------------------------------
### Handle Various WeChat Message Types with Decorators
Source: https://context7.com/miloira/wxhelper/llms.txt
Register message handlers using the @bot.handle() decorator for different event types like ALL_MESSAGE, TEXT_MESSAGE, IMAGE_MESSAGE, FRIEND_VERIFY_MESSAGE, and media types. Supports handling multiple types simultaneously.
```python
from wxhelper import Bot, events
from wxhelper.model import Event
bot = Bot()
# 处理所有消息
@bot.handle(events.ALL_MESSAGE)
def handle_all(bot: Bot, event: Event):
print(f"收到消息: {event.content}")
# 处理文本消息
@bot.handle(events.TEXT_MESSAGE)
def handle_text(bot: Bot, event: Event):
if event.fromGroup:
print(f"群消息 - 群ID: {event.fromGroup}, 发送者: {event.fromUser}")
else:
print(f"私聊消息 - 发送者: {event.fromUser}")
print(f"内容: {event.content}")
# 处理图片消息
@bot.handle(events.IMAGE_MESSAGE)
def handle_image(bot: Bot, event: Event):
print(f"收到图片: {event.path}")
# 处理好友验证请求
@bot.handle(events.FRIEND_VERIFY_MESSAGE)
def handle_friend_request(bot: Bot, event: Event):
print(f"收到好友请求: {event.content}")
# 同时处理多种消息类型
@bot.handle([events.VIDEO_MESSAGE, events.VOICE_MESSAGE])
def handle_media(bot: Bot, event: Event):
print(f"收到媒体消息,类型: {event.type}")
bot.run()
```
--------------------------------
### Fetch Moments (SNS)
Source: https://context7.com/miloira/wxhelper/llms.txt
Retrieves content from the WeChat Moments feed, supporting pagination.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 获取朋友圈第一页
first_page = bot.get_sns_first_page()
print(f"第一页结果: {first_page.result}")
# 获取下一页(需要提供 snsId)
next_page = bot.get_sns_next_page(sns_id=123456)
print(f"下一页结果: {next_page.result}")
bot.run()
```
--------------------------------
### Verify Friend Request
Source: https://context7.com/miloira/wxhelper/llms.txt
Processes incoming friend verification requests by accepting or rejecting them using the provided ticket and encrypted username.
```python
from wxhelper import Bot, events
bot = Bot()
@bot.handle(events.FRIEND_VERIFY_MESSAGE)
def on_friend_request(bot: Bot, event):
# 从事件中获取验证信息
content = event.content
v3 = content.get("@encryptusername", "")
v4 = content.get("@ticket", "")
# 通过好友请求 (permission: 3 表示通过)
response = bot.verify_apply(v3, v4, permission=3)
print(f"验证结果: {response.result}")
bot.run()
```
--------------------------------
### Send @ Mentions in Group Chats
Source: https://context7.com/miloira/wxhelper/llms.txt
Send messages in group chats that specifically @mention one or multiple members. Requires the group ID and a list of member wxids.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
# @ 单个成员
bot.send_room_at(room_id, ["wxid_member1"], "请查看这条消息")
# @ 多个成员
bot.send_room_at(
room_id,
["wxid_member1", "wxid_member2", "wxid_member3"],
"大家好,请注意这条通知"
)
bot.run()
```
--------------------------------
### Download Message Attachments
Source: https://context7.com/miloira/wxhelper/llms.txt
Downloads files attached to a specific message using its ID.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
if event.msgId:
# 下载附件
response = bot.download_attachment(event.msgId)
print(f"下载结果: {response.result}")
bot.run()
```
--------------------------------
### Logout Account
Source: https://context7.com/miloira/wxhelper/llms.txt
Terminates the current WeChat session.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 退出登录
response = bot.logout()
print(f"退出结果: {response.result}")
bot.run()
```
--------------------------------
### Perform OCR on Images
Source: https://context7.com/miloira/wxhelper/llms.txt
Extracts text content from images using OCR.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# OCR 识别
response = bot.ocr("C:/path/to/image.jpg")
print(f"识别结果: {response.text}")
print(f"状态: {response.code}")
bot.run()
```
--------------------------------
### Top/Untop Room Message
Source: https://context7.com/miloira/wxhelper/llms.txt
Pins or unpins a message within a group chat. Requires the room ID and the message sender's wxid.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
wxid = "wxid_member"
# 置顶消息
top_response = bot.top_msg(room_id, wxid)
print(f"置顶结果: {top_response.result}")
# 取消置顶
remove_response = bot.remove_top_msg(room_id, wxid)
print(f"取消置顶结果: {remove_response.result}")
bot.run()
```
--------------------------------
### Set Room Self Nickname
Source: https://context7.com/miloira/wxhelper/llms.txt
Modifies your own nickname within a specific group chat. Requires the room ID, your wxid, and the new nickname.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
room_id = "12345678@chatroom"
my_wxid = bot.info.wxid
# 修改群昵称
response = bot.set_room_self_nickname(room_id, my_wxid, "我的新群昵称")
print(f"修改结果: {response.result}")
bot.run()
```
--------------------------------
### Handle Transfer Receipts
Source: https://context7.com/miloira/wxhelper/llms.txt
Confirms or refuses incoming WeChat money transfers.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
wxid = "wxid_sender"
transaction_id = "transaction_123"
transfer_id = "transfer_456"
# 确认收款
confirm_response = bot.confirm_receipt(wxid, transaction_id, transfer_id)
print(f"确认收款: {confirm_response.result}")
# 拒绝收款
refuse_response = bot.refuse_receipt(wxid, transaction_id, transfer_id)
print(f"拒绝收款: {refuse_response.result}")
bot.run()
```
--------------------------------
### Search Friend
Source: https://context7.com/miloira/wxhelper/llms.txt
Searches for a user by their WeChat ID or phone number. Returns user information including nickname, account, and signature.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 搜索好友
user_info = bot.search_friend("wxid_or_phone")
print(f"昵称: {user_info.nickname}")
print(f"微信号: {user_info.account}")
print(f"V3: {user_info.v3}")
print(f"头像: {user_info.smallImage}")
print(f"签名: {user_info.signature}")
print(f"城市: {user_info.city}")
print(f"省份: {user_info.province}")
bot.run()
```
--------------------------------
### Modify Contact Remark
Source: https://context7.com/miloira/wxhelper/llms.txt
Changes the remark name for a specified friend. Requires the friend's wxid and the new remark name.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 修改好友备注
response = bot.modify_contact_remark("wxid_friend", "新备注名")
print(f"修改结果: {response.code}, {response.result}")
bot.run()
```
--------------------------------
### Decode Encrypted Images
Source: https://context7.com/miloira/wxhelper/llms.txt
Decrypts WeChat's .dat image files into standard image formats.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 解码图片
response = bot.decode_image(
image_path="C:/path/to/encrypted.dat",
save_path="C:/path/to/decoded.jpg"
)
print(f"解码结果: {response.result}")
bot.run()
```
--------------------------------
### Recall Sent Message
Source: https://context7.com/miloira/wxhelper/llms.txt
Recalls a message that was sent within the last two minutes. Requires the message ID.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 发送消息
response = bot.send_text("filehelper", "这条消息将被撤回")
# 撤回消息
if event.msgId:
revoke_response = bot.revoke_msg(str(event.msgId))
print(f"撤回结果: {revoke_response.result}")
bot.run()
```
--------------------------------
### Forward Official Account Messages
Source: https://context7.com/miloira/wxhelper/llms.txt
Forward articles from official WeChat accounts to contacts or groups. Supports forwarding by providing article details or by using the message's svrid.
```python
from wxhelper import Bot
bot = Bot()
@bot.handle()
def on_message(bot: Bot, event):
# 转发公众号消息
response = bot.forward_public_msg(
wxid="filehelper",
appname="公众号名称",
username="gh_xxxxxx",
title="文章标题",
url="https://mp.weixin.qq.com/s/xxxxx",
thumb_url="https://mmbiz.qpic.cn/xxxxx",
digest="文章摘要"
)
print(f"转发结果: {response.code}")
# 通过 svrid 转发公众号消息
bot.forward_public_msg_by_svrid("friend_wxid", msg_id=123456789)
bot.run()
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.