dingtalk

package module
v0.0.0-...-24c8a74 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2021 License: MIT Imports: 13 Imported by: 0

README

dingtalk

钉钉群机器人SDK

安装

go get github.com/shockerli/dingtalk

使用

初始化配置
var robot = dingtalk.NewRobotCustom()
robot.SetWebhook("your_robot_webhook")
robot.SetSecret("your_secret") // 可选
Text
robot.SendText("TEST: Text")
AtAll
robot.SendText("TEST: Text&AtAll", robot.AtAll())
AtMobiles
robot.SendText("TEST: Text&AtMobiles", robot.AtMobiles("19900001111"))
robot.SendLink(
    "TEST: Link",
    "link content",
    "https://github.com/shockerli",
    "https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg",
)
Markdown
robot.SendMarkdown("TEST: Markdown", markdown)

robot.SendMarkdown("TEST: Markdown&AtAll", markdown, robot.AtAll())

robot.SendMarkdown("TEST: Markdown&AtMobiles", markdown, robot.AtMobiles("19900001111"))
ActionCard
robot.SendActionCard(
    "TEST: ActionCard&SingleCard",
    "SingleCard content",
    robot.SingleCard("阅读全文", "https://github.com/shockerli"),
)

robot.SendActionCard(
    "TEST: ActionCard&MultiCard",
    "MultiCard content",
    robot.MultiCard("内容不错", "https://github.com/shockerli"),
    robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
)

robot.SendActionCard(
    "TEST: ActionCard&BtnOrientation",
    "BtnOrientation content",
    robot.MultiCard("内容不错", "https://github.com/shockerli"),
    robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
    robot.BtnOrientation("0"),
)

robot.SendActionCard(
    "TEST: ActionCard&Image",
    "![xxx](https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg)\n\nSingleCard content with image",
    robot.SingleCard("阅读全文", "https://github.com/shockerli"),
)

robot.SendActionCard(
    "TEST: ActionCard&HideAvatar",
    "24565\n\n![xxx](https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg)\n\nSingleCard content with image",
    robot.SingleCard("阅读全文", "https://github.com/shockerli"),
    robot.HideAvatar("1"),
)
FeedCard
robot.SendFeedCard(
    robot.FeedCard("3月15日起,Chromium 不能再调用谷歌 API", "https://bodhi.fedoraproject.org/updates/FEDORA-2021-48866282e5%29", "https://www.wangbase.com/blogimg/asset/202101/bg2021012506.jpg"),
    robot.FeedCard("考古学家在英国发现两枚11世纪北宋时期的中国硬币", "https://www.caitlingreen.org/2020/12/another-medieval-chinese-coin-from-england.html", "https://www.wangbase.com/blogimg/asset/202101/bg2021012208.jpg"),
)
Outgoing
// 获取HTTP请求Body
var contents = getRequestBody()

// 解析Outgoing内容
og, err := robot.ParseOutgoing(contents)
if err != nil {
    // ...
}

// 自定义业务逻辑,生成响应的Text消息内容
var res = doSomeThing(og)

// 发送回复消息
err = robot.SendText(res, robot.WithOutgoing(og))
if err != nil {
    // ...
}

获取群机器人Token

  • 选择自定义机器人

    选择自定义机器人

  • 设置机器人

    设置机器人

  • 获取Webhook&token

    获取Webhook

测试

  1. 打开 robot_test.go 文件,修改 your_robot_webhookyour_secret
  2. 运行单元测试 go test -v *_test.go

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RobotCustom

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

RobotCustom 群机器人-自定义

官方文档: https://developers.dingtalk.com/document/app/custom-robot-access

func NewRobotCustom

func NewRobotCustom() *RobotCustom

NewRobotCustom 实例化

func (*RobotCustom) AtAll

func (rc *RobotCustom) AtAll() RobotOption

AtAll 设置是否@所有人

适用Text/Markdown类型

示例:

robot.SendMarkdown("TEST: Markdown&AtAll", markdown, robot.AtAll())

func (*RobotCustom) AtMobiles

func (rc *RobotCustom) AtMobiles(m ...string) RobotOption

AtMobiles 设置@人的手机号

适用Text/Markdown类型

示例:

robot.SendMarkdown("TEST: Markdown&AtMobiles", markdown, robot.AtMobiles("19900001111"))

func (*RobotCustom) BtnOrientation

func (rc *RobotCustom) BtnOrientation(v string) RobotOption

