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

仅支持企业应用

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)
相关推荐
花生树什么树16 小时前
机器人操作臂逆运动学
机器人·逆运动学
anzocapital16 小时前
anzocapital 昂首资本:外汇机器人趋势判断秘籍
机器人
旗晟机器人1 天前
化工防爆巡检机器人:在挑战中成长,为化工安全保驾护航
人工智能·机器人
QYR市场调研1 天前
手术机器人:精准医疗的新选择
机器人
小帅吖1 天前
Webots控制器编程
机器人·机器人仿真·webots
天行健王春城老师2 天前
基于TRIZ的教育机器人功能创新
经验分享·机器人
xwz小王子2 天前
Science Robotics 综述揭示演化研究新范式,从机器人复活远古生物!
机器人·古生物启发
奔跑的花短裤2 天前
少儿编程启蒙学习
学习·青少年编程·机器人·ai编程
sun_weitao2 天前
钉钉扫码登录(DTFrameLogin) 用户注销后重新登录出现回调叠加的问题
前端·javascript·钉钉
邱自强2 天前
钉钉小程序使用getApp实现类似provide inject的功能 应用场景:解决页面同步子组件弹窗的滚动问题
小程序·钉钉