dingbot

package module
Version: v0.0.0-...-465007d Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2020 License: MIT Imports: 11 Imported by: 0

README

DingBot

logo

Bot for DingTalk (钉钉).

Features

Documentation

Overview

Package dingbot provides Bot related APIs for dingtalk open platform.

Index

Examples

Constants

View Source
const (
	WebhookURL = "https://oapi.dingtalk.com/robot/send"
	DingCodeOk = 0
)

constants

View Source
const (
	MsgTypeText       = "text"
	MsgTypeLink       = "link"
	MsgTypeMd         = "markdown"
	MsgTypeActionCard = "actionCard"
	MsgTypeFeedCard   = "feedCard"
)

exported message types

Variables

This section is empty.

Functions

func Sign

func Sign(secret, timestamp string) string

Sign signs the timestamp with a secret.

Types

type ActionCardMsg

type ActionCardMsg struct {
	Text           string `json:"text"`
	Title          string `json:"title"`
	HideAvatar     string `json:"hideAvatar"`     // "0" or "1"
	BtnOrientation string `json:"btnOrientation"` // "0" or "1"
	// single card
	SingleTitle string `json:"singleTitle"`
	SingleURL   string `json:"singleURL"`
	// independent cards
	Btns []struct {
		Title     string `json:"title"`
		ActionURL string `json:"actionURL"`
	} `json:"btns"`
}

ActionCardMsg represents a action card.

type AtOption

type AtOption struct {
	AtMobiles []string `json:"atMobiles"`
	IsAtAll   bool     `json:"isAtAll"`
}

AtOption is the option for @someone.

type DingMessage

type DingMessage struct {
	Msgtype    string         `json:"msgtype"`
	Text       *TextMsg       `json:"text,omitempty"`
	Link       *LinkMsg       `json:"link,omitempty"`
	Markdown   *MarkdownMsg   `json:"markdown,omitempty"`
	ActionCard *ActionCardMsg `json:"actionCard,omitempty"`
	FeedCard   *FeedCardMsg   `json:"feedCard,omitempty"`
	At         *AtOption      `json:"at,omitempty"`
}

DingMessage is the JSON-encoded message to send.

type DingResponse

type DingResponse struct {
	Errmsg  string `json:"errmsg"`
	Errcode int    `json:"errcode"`
}

DingResponse is the response for sending msg.

func (DingResponse) Error

func (d DingResponse) Error() string

type FeedCardMsg

type FeedCardMsg struct {
	Links []FeedLink `json:"links"`
}

FeedCardMsg represents a feed card.

type FeedLink struct {
	Title      string `json:"title"`
	PicURL     string `json:"picUrl"`
	MessageURL string `json:"messageUrl"`
}

FeedLink represents a feed.

type LinkMsg

type LinkMsg struct {
	Text       string `json:"text"`
	Title      string `json:"title"`
	PicURL     string `json:"picUrl,omitempty"`
	MessageURL string `json:"messageUrl"`
}

LinkMsg represents a link.

type MarkdownMsg

type MarkdownMsg struct {
	Text  string `json:"text"`
	Title string `json:"title"`
}

MarkdownMsg represents markdown formatted text.

type Sender

type Sender interface {
	// Send sends a DingMessage.
	Send(*DingMessage) error
}

Sender sends messages of various types to a Dingtalk group.

type TextMsg

type TextMsg struct {
	Content string `json:"content"`
}

TextMsg represents text.

type Webhook

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

Webhook sends the message via webhook API. See https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq.

Example
package main

import (
	"fmt"

	"github.com/practigo/dingbot"
)

func main() {
	var testToken = "YOUR_TOKEN_HERE"

	bot := dingbot.NewWebhook(testToken)

	var testSecret = "" // if you have secret set, fill here
	bot.WithSecret(testSecret)

	msg := dingbot.DingMessage{
		Msgtype: dingbot.MsgTypeText,
		Text: &dingbot.TextMsg{
			Content: "hello world",
		},
	}
	fmt.Println(bot.Send(&msg))
}
Output:

Error 300001: token is not exist

func NewWebhook

func NewWebhook(token string) *Webhook

NewWebhook returns a Webhook with the provided token.

func (*Webhook) Send

func (b *Webhook) Send(msg *DingMessage) (err error)

Send sends a DingMessage. The underlying HTTP client has a 5s timeout and will not retry when fail.

func (*Webhook) WithSecret

func (b *Webhook) WithSecret(s string)

WithSecret sets the webhook serect for passing the security check.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL