Golang三行代碼發送釘釘機器人消息

dingtalk

釘釘機器人消息封裝,三行代碼發送釘釘消息——Golang實現,簡化日常通過釘釘羣機器人發送提示、報警消息操作。

目前自定義機器人支持

  • 文本(text)
  • 鏈接(link)
  • markdown
  • ActionCard
    • 整體跳轉
    • 獨立跳轉
  • FeedCard

github傳送門
機器人官方文檔

使用

創建釘釘羣機器人

  1. 選擇添加自定義機器人。
  2. 安全設置
    共有關鍵詞、加簽、IP白名單三種設置,需要根據情況進行選擇。
    img-t9jV9hxO-1593936340184
  3. 選擇自定義關鍵詞,這裏設置的關鍵詞在初始化機器人的時候會用到。

獲取

  • go get github.com/blinkbean/dingtalk
    

初始化

  • // key 創建釘釘機器人需要設置的關鍵詞,默認爲`.`
    func InitDingTalk(tokens []string, key string) *dingTalk
    
  • import "github.com/blinkbean/dingtalk"
    
    func main() {
        // 單個機器人有單位時間內消息條數的限制,如果有需要可以初始化多個token,發消息時隨機發給其中一個機器人。
        var dingToken = []string{"7bd675b66646ba890046c2198257576470099e1bda0770bad7dd6684fb1e0415"}
        cli := dingtalk.InitDingTalk(dingToken, ".")
        cli.SendTextMessage("content")
    }
    

text類型

  • 方法及可選參數
    // 方法定義
    SendTextMessage(content string, opt ...atOption) error
    
    // 可選參數
    // @所有人
    WithAtAll()
    
    // @指定羣成員
    WithAtMobiles(mobiles []string)
    
  • 使用
    // at所有人
    cli.SendTextMessage("content", WithAtAll())
    
    // at指定羣成員
    mobiles := []string{"131********"}
    cli.SendTextMessage("content", WithAtMobiles(mobiles))
    
  • img-crg4eld0-1593936340187

link類型

  • 方法
    // 方法定義
    SendLinkMessage(title, text, picUrl, msgUrl string) error
    
  • 使用
    cli.SendLinkMessage(title, text, picUrl, msgUrl)
    
  • Xnip2020-07-05_10-25-33.jpg

markdown類型

  • 方法及可選參數
    // 方法定義
    // text:markdown格式字符串
    SendMarkDownMessage(title, text string, opts ...atOption) error
    
    // 可選參數 目前釘釘markdown格式消息不支持@(可能是釘釘的bug),所以以下可選參數暫時不生效。
    // @所有人
    WithAtAll()
    
    // @指定羣成員
    WithAtMobiles(mobiles []string)
    
  • 使用
    cli.SendMarkDownMessage(title, text)
    
  • img-Nh1ID6bj-1593936340193

整體跳轉ActionCard類型

  • 方法及可選參數
    // 方法定義
    SendActionCardMessage(title, text string, opts ...actionCardOption) error
    
    // 可選參數
    // 標題
    WithCardSingleTitle(title string)
    
    // 跳轉地址
    WithCardSingleURL(url string)
    
  • 使用
    cli.SendActionSingleMessage(title, text, WithCardSingleTitle(sTitle), WithCardSingleURL(url))
    
  • [外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-XB6PGmk7-1593936340194)(https://i.loli.net/2020/07/05/kKELHAlomndiO9I.jpg)]

獨立跳轉ActionCard類型

  • 方法及可選參數
    // 方法定義
    SendActionCardMessage(title, text string, opts ...actionCardOption) error
    
    // 可選參數
    // 按鈕排列方向,默認水平
    WithCardBtnVertical()
    
    // 跳轉按鈕
    WithCardBtns(btns []ActionCardMultiBtnModel)
    
    // ActionCardMultiBtnModel
    type ActionCardMultiBtnModel struct {
    	Title     string `json:"title,omitempty"`
    	ActionURL string `json:"actionURL,omitempty"`
    }
    
  • 使用
    btns := []ActionCardMultiBtnModel{{
        Title:     "test1",
        ActionURL: testUrl,
        },{
        Title:     "test2",
        ActionURL: testUrl,
        },
    }
    cli.SendActionSingleMessage(title, text, WithCardBtns(btns))
    
  • Xnip2020-07-05_10-29-21.jpg

FeedCard類型

  • 方法
    // 方法定義
    SendFeedCardMessage(feedCard []FeedCardLinkModel) error
    
    // FeedCardLinkModel
    type FeedCardLinkModel struct {
    	Title      string `json:"title,omitempty"`
    	MessageURL string `json:"messageURL,omitempty"`
    	PicURL     string `json:"picURL,omitempty"`
    }
    
  • 使用
    links := []FeedCardLinkModel{
        {
            Title:      "FeedCard1.",
            MessageURL: testUrl,
            PicURL:     testImg,
        },
        {
            Title:      "FeedCard2",
            MessageURL: testUrl,
            PicURL:     testImg,
        },
        {
            Title:      "FeedCard3",
            MessageURL: testUrl,
            PicURL:     testImg,
        },
    }
    cli.SendFeedCardMessage(links)
    
  • Xnip2020-07-05_10-30-02.jpg
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章