dingtalk

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 9 Imported by: 1

README

dingtalk-robot

Go Reference 钉钉自定义机器人

Installation

# Go Modules

require github.com/group-robot/dingtalk-robot/v2 

Example

text
textMessage := NewTextMessage("我就是我, @XXX 是不一样的烟火")
textMessage.At = NewAt(true)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(textMessage)
linkMsg := NewLinkMessage("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林",
"时代的火车向前开",
"https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI",
)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(linkMsg)
markdown
markdownMsg := NewMarkdownMessage("杭州天气", "#### 杭州天气 @150XXXXXXXX \n > 9度,西北风1级,空气良89,相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n")
markdownMsg.At = NewAt(true)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(markdownMsg)
card
single action card
singleActionCardMsg := NewSingleActionCardMessage("乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
"![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png) \n ### 乔布斯 20 年前想打造的苹果咖啡厅 \n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
"阅读全文",
"https://www.dingtalk.com/",
)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(singleActionCardMsg)
action card
actionCardMsg := NewActionCardMessage(
"我 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身",
"![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png) \n\n #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划",
NewActionCardButton(
"内容不错",
"https://www.dingtalk.com/",
),
)
actionCardMsg.AddButtons(

NewActionCardButton(
"不感兴趣",
"https://www.dingtalk.com/",
),
)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(actionCardMsg)
feed card
feedCardMsg := NewFeedCardMessage(
NewLink(
"时代的火车向前开1",
"https://www.dingtalk.com/",
"https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png",
),
)
feedCardMsg.AddLinks(
NewLink(
"时代的火车向前开2",
"https://www.dingtalk.com/",
"https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png",
),
)
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessage(feedCardMsg)

Client

textMsg := `{"at":{"atMobiles":["180xxxxxx"],"atUserIds":["user123"],"isAtAll":false},"text":{"content":"我就是我, @XXX 是不一样的烟火"},"msgtype":"text"}`
client := NewClient()
client.Webhook = os.Getenv("webhook")
client.Secret = os.Getenv("secret")
res, err := client.SendMessageStr(textMsg)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionCardButton

type ActionCardButton struct {
	// Title 按钮标题
	Title string
	// ActionUrl 点击按钮触发的URL
	ActionUrl string
}

ActionCardButton ActionCardMessage btn

func NewActionCardButton

func NewActionCardButton(title, actionUrl string) ActionCardButton

NewActionCardButton create ActionCardButton

func (ActionCardButton) ToMessageMap

func (button ActionCardButton) ToMessageMap() map[string]interface{}

type ActionCardMessage

type ActionCardMessage struct {
	// Title 首屏会话透出的展示内容
	Title string
	// Text markdown格式的消息
	Text string
	// Buttons 按钮
	Buttons []ActionCardButton
	// BtnOrientation 按钮排布方式
	// true: 按钮横向排列
	// false: 按钮竖直排列
	BtnOrientation bool
}

ActionCardMessage 独立跳转ActionCard类型

func NewActionCardMessage

func NewActionCardMessage(title, text string, btns ...ActionCardButton) *ActionCardMessage

NewActionCardMessage create ActionCardMessage

func (*ActionCardMessage) AddButtons

func (actionCard *ActionCardMessage) AddButtons(btns ...ActionCardButton) *ActionCardMessage

AddButtons add ActionCardMessage Buttons

func (*ActionCardMessage) ToMessageMap

func (actionCard *ActionCardMessage) ToMessageMap() map[string]interface{}

type At

type At struct {
	// AtMobiles 被@人的手机号
	AtMobiles []string
	// AtUserIds 被@人的用户userid
	AtUserIds []string
	// atAll 是否@所有人
	AtAll bool
}

At dingtalk at

func NewAt

func NewAt(isAtAll bool) *At

NewAt create At

func (*At) AddAtMobiles

func (a *At) AddAtMobiles(atMobiles ...string) *At

AddAtMobiles add AtMobiles

func (*At) AddAtUserIds

func (a *At) AddAtUserIds(atUserIds ...string) *At

AddAtUserIds add AtUserIds

func (*At) SetAtMobiles

func (a *At) SetAtMobiles(atMobiles ...string) *At

SetAtMobiles set AtMobiles

func (*At) SetAtUserIds

func (a *At) SetAtUserIds(atUserIds ...string) *At

SetAtUserIds set AtUserIds

func (*At) ToMessageMap

func (a *At) ToMessageMap() map[string]interface{}

type Client

type Client struct {
	// Webhook dingtalk robot webhook
	Webhook string
	// Secret dingtalk robot secret
	Secret string
	// contains filtered or unexported fields
}

Client dingtalk robot client

func NewClient

func NewClient() *Client

NewClient create Client

func (*Client) GenSign

func (c *Client) GenSign(secret string, timestamp int64) (string, error)

