這篇文章將為大家詳細(xì)講解有關(guān)nodejs通過釘釘群機(jī)器人推送消息,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
實(shí)現(xiàn)
代碼是 ts 實(shí)現(xiàn)的,用了 request 發(fā)起http請求,具體參數(shù)參考釘釘官方文檔,只實(shí)現(xiàn)了文本消息的推送,其它消息類似,再進(jìn)行一層封裝,實(shí)現(xiàn)代碼如下:
import * as request from "request"; import * as log4js from "log4js"; const logger = log4js.getLogger("DingdingBot"); const ApplicationTypeHeader:string = "application/json;charset=utf-8"; // DingdingBot // https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq export class DingdingBot{ private readonly _webhookUrl:string; constructor(webhookUrl:string){ this._webhookUrl = webhookUrl; } public pushMsg (msg: string, atMobiles?: Array<string>): boolean{ try { let options: request.CoreOptions = { headers: { "Content-Type": ApplicationTypeHeader }, json: { "msgtype": "text", "text": { "content": msg }, "at": { "atMobiles": atMobiles == null ? [] : atMobiles, "isAtAll": false } } }; request.post(this._webhookUrl, options, function(error, response, body){ logger.debug(`push msg ${msg}, response: ${JSON.stringify(body)}`); }); } catch(err) { console.error(err); return false; } } }
使用方式:
// botWebhookUrl 為對應(yīng)釘釘機(jī)器人的 webhook 地址 let bot = new DingdingBot(botWebhookUrl);; // 直接推送消息 bot.pushMsg("測試消息"); // 推送消息并 @ 某些人 var mobiles = new Array<string>(); mobiles.push("13255573334"); bot.pushMsg("測試消息并@", mobiles);
關(guān)于nodejs通過釘釘群機(jī)器人推送消息就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前名稱:nodejs通過釘釘群機(jī)器人推送消息-創(chuàng)新互聯(lián)
轉(zhuǎn)載注明:http://chinadenli.net/article2/dggpoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站營銷、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容