1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| import requests
def getAccessToken(AppKey,AppSecret): url = r'https://oapi.dingtalk.com/gettoken?appkey={}&appsecret={}'.format(AppKey,AppSecret) response = requests.get(url) return response.json().get('access_token') def sendMsg(access_token,json): url = r'https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={}'.format(access_token) response = requests.post(url,data=json) return response.json() def recallMsg(access_token,task_id,agent_id): url = r'https://oapi.dingtalk.com/topapi/message/corpconversation/recall?access_token={}'.format(access_token) msg = { "msg_task_id":task_id, "agent_id":agent_id } responce = requests.post(url,msg) return responce.json()
if __name__ == "__main__": agent_id = '复制的AgentId' AppKey = '复制的AppKey' AppSecret = '复制的AppSecret' access_token = getAccessToken(AppKey = AppKey,AppSecret= AppSecret) msg = '''{ "msg":{ "text":{ "content":"我是数字员工。高效办公、事项提醒、消息通知一网打尽。" }, "markdown":{ "text":"我是数字员工\n + 高效办公\n+ 事项提醒\n + 消息通知 \n\n如有需要可联系金融科技部 ", "title":"提醒" },
"action_card":{ "btn_json_list":{ "action_url":"http://www.baidu.com", "title":"title2" }, "single_url":"https://www.toutiao.com/article/7249711322559185448/?log_from=0a46e2448d138_1688028529113", "btn_orientation":"0", "single_title":"海上风电机组成功吊装", "markdown":"今日热点", "title":"测试" }, "link":{ "picUrl":"https://img1.imgtp.com/2023/06/29/1cZT9PvV.jpg", "messageUrl":"http://www.baidu.com", "text":"万众一心,破浪而行", "title":"端午安康" }, "voice":{ "duration":"100", "media_id":"100" }, "image":{ "media_id":"@123" }, "file":{ "media_id":"media_id" }, "oa":{ "head":{ "bgcolor":"bgcolor", "text":"text" }, "pc_message_url":"http://www.baidu.com", "status_bar":{ "status_value":"进行中", "status_bg":"0xFFF65E5E" }, "body":{ "file_count":"2", "image":"https://img1.imgtp.com/2023/06/29/1cZT9PvV.jpg", "form":[{ "value":"小智", "key":"姓名:" },{ "value":"1", "key":"年龄:" },{ "value":"RPA、人工智能", "key":"能力:" }], "author":"陈迪", "rich":{ "unit":"大领域能力", "num":"5" }, "title":"简介", "content":"" }, "message_url":"message_url" },
"msgtype":"markdown" }, "to_all_user":"false", "agent_id":"复制的AgentId", "userid_list":"683" } ''' result = sendMsg(access_token,msg.encode('utf-8'))
print(result)
|