溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nodejs通過釘釘群機(jī)器人推送消息

發(fā)布時間:2021-06-03 16:29:25 來源:億速云 閱讀:365 作者:Leah 欄目:web開發(fā)

這篇文章將為大家詳細(xì)講解有關(guān)nodejs通過釘釘群機(jī)器人推送消息,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

實現(xiàn)

代碼是 ts 實現(xiàn)的,用了 request 發(fā)起http請求,具體參數(shù)參考釘釘官方文檔,只實現(xiàn)了文本消息的推送,其它消息類似,再進(jìn)行一層封裝,實現(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é)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI