dingtalk

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2020 License: GPL-3.0 Imports: 15 Imported by: 5

README

Golang DingTalk SDK

godoc build status

钉钉SDK

群机器人

功能

  • 支持完整的接口调用
  • 支持go模板文件调用

消息类型

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

Example

Let's have a look at an example which demonstrates most of the features found in this library.

package main

import (
	"io/ioutil"

	"github.com/CodyGuo/dingtalk"
	"github.com/CodyGuo/dingtalk/pkg/robot"
	"github.com/CodyGuo/glog"
)

func main() {
	glog.SetFlags(glog.LglogFlags)
	webHook := "https://oapi.dingtalk.com/robot/send?access_token=xxx"
	secret := "xxx"
	dt := dingtalk.New(webHook, dingtalk.WithSecret(secret))

	// text类型
	textContent := "我就是我, 是不一样的烟火@176xxxx8207"
	atMobiles := robot.SendWithAtMobiles([]string{"176xxxxxx07", "178xxxxxx28"})
	if err := dt.RobotSendText(textContent, atMobiles); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)

	// link类型
	linkTitle := "时代的火车向前开"
	linkText := `这个即将发布的新版本,创始人xx称它为“红树林”。` +
		`而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,` +
		`这一次,为什么是“红树林”?`
	linkMessageURL := "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"
	linkPicURL := "https://cdn.pixabay.com/photo/2020/05/05/08/05/butterfly-5131967_960_720.jpg"
	if err := dt.RobotSendLink(linkTitle, linkText, linkMessageURL, linkPicURL); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)

	// markdown类型
	markdownTitle := "markdown"
	markdownText := "#### 杭州天气 @176XXXXXXXX\n" +
		"> 9度,西北风1级,空气良89,相对温度73%\n" +
		"> ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n" +
		"> ###### 10点20分发布 [天气](https://www.dingtalk.com)\n"
	if err := dt.RobotSendMarkdown(markdownTitle, markdownText); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)

	// 整体跳转ActionCard类型
	actionCardTitle := "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身"
	actionCardText := "![screenshot](@lADOpwk3K80C0M0FoA)\n" +
		"### 乔布斯 20 年前想打造的苹果咖啡厅\n" +
		"Apple Store 的设计正从原来满满的科技感走向生活化," +
		"而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划"
	actionCardSingleTitle := "阅读全文"
	actionCardSingleURL := "https://www.dingtalk.com/"
	actionCardBtnOrientation := "0"
	if err := dt.RobotSendEntiretyActionCard(actionCardTitle,
		actionCardText,
		actionCardSingleTitle,
		actionCardSingleURL,
		actionCardBtnOrientation); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)

	// 独立跳转ActionCard类型
	btns := map[string]string{
		"内容不错": actionCardSingleURL,
		"不感兴趣": actionCardSingleURL,
	}
	if err := dt.RobotSendIndependentActionCard(actionCardTitle,
		actionCardText,
		actionCardBtnOrientation,
		btns); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)

	// FeedCard类型
	link1 := robot.FeedCardLink{
		Title:      linkTitle,
		MessageURL: linkMessageURL,
		PicURL:     linkPicURL,
	}
	link2 := robot.FeedCardLink{
		Title:      linkTitle + "2",
		MessageURL: linkMessageURL,
		PicURL:     linkPicURL,
	}
	links := []robot.FeedCardLink{link1, link2}
	if err := dt.RobotSendFeedCard(links); err != nil {
		glog.Fatal(err)
	}
	printResult(dt)
}

func printResult(dt *dingtalk.DingTalk) {
	response, err := dt.GetResponse()
	if err != nil {
		glog.Fatal(err)
	}
	reqBody, err := response.Request.GetBody()
	if err != nil {
		glog.Fatal(err)
	}
	reqData, err := ioutil.ReadAll(reqBody)
	if err != nil {
		glog.Fatal(err)
	}
	glog.Infof("发送消息成功, message: %s", reqData)
}

text类型

image

image

markdown类型

image

整体跳转ActionCard类型

image

独立跳转ActionCard类型

image

FeedCard类型

image

Using

$ go get github.com/CodyGuo/dingtalk

You can use go get -u to update the package.

Documentation

For docs, see https://pkg.go.dev/github.com/CodyGuo/dingtalk or run:

$ go doc github.com/CodyGuo/dingtalk

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLogLevel

func GetLogLevel() glog.Level

func SetLogLevel

func SetLogLevel(level glog.Level)

Types

type DingTalk

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

func New

func New(url string, options ...Option) *DingTalk

func (*DingTalk) GetResponse

func (dt *DingTalk) GetResponse() (*http.Response, error)

func (*DingTalk) GetSecret added in v0.0.2

func (dt *DingTalk) GetSecret() string

func (*DingTalk) GetTimeout added in v0.0.2

func (dt *DingTalk) GetTimeout() time.Duration

func (*DingTalk) Request

func (dt *DingTalk) Request(req Requester) error

func (*DingTalk) RobotSendEntiretyActionCard

func (dt *DingTalk) RobotSendEntiretyActionCard(title, text, singleTitle, singleURL, btnOrientation string, options ...robot.SendOption) error

RobotSendEntiretyActionCard 整体跳转ActionCard类型

func (*DingTalk) RobotSendFeedCard

func (dt *DingTalk) RobotSendFeedCard(links []robot.FeedCardLink, options ...robot.SendOption) error

RobotSendFeedCard FeedCard类型

func (*DingTalk) RobotSendIndependentActionCard

func (dt *DingTalk) RobotSendIndependentActionCard(title, text, btnOrientation string, btns map[string]string, options ...robot.SendOption) error

RobotSendIndependentActionCard 独立跳转ActionCard类型

func (dt *DingTalk) RobotSendLink(title, text, messageURL, picURL string, options ...robot.SendOption) error

RobotSendLink link类型的消息

func (*DingTalk) RobotSendLinkWithFile added in v0.0.3

func (dt *DingTalk) RobotSendLinkWithFile(filename string, data interface{}, options ...robot.SendOption) error

RobotSendLinkWithFile link类型的消息 template file:

第一行: title
第二行: messageURL
第三行: picURL
其他行: text

func (*DingTalk) RobotSendLinkWithTemplate added in v0.0.3

func (dt *DingTalk) RobotSendLinkWithTemplate(text string, data interface{}, options ...robot.SendOption) error

RobotSendLinkWithTemplate link类型的消息 template:

第一行: title
第二行: messageURL
第三行: picURL
其他行: text

func (*DingTalk) RobotSendMarkdown

func (dt *DingTalk) RobotSendMarkdown(title, text string, options ...robot.SendOption) error

RobotSendMarkdown markdown类型的消息

func (*DingTalk) RobotSendText

func (dt *DingTalk) RobotSendText(text string, options ...robot.SendOption) error

RobotSendText text类型的消息

func (*DingTalk) RobotSendTextWithFile added in v0.0.3

func (dt *DingTalk) RobotSendTextWithFile(filename string, data interface{}, options ...robot.SendOption) error

RobotSendTextWithFile text类型的消息 template file

func (*DingTalk) RobotSendTextWithTemplate added in v0.0.3

func (dt *DingTalk) RobotSendTextWithTemplate(text string, data interface{}, options ...robot.SendOption) error

RobotSendTextWithTemplate text类型的消息 template

func (*DingTalk) SetSecret

func (dt *DingTalk) SetSecret(secret string)

func (*DingTalk) SetTimeout added in v0.0.2

func (dt *DingTalk) SetTimeout(timeout time.Duration)

type Error added in v0.0.2

type Error struct {
	Op   string `json:"Op"`
	URL  string `json:"URL"`
	Body string `json:"Body"`
	Err  error  `json:"Err"`
}

func (*Error) Error added in v0.0.2

func (e *Error) Error() string

func (*Error) Unwrap added in v0.0.2

func (e *Error) Unwrap() error

type Option

type Option func(*DingTalk)

func WithSecret

func WithSecret(secret string) Option

func WithTimeout added in v0.0.2

func WithTimeout(timeout time.Duration) Option

type Requester

type Requester interface {
	GetMethod() string
	GetHeader() map[string]string
	GetBody() ([]byte, error)
	GetSuccessCode() int64
}

type ResponseMsg added in v0.0.2

type ResponseMsg struct {
	ErrCode         int64  `json:"errcode"`
	ErrMsg          string `json:"errmsg"`
	ApplicationHost string `json:"application_host,omitempty"`
	ServiceHost     string `json:"service_host,omitempty"`
}

func (ResponseMsg) String added in v0.0.2

func (r ResponseMsg) String() string

Directories

Path Synopsis
examples
pkg

Jump to

Keyboard shortcuts

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