BtnOrientation 按钮排列(0-竖直排列, 1-横向排列, 默认1)

适用ActionCard类型

示例:

robot.SendActionCard(
	"TEST: ActionCard&BtnOrientation",
	"BtnOrientation content",
	robot.MultiCard("内容不错", "https://github.com/shockerli"),
	robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
	robot.BtnOrientation("0"),
)

func (*RobotCustom) FeedCard

func (rc *RobotCustom) FeedCard(title, msgURL, picURL string) RobotOption

FeedCard 添加一个FeedCard项

适用FeedCard类型

示例:

robot.SendFeedCard(
	robot.FeedCard("3月15日起,Chromium 不能再调用谷歌 API", "https://bodhi.fedoraproject.org/updates/FEDORA-2021-48866282e5%29", "https://www.wangbase.com/blogimg/asset/202101/bg2021012506.jpg"),
	robot.FeedCard("考古学家在英国发现两枚11世纪北宋时期的中国硬币", "https://www.caitlingreen.org/2020/12/another-medieval-chinese-coin-from-england.html", "https://www.wangbase.com/blogimg/asset/202101/bg2021012208.jpg"),
)

func (*RobotCustom) HideAvatar

func (rc *RobotCustom) HideAvatar(v string) RobotOption

HideAvatar 隐藏头像(0-显示, 1-隐藏, 默认0)

适用ActionCard类型

示例:

robot.SendActionCard(
	"TEST: ActionCard&HideAvatar",
	"24565\n\n![xxx](https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg)\n\nSingleCard content with image",
	robot.SingleCard("阅读全文", "https://github.com/shockerli"),
	robot.HideAvatar("1"),
)

func (*RobotCustom) MultiCard

func (rc *RobotCustom) MultiCard(title, url string) RobotOption

MultiCard 添加一个MultiCard项

适用ActionCard类型

示例:

robot.SendActionCard(
	"TEST: ActionCard&MultiCard",
	"MultiCard content",
	robot.MultiCard("内容不错", "https://github.com/shockerli"),
	robot.MultiCard("不感兴趣", "https://github.com/shockerli"),
)

func (*RobotCustom) ParseOutgoing

func (rc *RobotCustom) ParseOutgoing(r io.Reader) (og RobotOutgoing, err error)

ParseOutgoing 解析Outgoing消息体

示例:

robot.ParseOutgoing(callbackBody)

func (*RobotCustom) SendActionCard

func (rc *RobotCustom) SendActionCard(title, text string, opts ...RobotOption) error

SendActionCard 发送ActionCard消息

示例:

robot.SendActionCard(
	"TEST: ActionCard&SingleCard",
	"SingleCard content",
	robot.SingleCard("阅读全文", "https://github.com/shockerli"),
)

func (*RobotCustom) SendFeedCard

func (rc *RobotCustom) SendFeedCard(opts ...RobotOption) error

SendFeedCard 发送FeedCard消息

示例:

robot.SendFeedCard(
	robot.FeedCard("3月15日起,Chromium 不能再调用谷歌 API", "https://bodhi.fedoraproject.org/updates/FEDORA-2021-48866282e5%29", "https://www.wangbase.com/blogimg/asset/202101/bg2021012506.jpg"),
	robot.FeedCard("考古学家在英国发现两枚11世纪北宋时期的中国硬币", "https://www.caitlingreen.org/2020/12/another-medieval-chinese-coin-from-england.html", "https://www.wangbase.com/blogimg/asset/202101/bg2021012208.jpg"),
)
func (rc *RobotCustom) SendLink(title, text, msgURL, picURL string, opts ...RobotOption) error

SendLink 发送Link消息

示例:

robot.SendLink(
	"TEST: Link",
	"link content",
	"https://github.com/shockerli",
	"https://www.wangbase.com/blogimg/asset/202101/bg2021011601.jpg",
)

func (*RobotCustom) SendMarkdown

func (rc *RobotCustom) SendMarkdown(title, text string, opts ...RobotOption) error

SendMarkdown 发送Markdown消息

示例:

robot.SendMarkdown("TEST: Markdown", markdown)
robot.SendMarkdown("TEST: Markdown&AtAll", markdown, robot.AtAll())
robot.SendMarkdown("TEST: Markdown&AtMobiles", markdown, robot.AtMobiles("19900001111"))

func (*RobotCustom) SendText

func (rc *RobotCustom) SendText(content string, opts ...RobotOption) error

