wechat

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2020 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package wechat provides sending message ability on WeChat.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Article

func Article(title, description, url, picurl string) fw.Option

Article sets an article.

func Cue

func Cue(mention ...string) fw.Option

Cue sets mentions which could be phone number or user name.

func Filename

func Filename(filename string) fw.Option

Filename sets upload file name.

func MessageType

func MessageType(msgType MsgType) fw.Option

MessageType sets MsgType, like: TextType, MarkdownType, ImageType, NewsType and FileType.

func NewSender

func NewSender(webhook, key string, opt ...fw.Option) fw.Sender

NewSender returns Sender which sends message to WeChat.

Types

type Message

type Message struct {
	MessageType string           `json:"msgtype"`
	Text        *textMessage     `json:"text,omitempty"`
	Markdown    *markdownMessage `json:"markdown,omitempty"`
	Image       *imageMessage    `json:"image,omitempty"`
	News        *newsMessage     `json:"news,omitempty"`
	File        *fileMessage     `json:"file,omitempty"`
}

Message reflects the message body request WeChat api.

type MsgType

type MsgType int

MsgType indicates message format.

const (
	// TextType indicates text message you will send.
	TextType MsgType = iota + 1

	// MarkdownType indicates markdown message you will send.
	MarkdownType

	// ImageType indicates Image message you will send.
	ImageType

	// NewsType indicates news message you will send.
	NewsType

	// FileType indicates file message you will send.
	FileType
)

type WeChat

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

WeChat implements Sender which sends message WeChat.

func (*WeChat) Send

func (wc *WeChat) Send(message io.Reader, opt ...fw.Option) (err error)

Send sends message to WeChat which you have to provides io.Reader include message body, and few options which implements Option.

Example (File)
package main

import (
	"log"
	"os"

	fw "github.com/LiangXianSen/firewheel"
	"github.com/LiangXianSen/firewheel/sender/wechat"
)

func main() {
	fd, err := os.Open("../../testdata/report.csv")
	if err != nil {
		log.Fatal(err)
	}

	var sender fw.Sender

	key := ""
	webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	sender = wechat.NewSender(webhook, key)
	if err = sender.Send(
		fd,
		wechat.MessageType(wechat.FileType),
		wechat.Filename("report.csv"),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

Example (Image)
package main

import (
	"log"
	"os"

	fw "github.com/LiangXianSen/firewheel"
	"github.com/LiangXianSen/firewheel/sender/wechat"
)

func main() {
	fd, err := os.Open("../../testdata/timg.jpeg")
	if err != nil {
		log.Fatal(err)
	}

	var sender fw.Sender

	key := ""
	webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	sender = wechat.NewSender(webhook, key)
	if err = sender.Send(
		fd,
		wechat.MessageType(wechat.ImageType),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

Example (Markdown)
package main

import (
	"log"
	"strings"

	fw "github.com/LiangXianSen/firewheel"
	"github.com/LiangXianSen/firewheel/sender/wechat"
)

func main() {
	var sender fw.Sender

	key := ""
	webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	sender = wechat.NewSender(webhook, key)
	if err := sender.Send(
		strings.NewReader("# test message"),
		wechat.MessageType(wechat.MarkdownType),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

Example (News)
package main

import (
	"log"
	"strings"

	fw "github.com/LiangXianSen/firewheel"
	"github.com/LiangXianSen/firewheel/sender/wechat"
)

func main() {
	var sender fw.Sender

	key := ""
	webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	sender = wechat.NewSender(webhook, key)
	if err := sender.Send(
		strings.NewReader(""),
		wechat.MessageType(wechat.NewsType),
		wechat.Article("No.1", "Coding commnunity", "github.com", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1606718584317&di=2546710d44d6582d9fe58bf18d2711a7&imgtype=0&src=http%3A%2F%2Foss.cyzone.cn%2F2016%2F0304%2F20160304022832427.png"),
		wechat.Article("No.2", "something else", "github.com", ""),
		wechat.Article("No.3", "last one", "github.com", ""),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

Example (Text)
package main

import (
	"log"
	"strings"

	fw "github.com/LiangXianSen/firewheel"
	"github.com/LiangXianSen/firewheel/sender/wechat"
)

func main() {
	var sender fw.Sender

	key := ""
	webhook := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	sender = wechat.NewSender(webhook, key)
	if err := sender.Send(
		strings.NewReader("test message"),
		wechat.MessageType(wechat.TextType),
		wechat.Cue("@all"),
		wechat.Cue("18512345678"),
	); err != nil {
		log.Fatal(err)
	}
}
Output:

Jump to

Keyboard shortcuts

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