dingtalkrobot

package
v1.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 13 Imported by: 0

README

dingtalk

钉钉机器人消息封装——Golang

目前自定义机器人支持

  • 文本(text)
  • 链接(link)
  • markdown
  • ActionCard
    • 整体跳转
    • 独立跳转
  • FeedCard

机器人官方文档

  • 添加钉钉群:XXXX,执行dingtalk_test.go测试方法可直接查看以下消息内容。

使用

创建钉钉群机器人
  1. 选择添加自定义机器人。
  2. 安全设置 共有关键词、加签、IP白名单三种设置,需要根据情况进行选择。 Xnip2020-07-05_15-55-24.jpg
  3. 选择自定义关键词,这里设置的关键词在初始化机器人的时候会用到。
获取
  • go get gitee.com/lorock/go-dingtalk-sdk/dingtalk
    
初始化
  • // key 创建钉钉机器人需要设置的关键词,默认为`.`
    func InitDingTalk(tokens []string, key string) *dingTalk
    
  • import "gitee.com/lorock/go-dingtalk-sdk/dingtalk"
    
    func main() {
        // 单个机器人有单位时间内消息条数的限制,如果有需要可以初始化多个token,发消息时随机发给其中一个机器人。
        var dingToken = []string{"XXXXXX"}
        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))
    
  • Xnip2020-07-05_10-46-59.jpg
  • 方法
    // 方法定义
    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)
    
  • Markdown进阶

    // 按行写入数组,增强markdown的可读性
    msg := []string{
        "### Link test",
        "---",
        "- <font color=#00ff00 size=6>红色文字</font>",
        "- content2",
    }
    cli.SendMarkDownMessageBySlice("Markdown title", msg, WithAtAll())
    
    // 字体及颜色
    dm := DingMap()
    dm.Set("颜色测试", H2)
    dm.Set("失败:$$ 同行不同色 $$", RED)  // 双$分隔
    dm.Set("---", N)
    dm.Set("金色", GOLD)
    dm.Set("成功", GREEN)
    dm.Set("警告", BLUE)
    dm.Set("普通文字", N)
    cli.SendMarkDownMessageBySlice("color test", dm.Slice())
    
  • Xnip2020-07-05_10-27-33.jpg

  • Xnip2020-07-26_17-14-40.jpg

  • 点击链接发送消息

    点击'dtmdLink1',自动发送'dtmdValue1'并@机器人,简化输入

    // 创建有序map
    dtmdOrderMap := DingMap().
        Set("dtmdOrderMap1", "dtmdValue1").
        Set("dtmdOrderMap2", "dtmdValue2").
        Set("dtmdOrderMap3", "dtmdValue3")
    err := dingTalkCli.SendDTMDMessage("DTMD title", dtmdOrderMap)
    
  • Xnip2020-11-02_17-17-26.jpg

整体跳转ActionCard类型
  • 方法及可选参数
    // 方法定义
    SendActionCardMessage(title, text string, opts ...ActionCardOption) error
    
    // 可选参数
    // 标题
    WithCardSingleTitle(title string)
    
    // 跳转地址
    WithCardSingleURL(url string)
    
  • 使用
    cli.SendActionSingleMessage(title, text, WithCardSingleTitle(sTitle), WithCardSingleURL(url))
    
  • Xnip2020-07-05_10-28-57.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
  • Xnip2020-07-26_17-14-56.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
OutGoing
  • 消息格式

    {
        "atUsers":[
            {
                "dingtalkId":"$:LWCP_v1:$1h0bmSzcLCHncx0lCt3Bb/UVz7xv/8vh*"
            }],
        "chatbotUserId":"$:LWCP_v1:$1h0bmSzcLCHncx0lCt3Bb/UVz7x/8vh*",
        "conversationId":"cidkkCwvtlh1L0RmFuhmashi*==",
        "conversationTitle":"项目群",
        "conversationType":"2",
        "createAt":1595232438950,
        "isAdmin":false,
        "isInAtList":true,
        "msgId":"msgm/bJkKjTupFM7ZoRF/eKR*==",
        "msgtype":"text",
        "sceneGroupCode":"project",
        "senderId":"$:LWCP_v1:$x4wFOct/DGctv96o4IxxB*==",
        "senderNick":"blinkbean",
        "sessionWebhook":"https://oapi.dingtalk.com/robot/sendBySession?session=xxxxxx",
        "sessionWebhookExpiredTime":1595237839030,
        "text":{
            "content":" outgoing"
        }
    }
    
  • Usage

    func OutGoing(ctx *gin.Context){
        cli := dingtalk.InitDingTalk([]string{"***"}, ".")
        msg, _ := cli.OutGoing(ctx.Request.Body)
        // 处理content
        res := doSomeThing(msg.Text.Content)
    
        textMsg := dingtalk.NewTextMsg(res)
        ctx.Set("respData", textMsg)
        ctx.JSON(ctx.Writer.Status(), data)
        return 
    }
    

