notify

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2022 License: MIT Imports: 12 Imported by: 0

README

企业微信应用消息发送

Go Report Card

企业微信官方API文档

使用

Use as a library
go get github.com/dongfg/notify

简单示例:

package main

import (
    "fmt"
    "github.com/dongfg/notify"
)

func main() {
    n := notify.New("", 1000001, "") // your config

    result, err := n.Send(notify.MessageReceiver{
        ToUser: "@all",
    }, notify.Text{Content: "Simple Message"}, nil)

    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(result)
    }
}
命令行

Release 页面下载二进制文件,或者通过 go 命令安装:

GO111MODULE="off" go install github.com/dongfg/notify/notify@latest

全局参数:

Flags:
      --config string      config file (default is $HOME/.notify.yaml or .notify.yaml)
      --corpID string      企业ID,https://work.weixin.qq.com/wework_admin/frame#profile
      --agentID int        应用agentID,应用页面查看
      --appSecret string   应用secret,应用页面查看
  -u, --user string        指定接收消息的成员,成员ID列表,多个接收者用‘|’分隔,最多支持1000个。特殊情况:指定为 @all,则向该企业应用的全部成员发送
  -p, --party string       指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。当 user 为 @all 时忽略本参数
  -t, --tag string         指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。当 user 为 @all 时忽略本参数
  -v, --verbose            verbose mode

全局参数配置文件 .notify.yaml 内容参考,可通过 --config 指定,若未指定会从 $HOME 及当前目录查找:

appSecret: LTyXNttXXXXXXXXXXXXXyqtQ9Uw
agentID: 1000002
corpID: ww7XXXXXXXXXd9
user: "@all"
party: "1|2|3"
tag: "t1|t2|t3"

详细参数:

企业微信应用消息发送

Usage:
  notify [command]

Available Commands:
  completion  Generate the autocompletion script for the specified shell
  file        发送文件消息
  help        Help about any command
  image       发送图片消息
  markdown    发送 markdown 消息
  news        发送图文消息
  text        发送文本消息
  textcard    发送文本卡片消息
  upload      上传临时素材
  video       发送视频消息
  voice       发送语音消息

Flags:
      --config string      config file (default is $HOME/.notify.yaml or .notify.yaml)
      --corpID string      企业ID,https://work.weixin.qq.com/wework_admin/frame#profile
      --agentID int        应用agentID,应用页面查看
      --appSecret string   应用secret,应用页面查看
  -u, --user string        指定接收消息的成员,成员ID列表,多个接收者用‘|’分隔,最多支持1000个。特殊情况:指定为 @all,则向该企业应用的全部成员发送
  -p, --party string       指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。当 user 为 @all 时忽略本参数
  -t, --tag string         指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。当 user 为 @all 时忽略本参数
  -v, --verbose            verbose mode
  -h, --help               help for notify

Use "notify [command] --help" for more information about a command.

发送文本消息:

notify text "some text from command line"

发送文件:

notify file ./README.md

发送文本卡片:

notify textcard --title 恭喜你中奖了 --description 明天不用上班 --url www.baidu.com --btntxt 让我看看

其他消息请查看帮助 notify [command] --help

测试情况

  • Text
  • Image
  • Voice
  • Video
  • File
  • TextCard
  • News
  • MpNews
  • Markdown: 仅支持在企业微信查看
  • TaskCard: 仅支持在企业微信查看
  • MiniProgram: 未测试

命令行支持情况

  • Text
  • Image
  • Voice
  • Video
  • File
  • TextCard
  • News
  • MpNews
  • Markdown
  • TaskCard
  • MiniProgram

Todo-List

  • token 异常过期处理
  • 命令行发送其他类型消息
  • 命令行 token 缓存
  • 命令行 completion

Documentation

Overview

Package notify 对企业微信应用消息发送接口进行了封装.

接口文档见:https://work.weixin.qq.com/api/doc/90001/90143/90372

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File added in v1.1.0

type File struct {
	MediaID string `json:"media_id"` // 文件id,可以调用上传临时素材接口获取
}

File 文件消息

type Image added in v1.1.0

type Image struct {
	MediaID string `json:"media_id"` // 图片媒体文件id,可以调用上传临时素材接口获取
}

Image 图片消息

type Markdown added in v1.1.0

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

Markdown Markdown消息

type MessageKey added in v1.3.3

type MessageKey interface {
	// contains filtered or unexported methods
}

