技术解析

[go 库] 钉钉机器人
0
2021-06-09 17:05:12
idczone

钉钉机器人 go 库

github 地址: https://github.com/greycodee/dingbot

钉钉官方文档

快速开始

    go get github.com/greycodee/dingbot

示例程序:

package main

import (
	"fmt"
	"github.com/greycodee/dingbot"
	"github.com/greycodee/dingbot/message"
	"time"
)


func main() {
	bot:= dingbot.DingBot{
		Secret:      "你的加签秘钥",
		AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
	}
	msg := message.Message{
		MsgType: message.TextStr,
		Text:    message.Text_{
			Content: "go-钉钉机器人测试",
		},
	}
	bot.Send(msg)

}

消息支持

  • text 类型
  • link 类型
  • markdown 类型
  • 整体跳转 ActionCard 类型
  • 独立跳转 ActionCard 类型
  • FeedCard 类型

使用

发送 Text 消息

func send() {
	bot:= dingbot.DingBot{
		Secret:      "你的加签秘钥",
		AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
	}
	msg := message.Message{
        MsgType: message.TextStr,
        Text:    message.Text_{
            Content: "go-钉钉机器人测试",
            At: message.At_{
                AtMobiles: []string{"188xxxxxxxx"},
                IsAtAll:   false,
            },
        },
    }
	bot.Send(msg)

}

发送 link 类型消息

func send() {
	bot:= dingbot.DingBot{
		Secret:      "你的加签秘钥",
		AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
	}
	msg := message.Message{
        MsgType: message.LinkStr,
        Link: message.Link_{
            Text:       "link 测试 123123",
            Title:      "go 钉钉机器人",
            PicUrl:     "",
            MessageUrl: "https://developers.dingtalk.com/document/app/custom-robot-access/title-72m-8ag-pqw",
        },
    		
    }
	bot.Send(msg)

}

发送 markdown 类型消息

func send() {
	bot:= dingbot.DingBot{
		Secret:      "你的加签秘钥",
		AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
	}
	msg := message.Message{
        MsgType: message.MarkdownStr,
        Markdown: message.Markdown_{
            Title: "go 钉钉",
            Text:  "## go 钉钉测试 @188xxxxxxxx \n>文本 123",
            At:    message.At_{
                AtMobiles: []string{"188xxxxxxxx"},
                IsAtAll:   false,
            },
        },
    		
    }
	bot.Send(msg)

}

发送整体跳转 ActionCard 类型消息

func send() {
	bot:= dingbot.DingBot{
		Secret:      "你的加签秘钥",
		AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
	}
	msg := message.Message{
        MsgType: message.ActionCardStr,
        ActionCard: message.ActionCard_{
            Title:          "ActionCard 整体跳转 11",
            Text:           "ActionCardt 整体跳转 1223",
            SingleTitle:    "阅读全文",
            SingleURL:      "https://de美国服务器velopers.dingtalk.com/document/app/custom-robot-access/title-72m-8ag-pqw",
        },
    }
	bot.Send(msg)

}

发送独立跳转 ActionCard 类型消息

func send() {
    bot:= dingbot.DingBot{
        Secret:      "你的加签秘钥",
        AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
    }
    msg := message.Message{
        MsgType: message.ActionCardStr,
        ActionCard: message.ActionCard_{
            Title:          "ActionCard 跳转 11",
            Text:           "ActionCardt 跳转 1223",
            BtnOrientation: "1",
            HideAvatar:     "0",
            BtnS:           []message.Btn_{
                {
                    Title:     "按钮 1",
                    ActionURL: "https://developers.dingtalk.com/",
                },
                {
                    Title:     "按钮 2",
                    ActionURL: "https://developers.dingtalk.com/",
                },
            },
        },
    }
    bot.Send(msg)
}

发送 FeedCard 类型消息

func send() {
    bot:= dingbot.DingBot{
        Secret:      "你的加签秘钥",
        AccessToken: "你的 AccessToken  [从钉钉机器人的 url 上获取] ",
    }
    msg := message.Message{
        MsgType:  message.FeedCardStr,
        FeedCard: message.FeedCard_{[]message.Link_{
            {
                Title:      "标题 1",
                PicUrl:     "",
                MessageUrl: "https://developers.dingtalk.com/",
            },
            {
                Title:      "标题 2",
                PicUrl:     "",
                MessageUrl: "https://developers.dingtalk.com/",
            },
        }},
    }
    bot.Send(msg)
}

原文地址: [ go 库] 钉钉机器人




数据地带为您的网站提供全球顶级IDC资源
在线咨询
专属客服