dingtalk

package module
v0.0.0-...-c2a5e85 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: MIT Imports: 13 Imported by: 4

README

dingtalk

钉钉群聊机器人,基于原项目 blinkbean/dingtalk 重构。

目前支持发送的消息类型有:

配置

创建机器人
  1. 选择添加自定义机器人。
  2. 安全设置 共有关键词、加签、IP白名单三种设置,需要根据情况进行选择。 Xnip2020-07-05_15-55-24.jpg
获取库
go get github.com/Drelf2018/dingtalk
初始化

参考机器人结构体中字段的注释,根据需要填入需要的值。

// Bot 钉钉机器人
type Bot struct {
	// 名称,可自定义
	Name string `json:"name" yaml:"name" toml:"name" long:"name"`

	// 调用接口的凭证,钉钉提供的 Webhook 链接中 access_token 的值
	Token string `json:"token" yaml:"token" toml:"token" long:"token"`

	// 安全密钥,创建机器人时在安全设置项选择了加签后,钉钉提供的 SEC 开头的字符串
	Secret string `json:"secret" yaml:"secret" toml:"secret" long:"secret"`

	// 自定义关键词,创建机器人时在安全设置项填入的所有关键词。当消息文本中不包含任何一个关键词时,会自动在文本末尾添加第一个关键词
	Keywords []string `json:"keywords" yaml:"keywords" toml:"keywords" long:"keywords"`

	// 全局请求超时时间,值为正时生效
	Timeout time.Duration `json:"timeout" yaml:"timeout" toml:"timeout" long:"timeout"`

	// 每分钟发送消息限制量,平台规定每分钟最多发送 20 条消息。如果超过限制,会限流至下一分钟零秒时刻,值为零则不限流
	Limit int `json:"limit" yaml:"limit" toml:"limit" long:"limit"`
}

使用

本库的方法参数与官方文档 自定义机器人发送消息的消息类型 保持一致,你可以在文档中查看消息类型区别、参数含义、消息样式。

SendHandler 请求前处理器
// 发送消息接口的前处理器,可以用来生成的加密签名、设置消息幂等、设置@等
type SendHandler func(*Send) error

// 内置了六个常用的处理器,可自行在代码中查看使用方法
var _ = []SendHandler{UpdateMsg[Msg](nil), Secret(""), UUID(""), AtAll, AtMobile(""), AtUserID("")}
Text 文本类型
// SendText 发送文本类型消息
func (b *Bot) SendText(content string, handlers ...SendHandler) error
// SendLink 发送链接类型消息
func (b *Bot) SendLink(title, text, msgURL, picURL string, handlers ...SendHandler) error
Markdown 类型
// SendMarkdown 发送 markdown 类型消息
func (b *Bot) SendMarkdown(title, text string, handlers ...SendHandler) error
整体跳转 ActionCard 类型
// SendActionCard 发送整体跳转 actionCard 类型消息
func (b *Bot) SendActionCard(title, text, singleTitle, singleURL string, handlers ...SendHandler) error
独立跳转 ActionCard 类型
// ActionCardBtn actionCard 类型消息的按钮
type ActionCardBtn struct {
	// 按钮上显示的文本
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// 按钮跳转的 URL
	ActionURL string `json:"actionURL" yaml:"actionURL" toml:"actionURL" long:"actionURL"`
}

// SendActionsCard 发送独立跳转 actionCard 类型消息
func (b *Bot) SendActionsCard(title, text string, btns []ActionCardBtn, handlers ...SendHandler) error
FeedCard 类型
// FeedCardLink feedCard 类型消息的内容
type FeedCardLink struct {
	// 每条内容的标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// 每条内容的跳转链接
	MessageURL string `json:"messageURL" yaml:"messageURL" toml:"messageURL" long:"messageURL"`

	// 每条内容的图片 URL ,建议使用上传媒体文件接口获取
	PicURL string `json:"picURL" yaml:"picURL" toml:"picURL" long:"picURL"`
}

// SendFeedCard 发送 feedCard 类型消息
func (b *Bot) SendFeedCard(links []FeedCardLink, handlers ...SendHandler) error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AtAll

