workrobot

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

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 17 Imported by: 0

README

workrobot

Package workrobot implements work-wx robot.

Installation

Use the go command:

go get -u github.com/wjiec/workrobot

Example

package main

import (
	"net/http"
	"net/url"
	"os"

	"github.com/wjiec/workrobot"
	"github.com/wjiec/workrobot/markdown"
)

func main() {
	httpClient := &http.Client{
		Transport: &http.Transport{
			Proxy: func(_ *http.Request) (*url.URL, error) {
				return url.Parse("http://proxy.example.com:8888")
			},
		},
	}

	c, err := workrobot.NewClient("[your work-wx robot key]", workrobot.WithHttpClient(httpClient))
	if err != nil {
		panic(err)
	}

	text, _ := workrobot.NewText("hello world from workrobot")
	text.MentionMember("jayson")
	text.MentionMobile("18012345678")

	md, _ := workrobot.NewMarkdown(
		markdown.Title(markdown.MediumTitle, "Server Closed"),
		markdown.Quote("Ip: 10.2.3.4\nAction: Restart"),
		markdown.Link("View", "http://dashboard.example.com"),
		markdown.ColorOrangeRed("something message..."))

	f, _ := os.Open("/image.jpg")
	image, _ := workrobot.NewImage(f)

	result, _ := c.Uploader().UploadFromReader(f)
	media := workrobot.NewMedia(result.Id)

	if err := c.Send(text, md, image, media); err != nil {
		panic(err)
	}

	if err := c.SendConcurrency(false, text, md, image, media); err != nil {
		panic(err)
	}
}

Documentation

Documentation is hosted at GoDoc project.

Copyright (C) 2021 by Jayson Wang.

package released under MIT License. See LICENSE for details.

Documentation

Overview

Copyright (c) 2021 Jayson Wang.

Package workrobot implements work-wx robot.

see https://work.weixin.qq.com/api/doc/90000/90136/91770

Index

Constants

View Source
const (
	// default robot webhook gateway
	DefaultSendGateway = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	// default upload gateway
	DefaultUploadGateway = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media"
)
View Source
const (
	TextMessageMaxLength     = 2048
	MarkdownMessageMaxLength = 4096
	MaxImageFileSize         = 2 * 1024 * 1024 // 2M
	MaxArticleCount          = 8
)

Variables

View Source
var (
	// ErrMessageTooLong represents the message length exceeds the limit
	ErrMessageTooLong = errors.New("message too long")
	// ErrImageTooLarge represents the image size exceeds the limit(2m)
	ErrImageTooLarge = errors.New("image too large")
	// ErrTooManyArticle represents articles count more than 8
	ErrTooManyArticle = errors.New("too many articles")
)

Functions

func Webhook

func Webhook(key string) string

Webhook build webhook address from robot key

Types

type Article

type Article struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	Link        string `json:"url"`
	ImageUrl    string `json:"picurl"`
}

Article represents an article message data see https://work.weixin.qq.com/api/doc/90000/90136/91770#图文类型

type Card

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

Card represents a card message

func NewCard

func NewCard(articles ...*Article) (*Card, error)

NewCard create a article card message

func (*Card) AddArticle

func (c *Card) AddArticle(article *Article) error

AddArticle add article into card

func (*Card) Message

func (c *Card) Message() []byte

Message implement Messager and build message with card content

type Client

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

Client represents a robot client

func NewClient

func NewClient(key string, options ...ClientOption) (*Client, error)

NewClient create a instance of robot

func (*Client) Send

func (c *Client) Send(messages ...Messager) error

Send send messages to the group in order, when error occurs will be returns immediately and skip the rest of the messages

func (*Client) SendConcurrency

func (c *Client) SendConcurrency(fastFail bool, messages ...Messager) (err error)

SendConcurrency send message to the group concurrency, and error will returns according to fastFail, returns immediately when fastFail is true, aggregation errors otherwise

concurrency limit: 20/min, 2/sec see https://work.weixin.qq.com/api/doc/90000/90136/91770#消息发送频率限制

func (*Client) Uploader

func (c *Client) Uploader() *uploader.Uploader

Uploader returns the uploader for current robot

type ClientOption

type ClientOption func(*Client) error

ClientOption represents additional robot configuration

func WithHttpClient

func WithHttpClient(hc *http.Client) ClientOption

WithHttpClient override http client for robot request

func WithWebhook

func WithWebhook(webhook string) ClientOption

WithWebhook override robot webhook address

type Image

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

Image represents an image

func NewImage

func NewImage(reader io.Reader) (*Image, error)

NewImage create an image message from reader

func (*Image) From

func (img *Image) From(reader io.Reader) error

From read image data from reader

func (*Image) Message

func (img *Image) Message() []byte

Message implement Messager and build message with image content

type Markdown

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

Markdown represents a pure markdown message

func NewMarkdown

func NewMarkdown(lines ...interface{}) (*Markdown, error)

NewMarkdown create a markdown message from lines

func (*Markdown) AddLine

func (msg *Markdown) AddLine(l string) error

AddLine add a text as line

func (*Markdown) AddSegmentLine

func (msg *Markdown) AddSegmentLine(s md.Segment) error

AddSegmentLine add a segment as line

func (*Markdown) Message

func (msg *Markdown) Message() []byte

Message implement Messager and build message with markdown content

func (*Markdown) RawContent

func (msg *Markdown) RawContent(raw string) error

RawContent sets the raw markdown text

type Media

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

Media represents a file message

func NewMedia

func NewMedia(mediaId string) *Media

NewMedia create a media message from io.Reader

func (*Media) Message

func (m *Media) Message() []byte

Message implement Messager and build message with media content

type Mention

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

Mention represents a mention message

func NewMention

func NewMention(members []string, mobiles []string, all bool) *Mention

NewMention create a mention message without error

func (*Mention) MentionAll

func (msg *Mention) MentionAll(all bool) *Mention

MentionAll make mentioned all group members

func (*Mention) MentionMember

func (msg *Mention) MentionMember(member string) *Mention

MentionMember make mentioned someone in group

func (*Mention) MentionMobile

func (msg *Mention) MentionMobile(mobile string) *Mention

MentionMobile make mentioned someone by mobile in group

func (*Mention) Message

func (msg *Mention) Message() []byte

Message implement Messager and build message only mention

type Messager

type Messager interface {
	Message() []byte
}

Messager represents a message will sent

type Text

type Text struct {
	Mention
	// contains filtered or unexported fields
}

Text represents a text and mention message

func NewText

func NewText(content string) (*Text, error)

NewText create a text message

func (*Text) Content

func (msg *Text) Content(content string) error

Content sets the message content, only pure text, and max

func (*Text) Message

func (msg *Text) Message() []byte

Message implement Messager and build message with content and mention

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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