企业微信外部群发送消息(每天每个群只能发送一条,好像是)
import requests
import json
def get_access_token(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 = 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 = 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()