Documentation

Index

Constants

View Source
const (
	// TEXT 文本(text)
	TEXT msgTypeType = "text"
	// LINK 链接(link)
	LINK msgTypeType = "link"
	// MARKDOWN MARKDOWN
	MARKDOWN msgTypeType = "markdown"
	// ACTIONCARD ActionCard 整体跳转 独立跳转
	ACTIONCARD msgTypeType = "actionCard"
	// FEEDCARD FEEDCARD
	FEEDCARD msgTypeType = "feedCard"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionCardMsg

type ActionCardMsg struct {
	MsgType    msgTypeType     `json:"msgtype,omitempty"`
	ActionCard actionCardModel `json:"actionCard,omitempty"`
}

ActionCardMsg ActionCardMsg

func NewActionCardMsg

func NewActionCardMsg(title, text string, opts ...ActionCardOption) *ActionCardMsg

NewActionCardMsg NewActionCardMsg

func (ActionCardMsg) Marshaler

func (a ActionCardMsg) Marshaler() []byte

Marshaler Marshaler

type ActionCardMultiBtnModel

type ActionCardMultiBtnModel struct {
	Title     string `json:"title,omitempty"`
	ActionURL string `json:"actionURL,omitempty"`
}

ActionCardMultiBtnModel ActionCardMultiBtnModel

type ActionCardOption

type ActionCardOption interface {
	// contains filtered or unexported methods
}

ActionCardOption ActionCardOption

func WithCardBtnVertical

func WithCardBtnVertical() ActionCardOption

WithCardBtnVertical WithCardBtnVertical

func WithCardBtns

func WithCardBtns(btns []ActionCardMultiBtnModel) ActionCardOption

WithCardBtns WithCardBtns

func WithCardSingleTitle

func WithCardSingleTitle(title string) ActionCardOption

WithCardSingleTitle WithCardSingleTitle

func WithCardSingleURL

func WithCardSingleURL(url string) ActionCardOption

WithCardSingleURL WithCardSingleURL

type AtOption

type AtOption interface {
	// contains filtered or unexported methods
}

AtOption atOption

func WithAtAll

func WithAtAll() AtOption

WithAtAll WithAtAll

func WithAtMobiles

func WithAtMobiles(mobiles []string) AtOption

WithAtMobiles WithAtMobiles

type DingMap

type DingMap struct {
	// contains filtered or unexported fields
}

DingMap 有序map

func DingTalkMap

func DingTalkMap() *DingMap

DingTalkMap DingMap

func (*DingMap) Remove

func (d *DingMap) Remove(val string)

Remove Remove

func (*DingMap) Set

func (d *DingMap) Set(val string, t MarkType) *DingMap

Set Set

func (*DingMap) Slice

func (d *DingMap) Slice() []string

Slice Slice

type DingTalk

type DingTalk struct {
	// contains filtered or unexported fields
}

DingTalk DingTalk

func InitDingTalk

func InitDingTalk(tokens []string, key string) *DingTalk

InitDingTalk InitDingTalk

func (*DingTalk) OutGoing

func (d *DingTalk) OutGoing(r io.Reader) (outGoingMsg OutGoingModel, err error)

OutGoing OutGoing

func (*DingTalk) SendActionCardMessage

func (d *DingTalk) SendActionCardMessage(title, text string, opts ...ActionCardOption) error

SendActionCardMessage SendActionCardMessage

func (*DingTalk) SendActionCardMessageBySlice

func (d *DingTalk) SendActionCardMessageBySlice(title string, textList []string, opts ...ActionCardOption) error

SendActionCardMessageBySlice SendActionCardMessageBySlice

func (*DingTalk) SendDTMDMessage

func (d *DingTalk) SendDTMDMessage(title string, dtmdMap *DingMap, opt ...AtOption) error

SendDTMDMessage 利用dtmd发送点击消息

func (*DingTalk) SendFeedCardMessage

func (d *DingTalk) SendFeedCardMessage(feedCard []FeedCardLinkModel) error

SendFeedCardMessage SendFeedCardMessage

func (*DingTalk) SendLinkMessage

func (d *DingTalk) SendLinkMessage(title, text, picURL, msgURL string) error

SendLinkMessage SendLinkMessage

func (*DingTalk) SendMarkDownMessage

func (d *DingTalk) SendMarkDownMessage(title, text string, opts ...AtOption) error

SendMarkDownMessage SendMarkDownMessage

func (DingTalk) SendMarkDownMessageBySlice

func (d DingTalk) SendMarkDownMessageBySlice(title string, textList []string, opts ...AtOption) error

SendMarkDownMessageBySlice SendMarkDownMessageBySlice

func (*DingTalk) SendTextMessage

func (d *DingTalk) SendTextMessage(content string, opt ...AtOption) error

SendTextMessage SendTextMessage

type FeedCardLinkModel

type FeedCardLinkModel struct {
	Title      string `json:"title,omitempty"`
	MessageURL string `json:"messageURL,omitempty"`
	PicURL     string `json:"picURL,omitempty"`
}

FeedCardLinkModel FeedCardLinkModel

type FeedCardMsg

type FeedCardMsg struct {
	MsgType  msgTypeType   `json:"msgtype,omitempty"`
	FeedCard feedCardModel `json:"feedCard,omitempty"`
}

FeedCardMsg FeedCardMsg

func NewFeedCardMsg

func NewFeedCardMsg(feedCard []FeedCardLinkModel) *FeedCardMsg

NewFeedCardMsg NewFeedCardMsg

func (FeedCardMsg) Marshaler

func (f FeedCardMsg) Marshaler() []byte

Marshaler Marshaler

type LinkMsg

type LinkMsg struct {
	MsgType msgTypeType `json:"msgtype,omitempty"`
	Link    linkModel   `json:"link,omitempty"`
}

LinkMsg linkMsg

func NewLinkMsg

func NewLinkMsg(title, text, picURL, msgURL string) *LinkMsg

NewLinkMsg NewLinkMsg

func (LinkMsg) Marshaler

func (l LinkMsg) Marshaler() []byte

Marshaler Marshaler

type MarkDownMsg

type MarkDownMsg struct {
	MsgType  msgTypeType   `json:"msgtype,omitempty"`
	Markdown markDownModel `json:"markdown,omitempty"`
	At       atModel       `json:"at,omitempty"`
}

MarkDownMsg MarkDownMsg

func NewDTMDMsg

func NewDTMDMsg(title string, dtmdMap *DingMap, opts ...AtOption) *MarkDownMsg

NewDTMDMsg NewDTMDMsg

func NewMarkDownMsg

func NewMarkDownMsg(title string, text interface{}, opts ...AtOption) *MarkDownMsg

NewMarkDownMsg NewMarkDownMsg

func (MarkDownMsg) Marshaler

func (m MarkDownMsg) Marshaler() []byte

Marshaler Marshaler

type MarkType

type MarkType string

MarkType MarkType

const (
	// H1 H1
	H1 MarkType = "h1"
	// H2 H2
	H2 MarkType = "h2"
	// H3 H3
	H3 MarkType = "h3"
	// H4 H4
	H4 MarkType = "h4"
	// H5 H5
	H5 MarkType = "h5"
	// H6 H6
	H6 MarkType = "h6"
	// RED RED
	RED MarkType = "red"
	// BLUE BLUE
	BLUE MarkType = "blue"
	// GREEN GREEN
	GREEN MarkType = "green"
	// GOLD GOLD
	GOLD MarkType = "gold"
	// N N
	N MarkType = ""
)

type OutGoingModel

type OutGoingModel struct {
	AtUsers []struct {
		DingtalkID string `json:"dingtalkId"`
	} `json:"atUsers"`
	ChatbotUserID             string `json:"chatbotUserId"`
	ConversationID            string `json:"conversationId"`
	ConversationTitle         string `json:"conversationTitle"`
	ConversationType          string `json:"conversationType"`
	CreateAt                  int64  `json:"createAt"`
	IsAdmin                   bool   `json:"isAdmin"`
	IsInAtList                bool   `json:"isInAtList"`
	MsgID                     string `json:"msgId"`
	Msgtype                   string `json:"msgtype"`
	SceneGroupCode            string `json:"sceneGroupCode"`
	SenderID                  string `json:"senderId"`
	SenderNick                string `json:"senderNick"`
	SessionWebhook            string `json:"sessionWebhook"`
	SessionWebhookExpiredTime int64  `json:"sessionWebhookExpiredTime"`
	Text                      struct {
		Content string `json:"content"`
	} `json:"text"`
}

OutGoingModel OutGoingModel

type TextMsg

type TextMsg struct {
	MsgType msgTypeType `json:"msgtype,omitempty"`
	Text    textModel   `json:"text,omitempty"`
	At      atModel     `json:"at,omitempty"`
}

TextMsg TextMsg

func NewTextMsg

func NewTextMsg(content string, opts ...AtOption) *TextMsg

NewTextMsg NewTextMsg

func (TextMsg) Marshaler

func (t TextMsg) Marshaler() []byte

Marshaler Marshaler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL