wechatbot

package
v0.0.0-...-afa1935 Latest Latest
Warning

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

Go to latest
Published: May 8, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

群机器人配置说明

如何使用群机器人

  • 在终端某个群组添加机器人之后,创建者可以在机器人详情页看的该机器人特有的 webhookurl。开发者可以按以下说明a向这个地址发起HTTP POST 请求,即可实现给该群组发送消息。下面举个简单的例子. 假设 webhook 是:

    https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693a91f6-7xxx-4bc4-97a0-0ec2sifa5aaa
    

    特别特别要注意:一定要 保护好机器人的webhook地址 ,避免泄漏!不要分享到github、博客等可被公开查阅的地方,否则坏人就可以用你的机器人来发垃圾消息了。

以下是用curl工具往群组推送文本消息的示例(注意要将url替换成你的机器人webhook地址,content必须是utf8编码):

curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=693axxx6-7aoc-4bc4-97a0-0ec2sifa5aaa' \
   -H 'Content-Type: application/json' \
   -d '
   {
        "msgtype": "text",
        "text": {
            "content": "hello world"
        }
   }'
  • 当前自定义机器人支持文本(text)、markdown(markdown)、图片(image)、图文(news)四种消息类型。
  • 机器人的text/markdown类型消息支持在content中使用<@userid>扩展语法来@群成员

请求示例

package wechatbot

import "testing"

// 企业微信 企业内部开发 客户端API 群机器人 文档: https://work.weixin.qq.com/api/doc/90000/90136/91770
func TestWechatBot(t *testing.T) {
	webHookUrl := "https://qyapi.weixin.qq.com/cgi-bin/webhook/send"
	key := "xxxxxx"
	mentionedList := []string{"xxx"}
	mentionedMobileList := []string{""}
	msg := "test msg!"

	message := &MessageType{
		MsgType: TEXT, // 消息类型, 支持: text,markdown,image,news,file
		Text: &TextType{
			Content:             msg,
			MentionedList:       mentionedList,
			MentionedMobileList: mentionedMobileList,
		},
	}
	client := NewClient(webHookUrl, key, message)
	ok, err := client.SendMessage()
	if ok {
		t.Log("send successfully!")
	} else {
		t.Fatalf("send faild, error:%v", err)
	}
}

Documentation

Index

Constants

View Source
const (
	// TEXT 文本消息
	TEXT = "text"

	// MARKDOWN markdown消息
	MARKDOWN = "markdown"

	// IMAGE 图片消息
	IMAGE = "image"

	// NEWS 图文消息
	NEWS = "news"

	// FILE 文件消息
	FILE = "file"
)

企业微信 企业内部开发 客户端API 群机器人 文档: https://work.weixin.qq.com/api/doc/90000/90136/91770

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientType

type ClientType struct {
	WebHookUrl string // https://qyapi.weixin.qq.com/cgi-bin/webhook/send
	Key        string // 6b7372c1-53d0-47a9-941f-6adcb888888a
	Message    *MessageType
}

ClientType 客户端

func NewClient

func NewClient(webHookUrl string, key string, message *MessageType) *ClientType

func (*ClientType) SendMessage

func (client *ClientType) SendMessage() (bool, error)

SendMessage 发送消息

type ErrorType

type ErrorType struct {
	ErrCode int    `json:"errcode"` // 出错返回码,为0表示成功,非0表示调用失败
	ErrMsg  string `json:"errmsg"`  // 返回码提示语
}

ErrorType 发送消息返回错误

type FileType

type FileType struct {
	MediaId []NewType `json:"media_id"` // 是否必须: 是 , 文件id,通过下文的文件上传接口获取
}

FileType 文件类型

type ImageType

type ImageType struct {
	Base64 string `json:"base64"` // 是否必须: 是 , 图片内容的base64编码
	Md5    string `json:"md5"`    // 是否必须: 是 , 图片内容(base64编码前)的md5值
}

ImageType 图片类型

type MarkdownType

type MarkdownType struct {
	Content string `json:"content"` // 是否必须: 是 , markdown内容,最长不超过4096个字节,必须是utf8编码
}

MarkdownType markdown 类型

type MessageType

type MessageType struct {
	MsgType  string        `json:"msgtype"`  // 是否必须: 是 , 消息类型, 支持: text,markdown,image,news,file
	Text     *TextType     `json:"text"`     // 文本类型
	Markdown *MarkdownType `json:"markdown"` // markdown 类型
	Image    *ImageType    `json:"image"`    // 图片类型
	News     *NewsType     `json:"news"`     // 图文类型
	File     *FileType     `json:"file"`     // 文件类型
}

MessageType 微信消息

type NewType

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

NewType 单个图文消息

type NewsType

type NewsType struct {
	Articles []NewType `json:"articles"` // 是否必须: 是 , 图文消息,一个图文消息支持1到8条图文
}

NewsType 图文类型

type TextType

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

TextType 文本类型

Jump to

Keyboard shortcuts

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