钉钉群机器人撤回信息-实操详细教程

仅支持企业应用

1.在开发平台创建机器人,勾选机器人配置

发布

最终要在这里能找到才算成功

注:发布后要在钉钉内部群添加机器人选择新建的

第一步:获取accessToken(程序权限)

找到Appkey和AppSecret

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)

第二步:获取robotCode

第三步获取:openConversationId(群id)

已存在的企业内部群,可调用根据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

第四步:获取processQueryKeys(消息ID)

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)
相关推荐
AI小欧同学4 小时前
【AIGC-ChatGPT副业提示词指令】炼金术士的元素启示:在神秘中寻找生命的答案【限时免费阅读,一天之后自动进入进阶课程】
chatgpt·机器人·aigc
茶猫_6 小时前
力扣面试题 - 40 迷路的机器人 C语言解法
c语言·数据结构·算法·leetcode·机器人·深度优先
哔哥哔特商务网12 小时前
智元与汇川加码,机器人如何利好电机市场?
microsoft·机器人
敲代码敲到头发茂密15 小时前
基于 LangChain 实现数据库问答机器人
数据库·人工智能·语言模型·langchain·机器人
cnbestec15 小时前
Kinova在开源家庭服务机器人TidyBot++研究里大展身手
人工智能·科技·机器人
W Y19 小时前
【智能制造-50】雅可比矩阵在机器人中如何应用
矩阵·机器人·制造
速科德电机科技1 天前
机器人加装电主轴【铣削、钻孔、打磨、去毛刺】更高效
机器人·电主轴
温轻舟1 天前
前端开发 -- 自动回复机器人【附完整源码】
前端·javascript·css·机器人·html·交互·温轻舟
开疆智能1 天前
ModbusTCP转Profinet:ABB机器人与PLC的高效连接
科技·机器人·自动化
bs_1011 天前
【保姆式】python调用api通过机器人发送文件到飞书指定群聊
python·机器人·飞书