import requests
import json
def get_access_token():
url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"
headers = {
"Content-Type": "application/json"
}
payload = {
"appKey": '你找到的',
"appSecret": '你找到的'
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
data = response.json()
access_token = data.get("accessToken")
return access_token
else:
print("Failed to get access token:", response.text)
return None
access_token = get_access_token()
print("Access Token:", access_token)
已存在的企业内部群,可调用根据corpid选择会话获取
开发者后台统一登录 - 钉钉统一身份认证
import requests
# 替换成自己的
app_key = "你对应的"
app_secret = "你对应的"
corp_id = "你对应的"
group_name = "你对应的"
def get_group_ids(access_token):
url = "https://oapi.dingtalk.com/chat/list"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {access_token}"
}
params = {
"size": 200,
"offset": 0,
"corpId": corp_id
}
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
data = response.json()
group_list = data.get("chatlist", [])
for group in group_list:
if group.get("name") == group_name:
return group.get("chatid")
print(f"No group named {group_name} found.")
return None
else:
print("Failed to get group IDs:", response.text)
return None
def get_access_token():
url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"
headers = {
"Content-Type": "application/json"
}
payload = {
"appKey": '你对应的',
"appSecret": '你对应的'
}
response = requests.post(url, headers=headers, json=payload)
if response.status_code == 200:
data = response.json()
access_token = data.get("accessToken")
return access_token
else:
print("Failed to get access token:", response.text)
return None
access_token = get_access_token()
print("Access Token:", access_token)
group_ids = get_group_ids(access_token)
print("Group IDs:", group_ids) #即为access_token
import requests
url = 'https://api.dingtalk.com/v1.0/robot/groupMessages/send'
headers = {
'Content-Type': 'application/json',
'x-acs-dingtalk-access-token': '换成你自己的'
}
data = {
"msgParam": '{"content":"对应要删的消息"}',
"msgKey": "sampleText",
"openConversationId": "换成你自己的",
"robotCode": "换成你自己的"
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
result = response.json()
process_query_key = result.get("processQueryKey")
print("processQueryKey:", process_query_key)
else:
print("Request failed with status code:", response.status_code)
print("Error:", response.text)
import requests
def recall_message(access_token, open_conversation_id, robot_code, message_ids):
url = f"https://api.dingtalk.com/v1.0/robot/groupMessages/recall?x-acs-dingtalk-access-token={access_token}"
data = {
"openConversationId": open_conversation_id,
"robotCode": robot_code,
"processQueryKeys": message_ids
}
headers = {
"Content-Type": "application/json"
}
try:
response = requests.post(url, json=data, headers=headers)
if response.status_code == 200:
print("消息撤回成功")
else:
print("消息撤回失败")
# 打印失败理由
print(response.text)
except requests.exceptions.RequestException as err:
print(f"请求发生错误: {err}")
# 设置访问令牌、群会话ID、机器人代码和要撤回的消息ID列表
access_token = "your access token"
open_conversation_id = "XXXX"
robot_code = "XXXX"
message_ids = ["String"]
# 调用撤回消息函数
recall_message(access_token, open_conversation_id, robot_code, message_ids)