func AtAll(s *Send) error

AtAll @所有人

func GenerateSign

func GenerateSign(secret string) (int64, string, error)

GenerateSign 生成加密时间戳和签名,加签的方式是将时间戳和密钥当做签名字符串, 开发者服务内当前系统时间戳,单位是毫秒,与请求调用时间误差不能超过 1 小时, 使用 HmacSHA256 算法计算签名,然后进行 Base64 编码,得到最终的签名

Types

type ActionCard

type ActionCard struct {
	// 消息会话列表中展示的标题,非消息体的标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// actionCard 类型消息的正文内容,支持 markdown 语法
	Text string `json:"text" yaml:"text" toml:"text" long:"text"`

	// 按钮上显示的文本
	SingleTitle string `json:"singleTitle,omitempty" yaml:"singleTitle" toml:"singleTitle" long:"singleTitle"`

	// 点击 singleTitle 按钮触发的 URL
	SingleURL string `json:"singleURL,omitempty" yaml:"singleURL" toml:"singleURL" long:"singleURL"`
}

ActionCard 整体跳转 actionCard 类型消息

func (ActionCard) Type

func (ActionCard) Type() MsgType

type ActionCardBtn

type ActionCardBtn struct {
	// 按钮上显示的文本
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// 按钮跳转的 URL
	ActionURL string `json:"actionURL" yaml:"actionURL" toml:"actionURL" long:"actionURL"`
}

ActionCardBtn actionCard 类型消息的按钮

type ActionsCard

type ActionsCard struct {
	// 消息会话列表中展示的标题,非消息体的标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// actionCard 类型消息的正文内容,支持 markdown 语法
	Text string `json:"text" yaml:"text" toml:"text" long:"text"`

	// 按钮的信息列表
	Btns []ActionCardBtn `json:"btns,omitempty" yaml:"btns" toml:"btns" long:"btns"`

	// 消息内按钮排列方式,0:按钮竖直排列,1:按钮横向排列
	BtnOrientation string `json:"btnOrientation,omitempty" yaml:"btnOrientation" toml:"btnOrientation" long:"btnOrientation"`
}

ActionsCard 独立跳转 actionCard 类型消息

func (ActionsCard) Type

func (ActionsCard) Type() MsgType

type At

type At struct {
	IsAtAll   bool     `json:"isAtAll,omitempty"`   // 是否@所有人
	AtMobiles []string `json:"atMobiles,omitempty"` // 被@的群成员手机号
	AtUserIDs []string `json:"atUserIds,omitempty"` // 被@的群成员 userId
}

At 被@的群成员信息

type Bot

type Bot struct {
	// 名称,可自定义
	Name string `json:"name" yaml:"name" toml:"name" long:"name"`

	// 调用接口的凭证,钉钉提供的 Webhook 链接中 access_token 的值
	Token string `json:"token" yaml:"token" toml:"token" long:"token"`

	// 安全密钥,创建机器人时在安全设置项选择了加签后,钉钉提供的 SEC 开头的字符串
	Secret string `json:"secret" yaml:"secret" toml:"secret" long:"secret"`

	// 自定义关键词,创建机器人时在安全设置项填入的所有关键词。当消息文本中不包含任何一个关键词时,会自动在文本末尾添加第一个关键词
	Keywords []string `json:"keywords" yaml:"keywords" toml:"keywords" long:"keywords"`

	// 全局请求超时时间,值为正时生效
	Timeout time.Duration `json:"timeout" yaml:"timeout" toml:"timeout" long:"timeout"`

	// 每分钟发送消息限制量,平台规定每分钟最多发送 20 条消息。如果超过限制,会限流至下一分钟零秒时刻,值为零则不限流
	Limit int `json:"limit" yaml:"limit" toml:"limit" long:"limit"`
	// contains filtered or unexported fields
}

Bot 钉钉机器人

func (*Bot) ContainsAnyKeyword

func (b *Bot) ContainsAnyKeyword(text string) bool

ContainsAnyKeyword 检测字符串是否包含任意一个关键词,关键词切片为空也返回真

