用企业微信发送告警
1.遇到的坑
{"errcode":40001,"errmsg":"invalid credential, hint: [1507881186_cb1093c9bcaedaf108b7ce2ea10f2d38]"} 40001 不合法的secret参数 secret在应用详情/通讯录管理助手可查看 排查secret的取值也没有错啊。就郁闷了。 最后发现问题:是corpid写错了。把企业id写成了应用id。 因为“全局错误码”里没有提到corpid错误了会造成40001错误,所以一直以为是scerent的错。 参考:https://www.cnblogs.com/luoahong/articles/9018223.html
2.实用Python脚本
[root@cm ~]# cat test_jj.py #!python3 # -*- coding: utf-8 -*- import json import requests class WeChat(object): def __init__(self, corpid, secret, agentid): self.url = "https://qyapi.weixin.qq.com" self.corpid = corpid self.secret = secret self.agentid = agentid # 获取企业微信的 access_token def access_token(self): url_arg = '/cgi-bin/gettoken?corpid={id}&corpsecret={crt}'.format( id=self.corpid, crt=self.secret) url = self.url + url_arg response = requests.get(url=url) print('i am here') text = response.text print(text) self.token = json.loads(text)['access_token'] # 构建消息格式 def messages(self, msg): values = { "touser": '@all', "msgtype": 'text', "agentid": self.agentid, "text": {'content': msg}, "safe": 0 } # python 3 # self.msg = (bytes(json.dumps(values), 'utf-8')) # python 2 self.msg = json.dumps(values) # 发送信息 def send_message(self, msg): self.access_token() self.messages(msg) send_url = '{url}/cgi-bin/message/send?access_token={token}'.format( url=self.url, token=self.token) response = requests.post(url=send_url, data=self.msg) errcode = json.loads(response.text)['errcode'] if errcode == 0: print('Succesfully') else: print('Failed') #使用示例: corpid = "wwdbe2bhaha48965a3012" secret = "-rLV8ahtMIcYlRZMhahaik7y5ikASozwjjppx8ZPaBXyk" agentid = '10200237' msg = "supply high quolity and low price product......" wechat = WeChat(corpid, secret, agentid) wechat.access_token() wechat.send_message(msg)
用一个例子来演示会更加清晰
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App