python使用企业微信发送文本消息

脚本用于企业微信发送消息,可以使用普通微信关注企业微信帐户后实现微信消息推送

# -*- coding: utf-8 -*-
# coding=utf-8
import json
import requests

#企业惟一ID
corpid = "xxxxxx"

#自定义应用密钥
secret = "xxxxxxx"

#自定义应用编号
agentid = "xxxxxxx"


def GetTokenFromServer(Corpid,Secret):
    access_token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+secret
    r = requests.get(url=access_token_url)
    print(r.json()['access_token'])
    return r.json()['access_token']

def send_wechat():
    access_token = GetTokenFromServer(corpid,secret)
    Url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % access_token
    headers={
        "Content-Type": "application/json"
        }
    data={
        "touser": "xxx",    #消息接收人,多个用|分隔 如:  xxx|xxx|xxx
        #"toparty" : "1",    #如须要发送消息到整个部门的人,则使用该参数, 数字表明该部门 ID
        "agentid" : agentid,  #自定义应用编号
        "msgtype" : "text",    #发送类型
        "text": {
            "content": "hello world."    #内容
        }
    }
    data_dict = json.dumps(data, ensure_ascii=False).encode('utf-8')
    r = requests.post(url=Url, headers=headers, data=data_dict)
    print(r.text)

send_wechat()
相关文章
相关标签/搜索