3 企业微信接口发送消息

import json
import requests

# 参考:https://blog.csdn.net/Jason_WangYing/article/details/108117072
# 企业id在我的企业页面最下变
corpid = 'XXXX'
# 新应用可见范围必须是工作室,所有人才能接到消息
agentid = '1000002'
corpsecret = 'XXXX-XXXX'
# uid 在通讯录每个人的详情页,即账号
uid = 'XXXX'
session = requests.session()


# 获取 token
def get_token(corpid, corpsecret):
    url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}'.format(corpid, corpsecret)
    result = session.post(url=url)
    # print(result.json()['access_token'])
    # print(result.json())
    return result.json()['access_token']


# 发送信息
def send_data(message, access_token):
    send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}'.format(access_token)
    send_values = {
        "touser": uid,
        "msgtype": "text",
        "agentid": agentid,
        "text": {
            "content": message
            },
        "safe": "0"
        }
    send_msgs = (bytes(json.dumps(send_values), 'utf-8'))
    result = session.post(url=send_url, data=send_msgs)
    print(result.json())


if __name__ == '__main__':
    access_token = get_token(corpid, corpsecret)
    send_data('This is a auto message from lizi !', access_token)

 

posted @ 2021-01-20 20:43  栗子测试开发  阅读(310)  评论(1)    收藏  举报