企业微信外部群发送消息(每天每个群只能发送一条,好像是)

import requests
import json

# 获取access_token
def get_access_token(secret):
    # 应用的Secret
    token_url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    token_params = {'corpid':'企业微信企业ID', 'corpsecret':secret} 
    token_data = requests.get(token_url, params=token_params).json()
    access_token = token_data["access_token"]
    print("AccessToken Is ",access_token)
    return access_token
    
def get_group_chat_id():
    # 企业微信接口地址 
    url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/groupchat/list"

    # 获取access_token
    access_token = get_access_token(secret = "你的组织通讯录的secret") 

    # 构造请求数据
    data = {
        "status_filter": 0,
        "limit": 10 
    }

    # 设置请求参数,发送获取请求
    params = {'access_token': access_token}
    res = requests.post(url, params=params, data=json.dumps(data))

    # 打印客户群信息
    chat_id = res.json()["group_chat_list"][0]['chat_id']
    print('\nGroupList: ',chat_id)
    return chat_id

def send_group_message():
    # 企业微信接口地址
    url = "https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_msg_template"

    # 获取access_token    
    access_token = get_access_token(secret='你的组织通讯录的secret')

    # 构造请求数据
    data = {
        "chat_type" : "group",
        "chat_id" : [get_group_chat_id()],
        "sender" : "发送者的用户账号(账号!账号!不是昵称!)", 
        "text":{"content": "发送内容"},
        }

    # 设置请求参数,发送请求
    params = {'access_token': access_token,"debug":1}
    res = requests.post(url, params=params, data=json.dumps(data))
    print(res.text)
    
send_group_message()
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