wecombot

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2023 License: MIT Imports: 11 Imported by: 0

README

wecombot

PkgGoDev Go Report Card codebeat badge

wecombot 是企业微信群机器人的一个 go 语言 sdk,API友好,无任何第三方依赖。

安装

$ go get github.com/voidint/wecombot@latest

用法

发送文本类型消息
package main

import "github.com/voidint/wecombot"

func main() {
	bot := wecombot.NewBot("YOUR_KEY")
	bot.SendText("hello 世界!") // 最长不超过2048个字节
	bot.SendText(
		"你好 world!",
		wecombot.WithMentionedMobileList("186xxxx1234", "170xxxx9876"), // 发送文字内容并@某些手机用户
	)
}
发送Markdown类型消息
package main

import "github.com/voidint/wecombot"

var md = `
# 成绩单
**姓名:**张三
**数学:**<font color="info">97</font>
**语文:**<font color="comment">72</font>
**英语:**<font color="warning">61</font>
`

func main() {
	wecombot.NewBot("YOUR_KEY").SendMarkdown(md) // 最长不超过4096个字节
}

注意:企业微信群消息仅支持有限的 markdown 语法

发送图片类型消息
package main

import (
	"log"
	"os"

	"github.com/voidint/wecombot"
)

func main() {
	f, err := os.ReadFile("logo.jpg") // 图片最大不能超过2M,支持JPG,PNG格式。
	if err != nil {
		log.Fatal(err)
	}

	wecombot.NewBot("YOUR_KEY").SendImage(f)
}
发送图文类型消息
package main

import (
	"github.com/voidint/wecombot"
)

func takePointer(s string) *string {
	return &s
}

func main() {
	wecombot.NewBot("YOUR_KEY").SendNews(&wecombot.Article{
		Title:       "中秋节礼品领取",
		Description: takePointer("今年中秋节公司有豪礼相送"), // 可选字段使用指针类型
		URL:         "www.qq.com",
		PicURL:      takePointer("http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png"),
	})
}
发送文件类型消息
package main

import (
	"log"
	"os"

	"github.com/voidint/wecombot"
)

func main() {
	data, err := os.ReadFile("学生成绩单.xlsx") // 文件大小不超过20M
	if err != nil {
		log.Fatal(err)
	}

	wecombot.NewBot("YOUR_KEY").SendFile(data, "学生成绩单.xlsx")
}
发送语音类型消息
package main

import (
	"log"
	"os"

	"github.com/voidint/wecombot"
)

func main() {
	data, err := os.ReadFile("生日祝福.amr") // 文件大小不超过2M,播放长度不超过60s,仅支持AMR格式。
	if err != nil {
		log.Fatal(err)
	}

	wecombot.NewBot("YOUR_KEY").SendVoice(data, "生日祝福.amr")
}
发送文本通知模板卡片类型消息
package main

import (
	"github.com/voidint/wecombot"
)

func takePointer(s string) *string {
	return &s
}

func main() {
	var msg wecombot.TextNoticeTemplateCardMessage
	msg.TemplateCard.MainTitle.Title = takePointer("欢迎使用企业微信")
	msg.TemplateCard.MainTitle.Desc = takePointer("您的好友正在邀请您加入企业微信")
	msg.TemplateCard.CardAction.Type = 1
	msg.TemplateCard.CardAction.URL = takePointer("https://work.weixin.qq.com/?from=openApi")
	// 参数较多,详见文档。

	wecombot.NewBot("YOUR_KEY").SendTextNoticeTemplateCardMessage(&msg)
}
发送图文展示模板卡片类型消息
package main

import (
	"github.com/voidint/wecombot"
)

func takePointer(s string) *string {
	return &s
}

func main() {
	var msg wecombot.NewsNoticeTemplateCardMessage
	msg.TemplateCard.MainTitle.Title = takePointer("欢迎使用企业微信")
	msg.TemplateCard.MainTitle.Desc = takePointer("您的好友正在邀请您加入企业微信")
	msg.TemplateCard.CardImage.URL = *takePointer("https://wework.qpic.cn/wwpic/354393_4zpkKXd7SrGMvfg_1629280616/0")
	msg.TemplateCard.CardAction.Type = 1
	msg.TemplateCard.CardAction.URL = takePointer("https://work.weixin.qq.com/?from=openApi")
	// 参数较多,详见文档。

	wecombot.NewBot("YOUR_KEY").SendNewsNoticeTemplateCardMessage(&msg)
}
文件上传
package main

