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


    仅支持企业应用

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

    发布

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

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

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

    找到Appkey和AppSecret

    1. import requests
    2. import json
    3. def get_access_token():
    4. url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"
    5. headers = {
    6. "Content-Type": "application/json"
    7. }
    8. payload = {
    9. "appKey": '你找到的',
    10. "appSecret": '你找到的'
    11. }
    12. response = requests.post(url, headers=headers, json=payload)
    13. if response.status_code == 200:
    14. data = response.json()
    15. access_token = data.get("accessToken")
    16. return access_token
    17. else:
    18. print("Failed to get access token:", response.text)
    19. return None
    20. access_token = get_access_token()
    21. print("Access Token:", access_token)

    第二步:获取robotCode

    第三步获取:openConversationId(群id)

    已存在的企业内部群,可调用根据corpid选择会话获取

    开发者后台统一登录 - 钉钉统一身份认证

    1. import requests
    2. # 替换成自己的
    3. app_key = "你对应的"
    4. app_secret = "你对应的"
    5. corp_id = "你对应的"
    6. group_name = "你对应的"
    7. def get_group_ids(access_token):
    8. url = "https://oapi.dingtalk.com/chat/list"
    9. headers = {
    10. "Content-Type": "application/json",
    11. "Authorization": f"Bearer {access_token}"
    12. }
    13. params = {
    14. "size": 200,
    15. "offset": 0,
    16. "corpId": corp_id
    17. }
    18. response = requests.get(url, headers=headers, params=params)
    19. if response.status_code == 200:
    20. data = response.json()
    21. group_list = data.get("chatlist", [])
    22. for group in group_list:
    23. if group.get("name") == group_name:
    24. return group.get("chatid")
    25. print(f"No group named {group_name} found.")
    26. return None
    27. else:
    28. print("Failed to get group IDs:", response.text)
    29. return None
    30. def get_access_token():
    31. url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"
    32. headers = {
    33. "Content-Type": "application/json"
    34. }
    35. payload = {
    36. "appKey": '你对应的',
    37. "appSecret": '你对应的'
    38. }
    39. response = requests.post(url, headers=headers, json=payload)
    40. if response.status_code == 200:
    41. data = response.json()
    42. access_token = data.get("accessToken")
    43. return access_token
    44. else:
    45. print("Failed to get access token:", response.text)
    46. return None
    47. access_token = get_access_token()
    48. print("Access Token:", access_token)
    49. group_ids = get_group_ids(access_token)
    50. print("Group IDs:", group_ids) #即为access_token

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

    1. import requests
    2. url = 'https://api.dingtalk.com/v1.0/robot/groupMessages/send'
    3. headers = {
    4. 'Content-Type': 'application/json',
    5. 'x-acs-dingtalk-access-token': '换成你自己的'
    6. }
    7. data = {
    8. "msgParam": '{"content":"对应要删的消息"}',
    9. "msgKey": "sampleText",
    10. "openConversationId": "换成你自己的",
    11. "robotCode": "换成你自己的"
    12. }
    13. response = requests.post(url, headers=headers, json=data)
    14. if response.status_code == 200:
    15. result = response.json()
    16. process_query_key = result.get("processQueryKey")
    17. print("processQueryKey:", process_query_key)
    18. else:
    19. print("Request failed with status code:", response.status_code)
    20. print("Error:", response.text)

    第五步:进行消息撤回

    1. import requests
    2. def recall_message(access_token, open_conversation_id, robot_code, message_ids):
    3. url = f"https://api.dingtalk.com/v1.0/robot/groupMessages/recall?x-acs-dingtalk-access-token={access_token}"
    4. data = {
    5. "openConversationId": open_conversation_id,
    6. "robotCode": robot_code,
    7. "processQueryKeys": message_ids
    8. }
    9. headers = {
    10. "Content-Type": "application/json"
    11. }
    12. try:
    13. response = requests.post(url, json=data, headers=headers)
    14. if response.status_code == 200:
    15. print("消息撤回成功")
    16. else:
    17. print("消息撤回失败")
    18. # 打印失败理由
    19. print(response.text)
    20. except requests.exceptions.RequestException as err:
    21. print(f"请求发生错误: {err}")
    22. # 设置访问令牌、群会话ID、机器人代码和要撤回的消息ID列表
    23. access_token = "your access token"
    24. open_conversation_id = "XXXX"
    25. robot_code = "XXXX"
    26. message_ids = ["String"]
    27. # 调用撤回消息函数
    28. recall_message(access_token, open_conversation_id, robot_code, message_ids)

  • 相关阅读:
    软件工程期末复习
    Java开发从入门到精通(五):JDK9-JDK16 新特性
    LabVIEW将 VI 升级到较新的版本和恢复为先前版本
    基于postgis实现坐标转换的几个函数
    parallelStream并行流性能
    零基础小白学习 Java 要经历的阶段!
    docker network create命令
    Jetson 系列(二十九) Nebula Graph
    56资源网系统源码搭建知识付费-含源码
    模拟器抓HTTP/S的包时如何绕过单向证书校验(XP框架)
  • 原文地址:https://blog.csdn.net/EaSoNgo111/article/details/132881516