type MessageOptions added in v1.1.0

type MessageOptions struct {
	Safe                   bool `json:"safe"`                     // 表示是否是保密消息,默认否
	EnableIDTrans          bool `json:"enable_id_trans"`          // 表示是否开启id转译,默认否
	EnableDuplicateCheck   bool `json:"enable_duplicate_check"`   // 表示是否开启重复消息检查,默认否
	DuplicateCheckInterval int  `json:"duplicate_check_interval"` // 表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
}

MessageOptions 消息配置包括加密、id转译、重复检查等。部分消息类型只支持部分配置详见官方文档

type MessageReceiver added in v1.1.0

type MessageReceiver struct {
	ToUser  string `json:"touser"`  // 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
	ToParty string `json:"toparty"` // 指定接收消息的部门,部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为”@all”时忽略本参数
	ToTag   string `json:"totag"`   // 指定接收消息的标签,标签ID列表,多个接收者用‘|’分隔,最多支持100个。 当touser为”@all”时忽略本参数
}

MessageReceiver 消息接收者 ToUser、ToParty、ToTag 至少一个

type MessageResult added in v1.1.0

type MessageResult struct {
	ErrorCode    int64  `json:"errcode"` // 错误码,0为全部成功
	ErrorMsg     string `json:"errmsg"`
	InvalidUser  string `json:"invaliduser"`
	InvalidParty string `json:"invalidparty"`
	InvalidTag   string `json:"invalidtag"`
}

MessageResult 消息发送结果。如果部分接收人无权限或不存在,发送仍然执行,但会返回无效的部分(即invaliduser或invalidparty或invalidtag),常见的原因是接收人不在应用的可见范围内。 如果全部接收人无权限或不存在,则本次调用返回失败,errcode为81013。 返回包中的userid,不区分大小写,统一转为小写

type MiniProgram added in v1.1.0

type MiniProgram struct {
	AppID             string                   `json:"appid"`                         // 小程序appid,必须是与当前小程序应用关联的小程序
	Page              string                   `json:"page,omitempty"`                // 非必填。点击消息卡片后的小程序页面,仅限本小程序内的页面。该字段不填则消息点击后不跳转。
	Title             string                   `json:"title"`                         // 消息标题,长度限制4-12个汉字(支持id转译)
	Description       string                   `json:"description,omitempty"`         // 非必填。消息描述,长度限制4-12个汉字(支持id转译)
	EmphasisFirstItem bool                     `json:"emphasis_first_item,omitempty"` // 非必填。是否放大第一个 content_item
	ContentItems      []MiniProgramContentItem `json:"content_item,omitempty"`        // 非必填。消息内容键值对,最多允许10个item
}

MiniProgram 小程序通知消息。小程序通知消息只允许小程序应用发送, 从2019年6月28日起,用户收到的小程序通知会出现在各个独立的小程序应用中。 小程序应用仅支持发送小程序通知消息,暂不支持文本、图片、语音、视频、图文等其他类型的消息。 不支持@all全员发送

type MiniProgramContentItem added in v1.1.0

type MiniProgramContentItem struct {
	Key   string `json:"key"`   // 长度10个汉字以内
	Value string `json:"value"` // 长度30个汉字以内(支持id转译)
}

MiniProgramContentItem 小程序通知消息内容键值

type MpNews added in v1.1.0

type MpNews struct {
	Articles []MpNewsArticle `json:"articles"` // 图文消息,一个图文消息支持1到8条图文
}

MpNews 图文消息(mpnews)。mpnews类型的图文消息,跟普通的图文消息一致,唯一的差异是图文内容存储在企业微信。 多次发送mpnews,会被认为是不同的图文,阅读、点赞的统计会被分开计算。

type MpNewsArticle

type MpNewsArticle struct {
	Title            string `json:"title"`                        // 标题,不超过128个字节,超过会自动截断(支持id转译)
	ThumbMediaID     string `json:"thumb_media_id"`               // 图文消息缩略图的media_id, 可以通过素材管理接口获得。此处thumb_media_id即上传接口返回的media_id
	Author           string `json:"author,omitempty"`             // 非必填。图文消息的作者,不超过64个字节
	ContentSourceURL string `json:"content_source_url,omitempty"` // 非必填。图文消息点击“阅读原文”之后的页面链接
	Content          string `json:"content"`                      // 图文消息的内容,支持html标签,不超过666 K个字节(支持id转译)
	Digest           string `json:"digest,omitempty"`             // 非必填。图文消息的描述,不超过512个字节,超过会自动截断(支持id转译)
}