import (
	"log"
	"os"

	"github.com/voidint/wecombot"
)

func main() {
	data, err := os.ReadFile("学生成绩单.xlsx") // 文件大小不超过20M
	if err != nil {
		log.Fatal(err)
	}

	mediaRes, err := wecombot.NewBot("YOUR_KEY").UploadMedia(wecombot.NormalFile, data, "学生成绩单.xlsx")
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("media id: %s", mediaRes.MediaID)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractKey added in v0.3.0

func ExtractKey(webhookURL string) (key string)

ExtractKey 返回 webhook 中包含的 key 信息。若 URL 地址无效,或 URL 中不包含 key 参数,则返回空字符串。

func NewResError

func NewResError(code int, msg string) error

NewResError 返回响应错误实例

func WithHttpClient

func WithHttpClient(c *http.Client) func(*Bot)

WithHttpClient 设置 HTTP 客户端

func WithMentionedList

func WithMentionedList(userid ...string) func(*TextMessage)

WithMentionedList 设置被提醒的 userid 列表

func WithMentionedMobileList

func WithMentionedMobileList(mobile ...string) func(*TextMessage)

WithMentionedMobileList 设置被提醒的手机号列表

func WithThreadSafe

func WithThreadSafe() func(*Bot)

WithThreadSafe 设置线程安全模式

Types

type Article added in v0.2.0

type Article struct {
	// Title 标题,不超过128个字节,超过会自动截断。
	Title string `json:"title"`
	// Description 描述,不超过512个字节,超过会自动截断。
	Description *string `json:"description"`
	// URL 点击后跳转的链接。
	URL string `json:"url"`
	// PicURL 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
	PicURL *string `json:"picurl"`
}

Article 图文

type Bot

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

Bot 企业微信群机器人

func NewBot

func NewBot(key string, opts ...func(*Bot)) *Bot

NewBot 返回企业微信群机器人实例

func (*Bot) SendFile

func (bot *Bot) SendFile(f []byte, filename string) (err error)

SendFile 发送文件

func (*Bot) SendFileMessage

func (bot *Bot) SendFileMessage(msg *FileMessage) (err error)

SendFileMessage 发送文件消息

func (*Bot) SendImage added in v0.2.0

func (bot *Bot) SendImage(img []byte) (err error)

SendImageBytes 发送图片消息

func (*Bot) SendImageMessage added in v0.2.0

func (bot *Bot) SendImageMessage(msg *ImageMessage) (err error)

SendImageMessage 发送图片消息

func (*Bot) SendMarkdown

func (bot *Bot) SendMarkdown(content string) (err error)

SendMarkdown 发送 Markdown 消息

func (*Bot) SendMarkdownMessage

func (bot *Bot) SendMarkdownMessage(msg *MarkdownMessage) (err error)

SendMarkdownMessage 发送 Markdown 消息

func (*Bot) SendNews added in v0.2.0

func (bot *Bot) SendNews(articles ...*Article) (err error)

SendNews 发送图文消息

func (*Bot) SendNewsMessage added in v0.2.0

func (bot *Bot) SendNewsMessage(msg *NewsMessage) (err error)

SendNewsMessage 发送图文消息

func (*Bot) SendNewsNoticeTemplateCardMessage added in v0.2.0

func (bot *Bot) SendNewsNoticeTemplateCardMessage(msg *NewsNoticeTemplateCardMessage) error

SendNewsNoticeTemplateCardMessage 发送图文展示模板卡片类型消息

func (*Bot) SendText

func (bot *Bot) SendText(content string, opts ...func(*TextMessage)) (err error)

SendText 发送文本消息

func (*Bot) SendTextMessage

func (bot *Bot) SendTextMessage(msg *TextMessage) error

SendTextMessage 发送文本消息

func (*Bot) SendTextNoticeTemplateCardMessage added in v0.2.0

func (bot *Bot) SendTextNoticeTemplateCardMessage(msg *TextNoticeTemplateCardMessage) error

SendTextNoticeTemplateCardMessage 发送文本通知模板卡片类型消息

func (*Bot) SendVoice

func (bot *Bot) SendVoice(f []byte, filename string) (err error)

SendVoice 发送语音

func (*Bot) SendVoiceMessage

func (bot *Bot) SendVoiceMessage(msg *VoiceMessage) (err error)

SendVoiceMessage 发送语音消息

type CardAction added in v0.2.0

type CardAction struct {
	// Type 卡片跳转类型,1 代表跳转url,2 代表打开小程序。
	Type uint8 `json:"type"`
	// URL 跳转事件的url,type是1时必填。
	URL *string `json:"url"`
	// AppID 跳转事件的小程序的appid,type是2时必填。
	AppID *string `json:"appid"`
	// PagePath 跳转事件的小程序的pagepath,type是2时选填。
	PagePath *string `json:"pagepath"`
}

整体卡片的点击跳转事件

type CardImage added in v0.2.0

type CardImage struct {
	// URL 图片的url
	URL string `json:"url"`
	// AspectRatio 图片的宽高比,宽高比要小于2.25,大于1.3,不填该参数默认1.3
	AspectRatio *float32 `json:"aspect_ratio"`
}

图片样式

type CardType added in v0.2.0

type CardType string

CardType 模版卡片类型

const (
	// TextNoticeCardType 文本通知模板卡片类型
	TextNoticeCardType CardType = "text_notice"
	// NewsNoticeCardType 图文展示模板卡片类型
	NewsNoticeCardType CardType = "news_notice"
)

type EmphasisContent added in v0.2.0

type EmphasisContent struct {
	// Title 关键数据样式的数据内容,建议不超过10个字
	Title *string `json:"title"`
	// Desc 关键数据样式的数据描述内容,建议不超过15个字
	Desc *string `json:"desc"`
}

EmphasisContent 关键数据样式

type FileMessage

type FileMessage struct {
	// MsgType 必填。消息类型,此时固定为 file 。
	MsgType MsgType `json:"msgtype"`
	// 消息内容
	File struct {
		// MediaID 必填。文件id,通过文件上传接口获取。
		MediaID string `json:"media_id"`
	} `json:"file"`
}

FileMessage 文件类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B

type FileType

type FileType string

FileType 文件类型

const (
	// NormalFile 普通文件
	NormalFile FileType = "file"
	// VoiceFile 语音文件
	VoiceFile FileType = "voice"
)

type HorizontalContent added in v0.2.0

type HorizontalContent struct {
	// Type 模版卡片的二级标题信息内容支持的类型,1是url,2是文件附件,3代表点击跳转成员详情。
	Type *uint8 `json:"type"`
	// Keyname 二级标题,建议不超过5个字。
	KeyName string `json:"keyname"`
	// Value 二级文本,如果type是2,该字段代表文件名称(要包含文件类型),建议不超过26个字。
	Value *string `json:"value"`
	// URL 链接跳转的url,type是1时必填。
	URL *string `json:"url"`
	// MediaID 附件的media_id,type是2时必填。
	MediaID *string `json:"media_id"`
	// UserID 成员详情的userid,type是3时必填。
	UserID *string `json:"userid"`
}

HorizontalContent 二级标题+文本列表

type ImageMessage added in v0.2.0

type ImageMessage struct {
	// MsgType 必填。消息类型,此时固定为 image 。
	MsgType MsgType `json:"msgtype"`
	// 消息内容
	Image struct {
		// Base64 必填。图片内容的base64编码
		Base64 string `json:"base64"`
		// Md5 必填。图片内容(base64编码前)的md5值
		Md5 string `json:"md5"`
	} `json:"image"`
}

ImageMessage 图片类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#%E5%9B%BE%E7%89%87%E7%B1%BB%E5%9E%8B

type ImageTextArea added in v0.2.0

type ImageTextArea struct {
	// Type 左图右文样式区域点击事件,0或不填代表没有点击事件,1 代表跳转url,2 代表跳转小程序。
	Type *uint8 `json:"type"`
	// URL 点击跳转的url,type是1时必填。
	URL *string `json:"url"`
	// AppID 点击跳转的小程序的appid,必须是与当前应用关联的小程序,type是2时必填。
	AppID *string `json:"appid"`
	// PagePath 点击跳转的小程序的pagepath,type是2时选填。
	PagePath *string `json:"pagepath"`
	// Title 左图右文样式的标题
	Title *string `json:"title"`
	// Desc 左图右文样式的描述
	Desc *string `json:"desc"`
	// ImageURL 左图右文样式的图片url
	ImageURL string `json:"image_url"`
}

ImageTextArea 左图右文样式

type Jump added in v0.2.0

type Jump struct {
	// Type 跳转链接类型,0或不填代表不是链接,1 代表跳转url,2 代表跳转小程序。
	Type *uint8 `json:"type"`
	// Title 跳转链接样式的文案内容,建议不超过13个字。
	Title string `json:"title"`
	// URL 跳转链接的url,type是1时必填。
	URL *string `json:"url"`
	// AppID 跳转链接的小程序的appid,type是2时必填。
	AppID *string `json:"appid"`
	// PagePath 跳转链接的小程序的pagepath,type是2时选填。
	PagePath *string `json:"pagepath"`
}

Jump 跳转指引样式

type MainTitle added in v0.2.0

type MainTitle struct {
	// Title 一级标题,建议不超过26个字。模版卡片主要内容的一级标题main_title.title和二级普通文本sub_title_text必须有一项填写
	Title *string `json:"title"`
	// Desc 标题辅助信息,建议不超过30个字
	Desc *string `json:"desc"`
}

MainTitle 模版卡片的主要内容,包括一级标题和标题辅助信息。

type MarkdownMessage

type MarkdownMessage struct {
	// MsgType 必填。消息类型,此时固定为 markdown 。
	MsgType MsgType `json:"msgtype"`
	// 消息内容
	Markdown struct {
		// Content 必填。markdown内容,最长不超过4096个字节,必须是utf8编码。
		Content string `json:"content"`
	} `json:"markdown"`
}

MarkdownMessage Markdown 类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#markdown%E7%B1%BB%E5%9E%8B

type MsgType added in v0.2.0

type MsgType string

MsgType 消息类型

const (
	// TextMsgType 文本类型
	TextMsgType MsgType = "text"
	// MarkdownMsgType Markdown 类型
	MarkdownMsgType MsgType = "markdown"
	// ImageMsgType 图片类型
	ImageMsgType MsgType = "image"
	// NewsMsgType 图文类型
	NewsMsgType MsgType = "news"
	// FileMsgType 文件类型
	FileMsgType MsgType = "file"
	// VoiceMsgType 语音类型
	VoiceMsgType MsgType = "voice"
	// TemplateCardMsgType 模板卡片类型
	TemplateCardMsgType MsgType = "template_card"
)

type NewsMessage added in v0.2.0

type NewsMessage struct {
	// MsgType 必填。消息类型,此时固定为 news 。
	MsgType MsgType `json:"msgtype"`
	// 消息内容
	News struct {
		Articles []*Article `json:"articles"`
	} `json:"news"`
}

NewsMessage 图文类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#%E5%9B%BE%E6%96%87%E7%B1%BB%E5%9E%8B

type NewsNoticeTemplateCardMessage added in v0.2.0

type NewsNoticeTemplateCardMessage struct {
	// MsgType 必填。消息类型,此时的消息类型固定为 template_card 。
	MsgType MsgType `json:"msgtype"`
	// TemplateCard 模板卡片
	TemplateCard struct {
		// CardType 模版卡片的模版类型,图文展示模版卡片的类型为 news_notice 。
		CardType CardType `json:"card_type"`
		// Source 可选。卡片来源样式信息。
		Source *Source `json:"source"`
		// MainTitle 模版卡片的主要内容,包括一级标题和标题辅助信息。
		MainTitle MainTitle `json:"main_title"`
		// CardImage 图片样式
		CardImage CardImage `json:"card_image"`
		// ImageTextArea 左图右文样式
		ImageTextArea *ImageTextArea `json:"image_text_area"`
		// QuoteArea 引用文献样式,建议不与关键数据共用。
		QuoteArea *QuoteArea `json:"quote_area"`
		// VerticalContentList 卡片二级垂直内容,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过4。
		VerticalContentList []*VerticalContent `json:"vertical_content_list"`
		// HorizontalContentList 二级标题+文本列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6。
		HorizontalContentList []*HorizontalContent `json:"horizontal_content_list"`
		// JumpList 跳转指引样式的列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过3。
		JumpList []*Jump `json:"jump_list"`
		// CardAction 整体卡片的点击跳转事件,text_notice模版卡片中该字段为必填项。
		CardAction CardAction `json:"card_action"`
	} `json:"template_card"`
}

NewsNoticeTemplateCardMessage 图文展示模版卡片类型消息

type QuoteArea added in v0.2.0

type QuoteArea struct {
	// Type 引用文献样式区域点击事件,0或不填代表没有点击事件,1 代表跳转url,2 代表跳转小程序。
	Type *uint8 `json:"type"`
	// URL 点击跳转的url,type是1时必填。
	URL *string `json:"url"`
	// AppID 点击跳转的小程序的appid,type是2时必填。
	AppID *string `json:"appid"`
	// PagePath 点击跳转的小程序的pagepath,type是2时选填。
	PagePath *string `json:"pagepath"`
	// Title 引用文献样式的标题
	Title *string `json:"title"`
	// QuoteText 引用文献样式的引用文案
	QuoteText *string `json:"quote_text"`
}

QuoteArea 引用文献样式,建议不与关键数据共用。

type ResError

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

ResError 响应错误

func (*ResError) ErrCode

func (e *ResError) ErrCode() int

ErrCode 返回错误码

func (*ResError) ErrMsg

func (e *ResError) ErrMsg() string

ErrMsg 返回错误消息内容

func (*ResError) Error

func (e *ResError) Error() string

Error 返回文本形式的错误描述

type Source added in v0.2.0

type Source struct {
	// IconURL 来源图片的url
	IconURL *string `json:"icon_url"`
	// Desc 来源图片的描述,建议不超过13个字。
	Desc *string `json:"desc"`
	// DescColor 来源文字的颜色,目前支持:0(默认) 灰色,1 黑色,2 红色,3 绿色。
	DescColor *uint8 `json:"desc_color"`
}

Source 卡片来源样式信息

type TextMessage

type TextMessage struct {
	// MsgType 必填。消息类型,此时固定为:text。
	MsgType MsgType `json:"msgtype"`
	Text    struct {
		// Content 必填。文本内容,最长不超过2048个字节,必须是utf8编码。
		Content string `json:"content"`
		// MentionedList 可选。userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list。
		MentionedList []string `json:"mentioned_list"`
		// MentionedMobileList 可选。手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人。
		MentionedMobileList []string `json:"mentioned_mobile_list"`
	} `json:"text"`
}

TextMessage 文本类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#%E6%96%87%E6%9C%AC%E7%B1%BB%E5%9E%8B

type TextNoticeTemplateCardMessage added in v0.2.0

type TextNoticeTemplateCardMessage struct {
	// MsgType 必填。消息类型,此时的消息类型固定为 template_card 。
	MsgType MsgType `json:"msgtype"`
	// TemplateCard 模板卡片
	TemplateCard struct {
		// CardType 模版卡片的模版类型,文本通知模版卡片的类型为 text_notice 。
		CardType CardType `json:"card_type"`
		// Source 可选。卡片来源样式信息。
		Source *Source `json:"source"`
		// MainTitle 模版卡片的主要内容,包括一级标题和标题辅助信息。
		MainTitle MainTitle `json:"main_title"`
		// EmphasisContent 关键数据样式
		EmphasisContent *EmphasisContent `json:"emphasis_content"`
		// QuoteArea 引用文献样式,建议不与关键数据共用。
		QuoteArea *QuoteArea `json:"quote_area"`
		// SubTitleText 二级普通文本,建议不超过112个字。模版卡片主要内容的一级标题main_title.title和二级普通文本sub_title_text必须有一项填写。
		SubTitleText *string `json:"sub_title_text"`
		// HorizontalContentList 二级标题+文本列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过6。
		HorizontalContentList []*HorizontalContent `json:"horizontal_content_list"`
		// JumpList 跳转指引样式的列表,该字段可为空数组,但有数据的话需确认对应字段是否必填,列表长度不超过3。
		JumpList []*Jump `json:"jump_list"`
		// CardAction 整体卡片的点击跳转事件,text_notice模版卡片中该字段为必填项。
		CardAction CardAction `json:"card_action"`
	} `json:"template_card"`
}

TextNoticeTemplateCardMessage 文本通知模版卡片类型消息

type UploadedMedia

type UploadedMedia struct {
	MediaID   string `json:"media_id"`
	CreatedAt string `json:"created_at"`
	// contains filtered or unexported fields
}

UploadedMedia 上传媒体文件结果

func (*UploadedMedia) ToError

func (um *UploadedMedia) ToError() error

type VerticalContent added in v0.2.0

type VerticalContent struct {
	// Title 卡片二级标题,建议不超过26个字。
	Title string `json:"title"`
	// Desc 二级普通文本,建议不超过112个字。
	Desc *string `json:"desc"`
}

VerticalContent 卡片二级垂直内容

type VoiceMessage

type VoiceMessage struct {
	// MsgType 必填。语音类型,此时固定为 voice 。
	MsgType MsgType `json:"msgtype"`
	// 消息内容
	Voice struct {
		// MediaID 必填。语音文件id,通过文件上传接口获取。
		MediaID string `json:"media_id"`
	} `json:"voice"`
}

VoiceMessage 语音类型消息。详见 https://developer.work.weixin.qq.com/document/path/91770#%E8%AF%AD%E9%9F%B3%E7%B1%BB%E5%9E%8B

Jump to

Keyboard shortcuts

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