筆記.事務(wù).釘釘事件綁定Go

筆記.事務(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&timestamp=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>

  1. 獲取鏈接參數(shù)并綁定json包到變量m上

  2. 使用token, AESKey, appkey做初次驗證童叠,將signature,timestamp,nonce, encrypt傳入GetDecryptMsg函數(shù)進(jìn)行解密返回解密數(shù)據(jù)

  3. 反序列化json數(shù)據(jù)后即可進(jìn)行事件處理框喳,此json數(shù)據(jù)中包含EventType事件類型以及此其他你所需求的參數(shù),具體見釘釘?shù)?a target="_blank">事件訂閱匯總清單

  4. 根據(jù)獲得的EventType對json數(shù)據(jù)做出不同處理厦坛,像是第一次事件訂閱配置時五垮,獲得的EventTypecheck_url,之后像是用戶加入企業(yè)(架構(gòu))之中則為user_add_org

  5. 最后返回包含success的加密數(shù)據(jù)即可

釘釘?shù)氖录嗛喆钆淦渌麘?yīng)用能夠做到很多事情粪般,配合釘釘企業(yè)機器人的開發(fā)能夠?qū)崟r通知一些事情拼余,同時也能夠?qū)崟r地更新服務(wù)器的數(shù)據(jù)庫

但是需要注意,點擊保存之后釘釘平臺返回的錯誤信息中不會返回你的接口返回的報錯信息亩歹,也就是你大概不會知道是如何錯的匙监,也許是直到你將程序過一遍之后發(fā)現(xiàn)使用你的接口需要驗證token,而釘釘平臺不會告訴你這個小作。

原文鏈接:筆記.事務(wù).釘釘事件綁定Go (pillow-blog.top)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末亭姥,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子顾稀,更是在濱河造成了極大的恐慌达罗,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件静秆,死亡現(xiàn)場離奇詭異粮揉,居然都是意外死亡,警方通過查閱死者的電腦和手機抚笔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門扶认,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人殊橙,你說我怎么就攤上這事辐宾∮樱” “怎么了?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵叠纹,是天一觀的道長季研。 經(jīng)常有香客問我,道長誉察,這世上最難降的妖魔是什么与涡? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任,我火速辦了婚禮冒窍,結(jié)果婚禮上递沪,老公的妹妹穿的比我還像新娘豺鼻。我一直安慰自己综液,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布儒飒。 她就那樣靜靜地躺著谬莹,像睡著了一般。 火紅的嫁衣襯著肌膚如雪桩了。 梳的紋絲不亂的頭發(fā)上附帽,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機與錄音井誉,去河邊找鬼蕉扮。 笑死,一個胖子當(dāng)著我的面吹牛颗圣,可吹牛的內(nèi)容都是我干的喳钟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼在岂,長吁一口氣:“原來是場噩夢啊……” “哼奔则!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起蔽午,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤易茬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后及老,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體抽莱,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年骄恶,在試婚紗的時候發(fā)現(xiàn)自己被綠了食铐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡叠蝇,死狀恐怖璃岳,靈堂內(nèi)的尸體忽然破棺而出年缎,到底是詐尸還是另有隱情,我是刑警寧澤铃慷,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布单芜,位于F島的核電站,受9級特大地震影響犁柜,放射性物質(zhì)發(fā)生泄漏洲鸠。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一馋缅、第九天 我趴在偏房一處隱蔽的房頂上張望扒腕。 院中可真熱鬧,春花似錦萤悴、人聲如沸瘾腰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽蹋盆。三九已至,卻和暖如春硝全,著一層夾襖步出監(jiān)牢的瞬間栖雾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工伟众, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留析藕,地道東北人。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓凳厢,卻偏偏與公主長得像账胧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子数初,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容