MpNewsArticle 图文消息详情详情

type News added in v1.1.0

type News struct {
	Articles []NewsArticle `json:"articles"` // 图文消息,一个图文消息支持1到8条图文
}

News 图文消息

type NewsArticle

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

NewsArticle 图文消息详情

type Notify added in v1.1.0

type Notify struct {
	TokenPersist   bool
	Token          string
	TokenExpiresAt int64
	// contains filtered or unexported fields
}

Notify reference to call send method

func New added in v1.1.0

func New(corpID string, agentID int64, appSecret string) *Notify

New client,corpID 企业ID,在企业信息页面查看, agentID + appSecret 在应用页面查看

func (*Notify) EnableTokenPersist added in v1.3.0

func (n *Notify) EnableTokenPersist()

func (*Notify) Send added in v1.1.0

func (n *Notify) Send(receiver MessageReceiver, message interface{}, options *MessageOptions) (MessageResult, error)

Send message with options to receiver, options can be nil

func (*Notify) Upload added in v1.2.0

func (n *Notify) Upload(media UploadMedia) (UploadMediaResult, error)

Upload temp media to server

type TaskCard added in v1.1.0

type TaskCard struct {
	Title       string           `json:"title"`         // 标题,不超过128个字节,超过会自动截断(支持id转译)
	Description string           `json:"description"`   // 描述,不超过512个字节,超过会自动截断(支持id转译)
	URL         string           `json:"url,omitempty"` // 非必填。点击后跳转的链接。最长2048字节,请确保包含了协议头(http/https)
	TaskID      string           `json:"task_id"`       // 任务id,同一个应用发送的任务卡片消息的任务id不能重复,只能由数字、字母和“_-@.”组成,最长支持128字节
	Buttons     []TaskCardButton `json:"btn"`           // 按钮列表,按钮个数为为1~2个
}

TaskCard 任务卡片消息。仅企业微信2.8.2及以上版本支持

type TaskCardButton added in v1.1.0

type TaskCardButton struct {
	Key         string `json:"key"`          // 按钮key值,用户点击后,会产生任务卡片回调事件,回调事件会带上该key值,只能由数字、字母和“_-@.”组成,最长支持128字节
	Name        string `json:"name"`         // 按钮名称
	ReplaceName string `json:"replace_name"` // 非必填。 点击按钮后显示的名称,默认为“已处理”
	Color       string `json:"color"`        // 非必填。 按钮字体颜色,可选“red”或者“blue”,默认为“blue”
	IsBold      bool   `json:"is_bold"`      // 非必填。按钮字体是否加粗,默认false
}

TaskCardButton 任务卡片消息操作按钮

type Text added in v1.1.0

type Text struct {
	Content string `json:"content"` // 消息内容,最长不超过2048个字节,超过将截断(支持id转译)
}

Text 文本消息

type TextCard added in v1.1.0

type TextCard struct {
	Title       string `json:"title"`            // 标题,不超过128个字节,超过会自动截断(支持id转译)
	Description string `json:"description"`      // 描述,不超过512个字节,超过会自动截断(支持id转译)
	URL         string `json:"url"`              // 点击后跳转的链接
	BtnTxt      string `json:"btntxt,omitempty"` // 非必填。按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断
}

TextCard 文本卡片消息

type UploadMedia added in v1.2.0

type UploadMedia struct {
	Type string
	Path string
}

type UploadMediaResult added in v1.2.0

type UploadMediaResult struct {
	ErrorCode int64  `json:"errcode"` // 错误码,0为全部成功
	ErrorMsg  string `json:"errmsg"`
	Type      string `json:"type"`
	MediaID   string `json:"media_id"`
	CreatedAt string `json:"created_at"`
}

type Video added in v1.1.0

type Video struct {
	MediaID     string `json:"media_id"`              // 视频媒体文件id,可以调用上传临时素材接口获取
	Title       string `json:"title,omitempty"`       // 非必填。视频消息的标题,不超过128个字节,超过会自动截断
	Description string `json:"description,omitempty"` // 非必填。视频消息的描述,不超过512个字节,超过会自动截断
}

Video 视频消息

type Voice added in v1.1.0

type Voice struct {
	MediaID string `json:"media_id"` // 语音文件id,可以调用上传临时素材接口获取
}

Voice 语音消息

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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