func (*Bot) Send

func (b *Bot) Send(msg Msg, handlers ...SendHandler) error

Send 发送消息

func (*Bot) SendActionCard

func (b *Bot) SendActionCard(title, text, singleTitle, singleURL string, handlers ...SendHandler) error

SendActionCard 发送整体跳转 actionCard 类型消息

func (*Bot) SendActionCardWithContext

func (b *Bot) SendActionCardWithContext(ctx context.Context, title, text, singleTitle, singleURL string, handlers ...SendHandler) error

SendActionCardWithContext 携带上下文发送整体跳转 actionCard 类型消息

func (*Bot) SendActionsCard

func (b *Bot) SendActionsCard(title, text string, btns []ActionCardBtn, handlers ...SendHandler) error

SendActionsCard 发送独立跳转 actionCard 类型消息

func (*Bot) SendActionsCardWithContext

func (b *Bot) SendActionsCardWithContext(ctx context.Context, title, text string, btns []ActionCardBtn, handlers ...SendHandler) error

SendActionsCardWithContext 携带上下文发送独立跳转 actionCard 类型消息

func (*Bot) SendFeedCard

func (b *Bot) SendFeedCard(links []FeedCardLink, handlers ...SendHandler) error

SendFeedCard 发送 feedCard 类型消息

func (*Bot) SendFeedCardWithContext

func (b *Bot) SendFeedCardWithContext(ctx context.Context, links []FeedCardLink, handlers ...SendHandler) error

SendFeedCardWithContext 携带上下文发送 feedCard 类型消息

func (b *Bot) SendLink(title, text, msgURL, picURL string, handlers ...SendHandler) error

SendLink 发送链接类型消息

func (*Bot) SendLinkWithContext

func (b *Bot) SendLinkWithContext(ctx context.Context, title, text, msgURL, picURL string, handlers ...SendHandler) error

SendLinkWithContext 携带上下文发送链接类型消息

func (*Bot) SendMarkdown

func (b *Bot) SendMarkdown(title, text string, handlers ...SendHandler) error

SendMarkdown 发送 markdown 类型消息

func (*Bot) SendMarkdownWithContext

func (b *Bot) SendMarkdownWithContext(ctx context.Context, title, text string, handlers ...SendHandler) error

SendMarkdownWithContext 携带上下文发送 markdown 类型消息

func (*Bot) SendText

func (b *Bot) SendText(content string, handlers ...SendHandler) error

SendText 发送文本类型消息

func (*Bot) SendTextWithContext

func (b *Bot) SendTextWithContext(ctx context.Context, content string, handlers ...SendHandler) error

SendTextWithContext 携带上下文发送文本类型消息

func (*Bot) SendWithContext

func (b *Bot) SendWithContext(ctx context.Context, msg Msg, handlers ...SendHandler) error

SendWithContext 携带上下文发送消息

type FeedCard

type FeedCard struct {
	// feedCard 类型消息的内容列表
	Links []FeedCardLink `json:"links" yaml:"links" toml:"links" long:"links"`
}

FeedCard feedCard 类型消息

func (FeedCard) Type

func (FeedCard) Type() MsgType
type FeedCardLink struct {
	// 每条内容的标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// 每条内容的跳转链接
	MessageURL string `json:"messageURL" yaml:"messageURL" toml:"messageURL" long:"messageURL"`

	// 每条内容的图片 URL ,建议使用上传媒体文件接口获取
	PicURL string `json:"picURL" yaml:"picURL" toml:"picURL" long:"picURL"`
}

FeedCardLink feedCard 类型消息的内容

type Link struct {
	// 链接消息标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// 链接消息的内容
	Text string `json:"text" yaml:"text" toml:"text" long:"text"`

	// 点击消息跳转的 URL
	MessageURL string `json:"messageUrl" yaml:"messageUrl" toml:"messageUrl" long:"messageUrl"`

	// 链接消息内的图片地址,建议使用上传媒体文件接口获取
	PicURL string `json:"picUrl,omitempty" yaml:"picUrl" toml:"picUrl" long:"picUrl"`
}

