筆記.事務(wù).釘釘事件綁定Go
釘釘平臺開放事件訂閱的功能,也就是釘釘平臺會推送你所訂閱的事件陶缺,像是部門變更、人員簽到舔株、群會話變動莺琳。
在釘釘開放平臺中——我的后臺——應(yīng)用開發(fā)中,選擇你的釘釘應(yīng)用载慈,在其中的事件與回調(diào)中使用事件訂閱功能惭等。
[圖片上傳失敗...(image-cbebec-1681697019025)]
加密aes_key
和簽名token
可以隨機生成也可以自己配置,填寫請求網(wǎng)址
(公網(wǎng)IP)后點擊保存
釘釘平臺會將加密aes_key
和簽名token
加密成一個名為encrypt
的字段包含在一個application/json
文件中POST向你的請求網(wǎng)址并且會攜帶signature
办铡、timestamp
辞做、nonce
三個參數(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://你注冊的HTTP地址?signature=111108bb8e6dbc2xxxx×tamp=1783610513&nonce=380320111</pre>
說明
signature:簽名
timestamp:時間戳
nonce:隨機數(shù)
encrypt:密文
解密方法在此不詳細(xì)說,釘釘平臺給出了完整流程的demo——dingtalk-callback-Crypto
在Go-Demo中寡具,具體使用流程如下
接受到如上四個參數(shù)后秤茅,首先使用我們配置的加密aes_key
和簽名token
以及appkey
(即小程序或應(yīng)用的唯一標(biāo)識)應(yīng)用類型不同不一定是appkey
[圖片上傳失敗...(image-9d1598-1681697019025)]
所有準(zhǔn)備好后開始解密,具體代碼如下:
<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" {
// 測試回調(diào)url的正確性,主要用于首次
zap.L().Info("測試回調(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
做初次驗證童叠,將signature
,timestamp
,nonce
,encrypt
傳入GetDecryptMsg
函數(shù)進(jìn)行解密返回解密數(shù)據(jù)反序列化json數(shù)據(jù)后即可進(jìn)行事件處理框喳,此json數(shù)據(jù)中包含
EventType
事件類型以及此其他你所需求的參數(shù),具體見釘釘?shù)?a target="_blank">事件訂閱匯總清單根據(jù)獲得的
EventType
對json數(shù)據(jù)做出不同處理厦坛,像是第一次事件訂閱配置時五垮,獲得的EventType
是check_url
,之后像是用戶加入企業(yè)(架構(gòu))之中則為user_add_org
最后返回包含
success
的加密數(shù)據(jù)即可
釘釘?shù)氖录嗛喆钆淦渌麘?yīng)用能夠做到很多事情粪般,配合釘釘企業(yè)機器人的開發(fā)能夠?qū)崟r通知一些事情拼余,同時也能夠?qū)崟r地更新服務(wù)器的數(shù)據(jù)庫
但是需要注意,點擊保存之后釘釘平臺返回的錯誤信息中不會返回你的接口返回的報錯信息亩歹,也就是你大概不會知道是如何錯的匙监,也許是直到你將程序過一遍之后發(fā)現(xiàn)使用你的接口需要驗證token,而釘釘平臺不會告訴你這個小作。