钉钉自定义机器人开发

it2025-07-31  14

前言 

关于钉钉自定义机器人的开发流程,其官网有全面的解释,就不作赘述。章节地址如下:

https://ding-doc.dingtalk.com/document#/org-dev-guide/qf2nxq

本篇博客主要目的就是补充一下前端通过AJAX()发送请求数据的方法,对curl发送消息的方式进行整理。

 

测试机器人发送消息

其官网中详细介绍了三种测试发送消息的方法,分别是curl 、Java程序、PHP程序。在SDK环境配置章节(SDK请求示例)中对Python程序、.NET程序发送消息的方法也有简要的说明。

上述方法都需要添加对应的依赖,添加SDK环境配置章节(SDK请求示例)地址如下:https://ding-doc.dingtalk.com/doc#/faquestions/vzbp02

其中,curl的请求方式似乎并没有用,我尝试过修改但都没有成功,希望有请求成功的大佬发表一下看法。

 

AJAX测试发送消息

前端通过AJAX发送钉钉消息的方式在官网中并没有提及,因此,写本篇博客补充一下。

需要说明,该方法需要浏览器是跨域的,不然会报错。完整的HTML页面代码如下。

<!DOCTYPE html> <html> <head> <title>ajax发送钉钉消息</title> <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.3.1/jquery.min.js"></script> <script> $(function(){ console.log("=== start ==="); $.ajax({ url:'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxx', type:"POST", beforeSend:function (xhr){ xhr.setRequestHeader('Content-Type', 'application/json'); }, data:JSON.stringify({"msgtype": "text","text": {"content": "Hello World! ---大话家"}}), success:function (res){ console.log("=== success ==="); console.log(res); }, error:function (err){ console.log("=== error ==="); console.log(err); } }); }); console.log("=== end ==="); </script> </head> <body> <h1>钉钉机器人消息发送中。。。</h1> </body> </html>

 

最新回复(0)