以下是使用 Python 实现企业微信应用发送长消息的示例代码:

import requests
import json

# 企业微信应用相关参数
corpid = '企业ID'
corpsecret = '应用Secret'
agentid = '应用ID'

# 获取 access_token
res = requests.get(
    f'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}')
access_token = res.json()['access_token']

# 构建发送消息的数据
data = {
    'touser': '@all',
    'msgtype': 'markdown',
    'agentid': agentid,
    'markdown': {
        'content': '这是一条长消息\n\n' * 10
    }
}

# 发送消息
res = requests.post(
    f'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={access_token}',
    data=json.dumps(data),
    headers={'Content-Type': 'application/json'}
)

# 输出返回结果
print(res.json())

在上述代码中,我们首先使用 corpid 和 corpsecret 获取了 access_token,然后构建了发送消息的数据,其中的 markdown 内容是重复了 10 遍的一条长消息。接着,我们使用 requests.post 方法发送了消息,并输出了返回结果。

需要注意的是,在发送消息时,我们需要将数据以 JSON 格式的字符串形式发送,并在请求头中指定 Content-Type 为 application/json。此外,企业微信应用发送消息的 API 限制了消息长度,如果超过了长度限制,需要将消息分成多条发送。

Python 企业微信应用发送长消息教程

原文地址: https://www.cveoy.top/t/topic/nVXn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录