SendText 发送Text消息

示例:

robot.SendText("TEST: Text")
robot.SendText("TEST: Text&AtAll", robot.AtAll())
robot.SendText("TEST: Text&AtMobiles", robot.AtMobiles("19900001111"))

func (*RobotCustom) SetSecret

func (rc *RobotCustom) SetSecret(s string) *RobotCustom

SetSecret 设置Secret

func (*RobotCustom) SetWebhook

func (rc *RobotCustom) SetWebhook(t string) *RobotCustom

SetWebhook 设置Token

func (*RobotCustom) SingleCard

func (rc *RobotCustom) SingleCard(title, url string) RobotOption

SingleCard 整体跳转配置

适用ActionCard类型

示例:

robot.SendActionCard(
	"TEST: ActionCard&SingleCard",
	"SingleCard content",
	robot.SingleCard("阅读全文", "https://github.com/shockerli"),
)

func (*RobotCustom) WithOutgoing

func (rc *RobotCustom) WithOutgoing(og RobotOutgoing) RobotOption

WithOutgoing 通过Outgoing的临时消息接口发送

示例:

og, err := robot.ParseOutgoing(bytes.NewBufferString(callbackBody))
err = robot.SendText("callback", robot.WithOutgoing(og))

type RobotOption

type RobotOption func(*robotMsg)

RobotOption 群机器人-消息配置项

type RobotOutgoing

type RobotOutgoing struct {
	// 被@人的信息
	AtUsers []struct {
		DingTalkID string `json:"dingtalkId"` // 加密的人员ID
	} `json:"atUsers"`
	ChatBotUserID             string    `json:"chatbotUserId"`             // 加密的机器人ID
	ConversationID            string    `json:"conversationId"`            // 加密的会话ID
	ConversationTitle         string    `json:"conversationTitle"`         // 会话标题(群聊时才有,即群名)
	ConversationType          string    `json:"conversationType"`          // 1-单聊、2-群聊
	CreateAt                  int64     `json:"createAt"`                  // 消息的时间戳,单位ms
	IsAdmin                   bool      `json:"isAdmin"`                   // 是否为管理员发送的消息
	IsInAtList                bool      `json:"isInAtList"`                //
	MsgID                     string    `json:"msgId"`                     // 加密的消息ID
	MsgType                   string    `json:"msgtype"`                   // 消息类型: 目前只支持Text
	SceneGroupCode            string    `json:"sceneGroupCode"`            // 群组场景类型Code
	SenderID                  string    `json:"senderId"`                  // 加密的发送者ID
	SenderNick                string    `json:"senderNick"`                // 发送者昵称
	SessionWebhook            string    `json:"sessionWebhook"`            // 临时的发送消息接口
	SessionWebhookExpiredTime int64     `json:"sessionWebhookExpiredTime"` // SessionWebhook可用的有效截止时间
	Text                      robotText `json:"text"`                      // Text类型的消息体
}

RobotOutgoing Outgoing回调消息体

示例:

   "msgtype": "text"
}
	{
   	"conversationId": "ciddz7nmHDaX/7Niz+Gb5VVrw==",
   	"sceneGroupCode": "project",
   	"atUsers": [
       	{
           	"dingtalkId": "$:LWCP_v1:$0sIVIuw1HvQQ5gRAtFWzypo0+T1TgPOP"
       	},
       	{
           	"dingtalkId": "$:LWCP_v1:$I3cyfTzrws4nCbY289cXbKCVcdd1wize"
       	}
   	],
   	"chatbotUserId": "$:LWCP_v1:$I3cyfTzrws4nCbY289cXbKCVcdd1wize",
   	"msgId": "msgaKcioIqERkELm2T8TlE9CA==",
   	"senderNick": "Jioby",
   	"isAdmin": false,
   	"sessionWebhookExpiredTime": 1612178396066,
   	"createAt": 1612172996026,
   	"conversationType": "2",
   	"senderId": "$:LWCP_v1:$deZJcSfMzexC2YK+oLkk1g==",
   	"conversationTitle": "xxx",
   	"isInAtList": true,
   	"sessionWebhook": "https://oapi.dingtalk.com/robot/sendBySession?session=eb18e18e8669b0a3cd7dff1388fe5e6a",
   	"text": {
       	"content": "  哈哈哈"
   	},
		"msgtype": "text"
	}

Jump to

Keyboard shortcuts

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