筆記.事務(wù).釘釘事件綁定Go
釘釘平臺(tái)開(kāi)放事件訂閱的功能,也就是釘釘平臺(tái)會(huì)推送你所訂閱的事件,像是部門變更、人員簽到、群會(huì)話變動(dòng)。
在釘釘開(kāi)放平臺(tái)中——我的后臺(tái)——應(yīng)用開(kāi)發(fā)中,選擇你的釘釘應(yīng)用,在其中的事件與回調(diào)中使用事件訂閱功能。
[圖片上傳失敗...(image-cbebec-1681697019025)]
加密aes_key
和簽名token
可以隨機(jī)生成也可以自己配置,填寫(xiě)請(qǐng)求網(wǎng)址
(公網(wǎng)IP)后點(diǎn)擊保存
釘釘平臺(tái)會(huì)將加密aes_key
和簽名token
加密成一個(gè)名為encrypt
的字段包含在一個(gè)application/json
文件中POST向你的請(qǐng)求網(wǎng)址并且會(huì)攜帶signature
、timestamp
、nonce
三個(gè)參數(shù)
<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://你注冊(cè)的HTTP地址?signature=111108bb8e6dbc2xxxx×tamp=1783610513&nonce=380320111</pre>
說(shuō)明
signature:簽名
timestamp:時(shí)間戳
nonce:隨機(jī)數(shù)
encrypt:密文
解密方法在此不詳細(xì)說(shuō),釘釘平臺(tái)給出了完整流程的demo——dingtalk-callback-Crypto
在Go-Demo中,具體使用流程如下
接受到如上四個(gè)參數(shù)后,首先使用我們配置的加密aes_key
和簽名token
以及appkey
(即小程序或應(yīng)用的唯一標(biāo)識(shí))應(yīng)用類型不同不一定是appkey
[圖片上傳失敗...(image-9d1598-1681697019025)]
所有準(zhǔn)備好后開(kāi)始解密,具體代碼如下:
<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. 參數(shù)獲取
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. 參數(shù)解密
callbackCrypto := dingding.NewDingTalkCrypto("token", "AESKey", "appkey")
decryptMsg, _ := callbackCrypto.GetDecryptMsg(signature, timestamp, nonce, m["encrypt"].(string))
// 3. 反序列化回調(diào)事件json數(shù)據(jù)
eventJson := make(map[string]interface{})
json.Unmarshal([]byte(decryptMsg), &eventJson)
eventType := eventJson["EventType"].(string)
subscription := dingding.NewDingSubscribe(eventJson)
// 4.根據(jù)EventType分類處理
if eventType == "check_url" {
// 測(cè)試回調(diào)url的正確性,主要用于首次
zap.L().Info("測(cè)試回調(diào)url的正確性\n")
} else if eventType == "其他事件字段" {
zap.L().Info("發(fā)生了:" + eventType + "事件")
// 處理事件
}
// 5. 返回包含success的加密數(shù)據(jù)
successMap, _ := callbackCrypto.GetEncryptMsg("success")
c.JSON(http.StatusOK, successMap)
}</pre>
獲取鏈接參數(shù)并綁定json包到變量m上
使用
token
,AESKey
,appkey
做初次驗(yàn)證,將signature
,timestamp
,nonce
,encrypt
傳入GetDecryptMsg
函數(shù)進(jìn)行解密返回解密數(shù)據(jù)反序列化json數(shù)據(jù)后即可進(jìn)行事件處理,此json數(shù)據(jù)中包含
EventType
事件類型以及此其他你所需求的參數(shù),具體見(jiàn)釘釘?shù)?a target="_blank">事件訂閱匯總清單根據(jù)獲得的
EventType
對(duì)json數(shù)據(jù)做出不同處理,像是第一次事件訂閱配置時(shí),獲得的EventType
是check_url
,之后像是用戶加入企業(yè)(架構(gòu))之中則為user_add_org
最后返回包含
success
的加密數(shù)據(jù)即可
釘釘?shù)氖录嗛喆钆淦渌麘?yīng)用能夠做到很多事情,配合釘釘企業(yè)機(jī)器人的開(kāi)發(fā)能夠?qū)崟r(shí)通知一些事情,同時(shí)也能夠?qū)崟r(shí)地更新服務(wù)器的數(shù)據(jù)庫(kù)
但是需要注意,點(diǎn)擊保存之后釘釘平臺(tái)返回的錯(cuò)誤信息中不會(huì)返回你的接口返回的報(bào)錯(cuò)信息,也就是你大概不會(huì)知道是如何錯(cuò)的,也許是直到你將程序過(guò)一遍之后發(fā)現(xiàn)使用你的接口需要驗(yàn)證token,而釘釘平臺(tái)不會(huì)告訴你這個(gè)。