GenSign sign secret

func (*Client) SendMessage

func (c *Client) SendMessage(message Message) (*Response, error)

SendMessage send message

func (*Client) SendMessageByUrl

func (c *Client) SendMessageByUrl(url string, message Message) (*Response, error)

SendMessageByUrl send message custom url

func (*Client) SendMessageStr

func (c *Client) SendMessageStr(json string) (*Response, error)

SendMessageStr send message json string

func (*Client) SendMessageStrByUrl

func (c *Client) SendMessageStrByUrl(url, json string) (*Response, error)

SendMessageStrByUrl send message custom url and json string message

type FeedCardMessage

type FeedCardMessage struct {
	// Links Link list
	Links []Link
}

FeedCardMessage FeedCard message

func NewFeedCardMessage

func NewFeedCardMessage(link ...Link) *FeedCardMessage

NewFeedCardMessage create FeedCardMessage

func (feedCard *FeedCardMessage) AddLinks(link ...Link) *FeedCardMessage

AddLinks add FeedCardMessage Links

func (*FeedCardMessage) ToMessageMap

func (feedCard *FeedCardMessage) ToMessageMap() map[string]interface{}
type Link struct {
	// Title 单条信息文本
	Title string
	// MessageUrl 点击单条信息到跳转链接
	MessageUrl string
	// PicUrl 单条信息后面图片的URL
	PicUrl string
}

Link FeedCardMessage links

func NewLink(title, messageUrl, picUrl string) Link

NewLink create Link

func (Link) ToMessageMap

func (link Link) ToMessageMap() map[string]interface{}

type LinkMessage

type LinkMessage struct {
	// Text  消息内容。如果太长只会部分展示
	Text string
	// Title 消息标题
	Title string
	// PicUrl 图片URL
	PicUrl string
	// MessageUrl 点击消息跳转的URL
	MessageUrl string
}

LinkMessage link message

func NewLinkMessage

func NewLinkMessage(text, title, messageUrl string) *LinkMessage

NewLinkMessage create LinkMessage

func (*LinkMessage) ToMessageMap

func (link *LinkMessage) ToMessageMap() map[string]interface{}

type MarkdownMessage

type MarkdownMessage struct {
	// Title  首屏会话透出的展示内容
	Title string
	// Text markdown格式的消息
	Text string
	// At @信息
	At *At
}

MarkdownMessage markdown message

func NewMarkdownMessage

func NewMarkdownMessage(title, text string) *MarkdownMessage

NewMarkdownMessage create MarkdownMessage

func (*MarkdownMessage) ToMessageMap

func (markdown *MarkdownMessage) ToMessageMap() map[string]interface{}

type Message

type Message interface {
	// ToMessageMap 返回Message对象组装出来的Map对象,供后续JSON序列化
	ToMessageMap() map[string]interface{}
}

Message dingtalk message

type MsgType

type MsgType string

MsgType message type

const (
	// TextMsgType text message Type
	TextMsgType MsgType = "text"
	// LinkMsgType link message type
	LinkMsgType MsgType = "link"
	// MarkdownMsgType markdown message type
	MarkdownMsgType MsgType = "markdown"
	// ActionCardMsgType actionCard message type
	ActionCardMsgType MsgType = "actionCard"
	// FeedCardMsgType feedCard message Type
	FeedCardMsgType MsgType = "feedCard"
)

type Response

type Response struct {
	ErrorCode    int    `json:"errcode"`
	ErrorMessage string `json:"errmsg"`
}

func (Response) Success

func (r Response) Success() bool

type SingleActionCardMessage

type SingleActionCardMessage struct {
	// Title 首屏会话透出的展示内容
	Title string
	// Text markdown格式的消息
	Text string
	// SingleTitle 单个按钮的标题
	SingleTitle string
	// SingleUrl 点击消息跳转的URL
	SingleUrl string
	// BtnOrientation 按钮排布方式
	// true: 按钮横向排列
	// false: 按钮竖直排列
	BtnOrientation bool
}

SingleActionCardMessage 整体跳转ActionCard类型

func NewSingleActionCardMessage

func NewSingleActionCardMessage(title, text, singleTitle, singleUrl string) *SingleActionCardMessage

NewSingleActionCardMessage create SingleActionCardMessage

func (*SingleActionCardMessage) ToMessageMap

func (single *SingleActionCardMessage) ToMessageMap() map[string]interface{}

type TextMessage

type TextMessage struct {
	// Content 消息内容
	Content string
	// At @信息
	At *At
}

TextMessage dingtalk text message

func NewTextMessage

func NewTextMessage(content string) *TextMessage

NewTextMessage create TextMessage

func (*TextMessage) ToMessageMap

func (text *TextMessage) ToMessageMap() map[string]interface{}

Jump to

Keyboard shortcuts

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