个人微信如何使用python脚本调用/sendtextMessage接口轻松实现在代码中发送一条文字消息,详细代码演示!
调用/sendTextMessage接口,轻松实现Python脚本发送文本消息Python脚本importrequestsimportjsondefsend_text_message(authorization,to_wxid,content,ats): 发送文本消息 Args: authorization (str): 从官网获取的AUTHORIZATION值 to_wxid (str): 接收方微信ID好友wxid或群聊ID content (str): 消息内容 ats (str, optional): 的好友wxid多个用英文逗号分隔。群主/管理员所有人传notifyall Returns: dict: 接口返回的JSON数据 # API接口地址urlhttps://你的API域名/sendTextMessage# 请替换为实际的API地址# 请求头headers{AUTHORIZATION:authorization,Content-Type:application/json}# 请求体payload{toWxid:to_wxid,ats:ats,content:content}try:# 发送POST请求responserequests.post(url,headersheaders,jsonpayload,timeout30)response.raise_for_status()# 检查HTTP错误resultresponse.json()returnresultexceptrequests.exceptions.RequestExceptionase:print(f请求失败:{e})returnNoneexceptjson.JSONDecodeErrorase:print(fJSON解析失败:{e})returnNonedefparse_send_result(result):解析发送结果ifresultisNone:print(发送失败请求异常)returnretresult.get(ret)msgresult.get(msg)ifret0:print(✅ 消息发送成功)dataresult.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. 基本配置AUTHORIZATIONyour_authorization_token_here# 替换为你的AUTHORIZATION# 2. 示例1发送给单个好友print( 示例1发送给好友 )friend_wxidwxid_o9jco5r4p63l22# 替换为好友的wxidresultsend_text_message(authorizationAUTHORIZATION,to_wxidfriend_wxid,content你好这是一条测试消息。)parse_send_result(result)print()# 3. 示例2发送到群聊print( 示例2发送到群聊 )group_wxid47558923582chatroom# 替换为群聊IDresultsend_text_message(authorizationAUTHORIZATION,to_wxidgroup_wxid,content各位群友大家好)parse_send_result(result)print()# 4. 示例3在群聊中某人print( 示例3在群聊中某人 )resultsend_text_message(authorizationAUTHORIZATION,to_wxidgroup_wxid,content张三 你好欢迎加入群聊,atswxid_zhangsan_wxid# 替换为要的好友wxid)parse_send_result(result)print()# 5. 示例4群主/管理员所有人print( 示例4群主所有人 )resultsend_text_message(authorizationAUTHORIZATION,to_wxidgroup_wxid,content所有人 今晚8点有重要通知请留意,atsnotifyall)parse_send_result(result)print()# 6. 示例5批量发送发送给多个好友/群聊print( 示例5批量发送 )targets[{wxid:wxid_friend1,msg:你好},{wxid:wxid_friend2,msg:朋友你好},{wxid:chatroom123chatroom,msg:群友们好}]fortargetintargets:resultsend_text_message(authorizationAUTHORIZATION,to_wxidtarget[wxid],contenttarget[msg])parse_send_result(result)详细使用说明1. 准备工作1.1 安装依赖pipinstallrequests1.2 获取必要信息AUTHORIZATION: 从系统官网获取用于身份验证好友/群聊的wxid: 可以通过联系人模块的fetchContactsList接口获取API地址: 替换脚本中的url为实际的API地址2. 参数说明参数类型必填说明authorizationstring是身份验证tokentoWxidstring是接收方ID好友wxid或群聊IDcontentstring是消息内容atsstring否的好友wxid多个用逗号分隔3. 使用步骤步骤1登录获取AUTHORIZATION# 先调用登录模块接口获取AUTHORIZATION# 具体参考登录模块的技术文档https://wechat-bot.apifox.cn步骤2确定接收方# 微信好友wxid示例friend_wxidwxid_o9jco5r4p8888# 微信群聊ID示例以chatroom结尾group_wxid47558923222chatroom步骤3发送消息# 简单发送resultsend_text_message(authorizationyour_token,to_wxidwxid_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: 新消息ID5. 注意事项AUTHORIZATION管理同一个微信账号避免使用不同的AUTHORIZATION值以免触发风控建议保存并复用同一个token发送频率建议控制发送频率避免短时间内大量发送群聊中多人时注意不要滥用群聊功能content中必须包含xxx文字ats参数传入要的好友wxidnotifyall仅群主或管理员可用错误处理建议添加重试机制注意捕获网络异常和API错误