个人微信如何使用python脚本调用/sendtextMessage接口轻松实现在代码中发送一条文字消息,详细代码演示!
·
调用 /sendTextMessage 接口,轻松实现Python脚本发送文本消息!
Python脚本
import requests
import json
def send_text_message(authorization, to_wxid, content, ats=""):
"""
发送文本消息
Args:
authorization (str): 从官网获取的AUTHORIZATION值
to_wxid (str): 接收方微信ID(好友wxid或群聊ID)
content (str): 消息内容
ats (str, optional): @的好友wxid,多个用英文逗号分隔。群主/管理员@所有人传'notify@all'
Returns:
dict: 接口返回的JSON数据
"""
# API接口地址
url = "https://你的API域名/sendTextMessage" # 请替换为实际的API地址
# 请求头
headers = {
"AUTHORIZATION": authorization,
"Content-Type": "application/json"
}
# 请求体
payload = {
"toWxid": to_wxid,
"ats": ats,
"content": content
}
try:
# 发送POST请求
response = requests.post(url, headers=headers, json=payload, timeout=30)
response.raise_for_status() # 检查HTTP错误
result = response.json()
return result
except requests.exceptions.RequestException as e:
print(f"请求失败: {e}")
return None
except json.JSONDecodeError as e:
print(f"JSON解析失败: {e}")
return None
def parse_send_result(result):
"""解析发送结果"""
if result is None:
print("发送失败:请求异常")
return
ret = result.get("ret")
msg = result.get("msg")
if ret == 0:
print("✅ 消息发送成功!")
data = result.get("data", {})
print(f" 接收方: {data.get('toWxid')}")
print(f" 消息ID: {data.get('msgId')}")
print(f" 新消息ID: {data.get('newMsgId')}")
print(f" 发送时间: {data.get('createTime')}")
else:
print(f"❌ 消息发送失败!")
print(f" 错误码: {ret}")
print(f" 错误信息: {msg}")
# ========== 使用示例 ==========
if __name__ == "__main__":
# 1. 基本配置
AUTHORIZATION = "your_authorization_token_here" # 替换为你的AUTHORIZATION
# 2. 示例1:发送给单个好友
print("=== 示例1:发送给好友 ===")
friend_wxid = "wxid_o9jco5r4p63l22" # 替换为好友的wxid
result = send_text_message(
authorization=AUTHORIZATION,
to_wxid=friend_wxid,
content="你好!这是一条测试消息。"
)
parse_send_result(result)
print()
# 3. 示例2:发送到群聊
print("=== 示例2:发送到群聊 ===")
group_wxid = "47558923582@chatroom" # 替换为群聊ID
result = send_text_message(
authorization=AUTHORIZATION,
to_wxid=group_wxid,
content="各位群友,大家好!"
)
parse_send_result(result)
print()
# 4. 示例3:在群聊中@某人
print("=== 示例3:在群聊中@某人 ===")
result = send_text_message(
authorization=AUTHORIZATION,
to_wxid=group_wxid,
content="@张三 你好,欢迎加入群聊!",
ats="wxid_zhangsan_wxid" # 替换为要@的好友wxid
)
parse_send_result(result)
print()
# 5. 示例4:群主/管理员@所有人
print("=== 示例4:群主@所有人 ===")
result = send_text_message(
authorization=AUTHORIZATION,
to_wxid=group_wxid,
content="@所有人 今晚8点有重要通知,请留意!",
ats="notify@all"
)
parse_send_result(result)
print()
# 6. 示例5:批量发送(发送给多个好友/群聊)
print("=== 示例5:批量发送 ===")
targets = [
{"wxid": "wxid_friend1", "msg": "你好!"},
{"wxid": "wxid_friend2", "msg": "朋友你好!"},
{"wxid": "chatroom123@chatroom", "msg": "群友们好!"}
]
for target in targets:
result = send_text_message(
authorization=AUTHORIZATION,
to_wxid=target["wxid"],
content=target["msg"]
)
parse_send_result(result)
详细使用说明
1. 准备工作
1.1 安装依赖
pip install requests
1.2 获取必要信息
- AUTHORIZATION: 从系统官网获取,用于身份验证
- 好友/群聊的wxid: 可以通过联系人模块的
fetchContactsList接口获取 - API地址: 替换脚本中的
url为实际的API地址
2. 参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
authorization |
string | 是 | 身份验证token |
toWxid |
string | 是 | 接收方ID(好友wxid或群聊ID) |
content |
string | 是 | 消息内容 |
ats |
string | 否 | @的好友wxid,多个用逗号分隔 |
3. 使用步骤
步骤1:登录获取AUTHORIZATION
# 先调用登录模块接口获取AUTHORIZATION
# 具体参考登录模块的技术文档:https://wechat-bot.apifox.cn
步骤2:确定接收方
# 微信好友wxid示例
friend_wxid = "wxid_o9jco5r4p8888"
# 微信群聊ID示例(以@chatroom结尾)
group_wxid = "47558923222@chatroom"
步骤3:发送消息
# 简单发送
result = send_text_message(
authorization="your_token",
to_wxid="wxid_xxx",
content="你好!"
)
4. 返回结果解析
成功响应示例:
{
"ret": 0,
"msg": "success",
"data": {
"toWxid": "wxid_xxx",
"createTime": 1701234567,
"msgId": 1234567890,
"newMsgId": 9876543210,
"type": 0
}
}
ret: 0表示成功,非0表示失败msg: 返回信息data.msgId: 消息ID,可用于后续操作(如撤回)data.newMsgId: 新消息ID
5. 注意事项
- AUTHORIZATION管理:
- 同一个微信账号避免使用不同的AUTHORIZATION值,以免触发风控
- 建议保存并复用同一个token
- 发送频率:
- 建议控制发送频率,避免短时间内大量发送
- 群聊中@多人时,注意不要滥用
- 群聊@功能:
content中必须包含@xxx文字ats参数传入要@的好友wxidnotify@all仅群主或管理员可用
- 错误处理:
- 建议添加重试机制
- 注意捕获网络异常和API错误
更多推荐



所有评论(0)