python企业微信推送信息

  1. 注册企业微信账号
  2. 在我的企业中获取企业的id
    corpID = wwfe6edfd511b002e2
  3. 应用管理中,推送消息的应用。并获取应用的AgentId和Secret
    AgentId = 1000003
    Secret = 5TbU2fubWcfbGeeAQEs8mgm0ZZAc5FJdaGk20KfeUl0
  4. 获取access_token, 请求url如下图所示。
    https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
    corpid和Secret为第2步和第3步获取到的信息

    https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwfe6edfd511b002e2&corpsecret=5TbU2fubWcfbGeeAQEs8mgm0ZZAc5FJdaGk20KfeUl0
  5. 构造消息体,使用post请求发送数据
{
   "touser" : "abelzhu|ZhuShengben",
   "msgtype" : "text",
   "agentid" : 1000002,
   "text" : {
       "content" : "我就试一下"
   },
   "safe":0
}
  1. 发送消息。使用第4步的token值和第5步的data信息,发送消息
    https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ID&corpsecret=SECRET
  2. 代码示例
    python代码
import json
import requests
def getToken():
    r = requests.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwfe6edfd511b002e2&corpsecret=5TbU2fubWcfbGeeAQEs8mgm0ZZAc5FJdaGk20KfeUl0")
    if r.status_code == 200:
        data = json.loads(r.text)
        if data["errcode"] == 0:
            return data["access_token"]
            pass
        else:
            return str(data["errcode"]) + "parameter error"
    else:
        return "network failure"  # 连接服务器失败

def sendMsg():
    data = {
           "touser" : "xdz|WangChaoHua",
           "msgtype" : "text",
           "agentid" : 1000003,
           "text" : {
               "content" : "hhh world"
           },
           "safe":0
        }
    access_token = getToken()
    r = requests.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(access_token), data=json.dumps(data))
    print(r.text)

if __name__ == "__main__":
    sendMsg()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值