笔记.事务.钉钉事件绑定Go

笔记.事务.钉钉事件绑定Go

钉钉平台开放事件订阅的功能,也就是钉钉平台会推送你所订阅的事件,像是部门变更、人员签到、群会话变动。

在钉钉开放平台中——我的后台——应用开发中,选择你的钉钉应用,在其中的事件与回调中使用事件订阅功能。

[图片上传失败...(image-cbebec-1681697019025)]

加密aes_key签名token可以随机生成也可以自己配置,填写请求网址(公网IP)后点击保存

钉钉平台会将加密aes_key签名token加密成一个名为encrypt的字段包含在一个application/json文件中POST向你的请求网址并且会携带signaturetimestampnonce三个参数

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="json" cid="n16" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(21, 45, 62); position: relative !important; -webkit-font-smoothing: antialiased; text-rendering: optimizelegibility; width: inherit; border-radius: 3px; border: 1px solid rgb(16, 36, 50); padding: 10px; color: rgb(219, 240, 239); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">{
"encrypt": "ajls384kdjx98XX" // 加密字符串
}</pre>

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="http" cid="n14" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(21, 45, 62); position: relative !important; -webkit-font-smoothing: antialiased; text-rendering: optimizelegibility; width: inherit; border-radius: 3px; border: 1px solid rgb(16, 36, 50); padding: 10px; color: rgb(219, 240, 239); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">http://你注册的HTTP地址?signature=111108bb8e6dbc2xxxx&timestamp=1783610513&nonce=380320111</pre>

说明

signature:签名

timestamp:时间戳

nonce:随机数

encrypt:密文

解密方法在此不详细说,钉钉平台给出了完整流程的demo——dingtalk-callback-Crypto

在Go-Demo中,具体使用流程如下

接受到如上四个参数后,首先使用我们配置的加密aes_key签名token以及appkey(即小程序或应用的唯一标识)应用类型不同不一定是appkey

[图片上传失败...(image-9d1598-1681697019025)]

所有准备好后开始解密,具体代码如下:

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="go" cid="n54" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(21, 45, 62); position: relative !important; -webkit-font-smoothing: antialiased; text-rendering: optimizelegibility; width: inherit; border-radius: 3px; border: 1px solid rgb(16, 36, 50); padding: 10px; color: rgb(219, 240, 239); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">func SubscribeTo(c *gin.Context) {
// 1. 参数获取
signature := c.Query("signature")
timestamp := c.Query("timestamp")
nonce := c.Query("nonce")
var m map[string]interface{}
if err := c.ShouldBindJSON(&m); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}

// 2. 参数解密
callbackCrypto := dingding.NewDingTalkCrypto("token", "AESKey", "appkey")
decryptMsg, _ := callbackCrypto.GetDecryptMsg(signature, timestamp, nonce, m["encrypt"].(string))

// 3. 反序列化回调事件json数据
eventJson := make(map[string]interface{})
json.Unmarshal([]byte(decryptMsg), &eventJson)
eventType := eventJson["EventType"].(string)
subscription := dingding.NewDingSubscribe(eventJson)

// 4.根据EventType分类处理
if eventType == "check_url" {
// 测试回调url的正确性,主要用于首次
zap.L().Info("测试回调url的正确性\n")
} else if eventType == "其他事件字段" {
zap.L().Info("发生了:" + eventType + "事件")
// 处理事件
}

// 5. 返回包含success的加密数据
successMap, _ := callbackCrypto.GetEncryptMsg("success")
c.JSON(http.StatusOK, successMap)
}</pre>

  1. 获取链接参数并绑定json包到变量m上

  2. 使用token, AESKey, appkey做初次验证,将signature,timestamp,nonce, encrypt传入GetDecryptMsg函数进行解密返回解密数据

  3. 反序列化json数据后即可进行事件处理,此json数据中包含EventType事件类型以及此其他你所需求的参数,具体见钉钉的事件订阅汇总清单

  4. 根据获得的EventType对json数据做出不同处理,像是第一次事件订阅配置时,获得的EventTypecheck_url,之后像是用户加入企业(架构)之中则为user_add_org

  5. 最后返回包含success的加密数据即可

钉钉的事件订阅搭配其他应用能够做到很多事情,配合钉钉企业机器人的开发能够实时通知一些事情,同时也能够实时地更新服务器的数据库

但是需要注意,点击保存之后钉钉平台返回的错误信息中不会返回你的接口返回的报错信息,也就是你大概不会知道是如何错的,也许是直到你将程序过一遍之后发现使用你的接口需要验证token,而钉钉平台不会告诉你这个。

原文链接:笔记.事务.钉钉事件绑定Go (pillow-blog.top)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容