Link 链接类型消息

func (Link) Type

func (Link) Type() MsgType

type Markdown

type Markdown struct {
	// 消息会话列表中展示的标题,非消息体的标题
	Title string `json:"title" yaml:"title" toml:"title" long:"title"`

	// markdown 类型消息的文本内容
	Text string `json:"text" yaml:"text" toml:"text" long:"text"`
}

Markdown markdown 类型消息

func (Markdown) Type

func (Markdown) Type() MsgType

type Msg

type Msg interface {
	Type() MsgType
}

Msg 消息接口

type MsgType

type MsgType string

MsgType 表示消息类型的字符串,已内置五种类型

MsgText       // 文本类型
MsgLink       // 链接类型,不支持@人
MsgMarkdown   // Markdown 类型
MsgActionCard // 整体跳转、独立跳转类型
MsgFeedCard   // FeedCard 类型,不支持@人
const (
	MsgText       MsgType = "text"       // 文本类型
	MsgLink       MsgType = "link"       // 链接类型,不支持@人
	MsgMarkdown   MsgType = "markdown"   // Markdown 类型
	MsgActionCard MsgType = "actionCard" // 整体跳转、独立跳转类型
	MsgFeedCard   MsgType = "feedCard"   // FeedCard 类型,不支持@人
)

type Send

type Send struct {
	// 要发送的消息
	Msg Msg

	// 自定义机器人调用接口的凭证
	AccessToken string `req:"query"`

	// 使用时间戳和密钥生成的加密签名
	Sign string `req:"query,omitempty"`

	// 开发者服务内当前系统时间戳,单位是毫秒,与请求调用时间误差不能超过 1 小时
	Timestamp int64 `req:"query,omitempty"`

	// 消息幂等,发消息时接口调用超时或未知错误等报错,开发者可使用同一个消息幂等重试,避免重复发出消息
	MsgUUID string `req:"body:msgUuid,omitempty"`

	// 被@的群成员信息
	At At `req:"body,omitempty"`

	// 请求头
	ContentType string `req:"header" default:"application/json"`
}

Send 自定义机器人发送群消息

func (*Send) Body

func (s *Send) Body(r *http.Request, value reflect.Value, body []reflect.StructField) (io.Reader, error)

func (*Send) Method

func (*Send) Method() string

func (*Send) RawURL

func (*Send) RawURL() string

type SendError

type SendError struct {
	API     *Send
	ErrMsg  string
	ErrCode int
}

SendError 发送消息错误

func (SendError) Error

func (s SendError) Error() string

type SendHandler

type SendHandler func(*Send) error

发送消息接口的前处理器,可以用来更新消息、生成加密签名、设置消息幂等、设置@等

func AtMobile

func AtMobile(mobiles ...string) SendHandler

AtMobile @指定群成员手机号

func AtUserID

func AtUserID(ids ...string) SendHandler

AtUserID @指定群成员 userId

func Secret

func Secret(secret string) SendHandler

Secret 会自动设置生成的加密签名,密钥参数为机器人安全设置页面,加签一栏下面显示的 SEC 开头的字符串

func UUID

func UUID(uuid string) SendHandler

UUID 设置消息幂等

func UpdateMsg

func UpdateMsg[T Msg](fn func(T) T) SendHandler

UpdateMsg 更新消息

type SendResponse

type SendResponse struct {
	ErrMsg  string `json:"errmsg"`
	ErrCode int    `json:"errcode"`
}

SendResponse 发送消息响应体

func PostSend

func PostSend(token string, msg Msg, handlers ...SendHandler) (SendResponse, error)

PostSendWithContext 发送消息

func PostSendWithContext

func PostSendWithContext(ctx context.Context, token string, msg Msg, handlers ...SendHandler) (r SendResponse, err error)

PostSendWithContext 携带上下文发送消息

type Text

type Text struct {
	Content string `json:"content" yaml:"content" toml:"content" long:"content"` // 文本消息的内容
}

Text 文本类型消息

func (Text) Type

func (Text) Type() MsgType

Jump to

Keyboard shortcuts

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