weixin

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

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

Go to latest
Published: Mar 20, 2018 License: MIT Imports: 4 Imported by: 5

README

weixin-kit

企业微信(微信) 开发库, 目前只支持发送消息,微信通过企业微信中的应用来接收消息,可以用来做报警,要接收报警的微信必须在企业微信的通讯录里面。

注意: 微信企业号现已升级为企业微信,微信插件继承原企业号的所有能力。管理员可在企业微信管理后台新建应用、群发通知,成员无需下载客户端,扫码关注微信插件后即可在微信中接收企业通知和使用企业应用。

使用微信报警流程

  1. 这里注册企业微信, 使用管理员微信账号扫码登录企业微信管理后台
  2. 邀请接收报警的人加入企业,首页里面有 邀请方式,可以通过微信扫码的方式加入,确保都在通讯录里面才能接收报警
  3. 企业应用 - 创建应用 - 上传一个Logo,填写应用名称,选择部门/成员 这些人就是通过这个应用接收报警
  4. 最后让所有接收报警的人,扫描 企业应用 - 微信插件 的二维码,即可接收报警。

用法

发送消息

点击查看详细的说明

package main

import (
	"fmt"
	"log"

	weixin "github.com/chanyipiaomiao/weixin-kit"
)

func main() {

    accessTokenAPI := "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
    corpID := "xxxxxx"
    appSecret := "xxxxxxxx"
    sendMessageAPIURL := "https://qyapi.weixin.qq.com/cgi-bin/message/send"
    
    message := &weixin.Message{
        MsgType: weixin.TEXT, // 目前只支持发送文本消息
        ToTag:   "1",         // ToTag 是在企业微信后台定义的标签ID,标签里面可以包含很多人 还有ToUser,ToParty参数 指定用户和部门ID
        AgentID: 1000002,    // 企业应用的id,整型。可在应用的设置页面查看
        Safe:    0,          // 表示是否是保密消息,0表示否,1表示是,默认0
        Text: &weixin.Text{
            Content: "有报警啦, 主机: xxx 报警内容: xxxx",
        },
    }

    client2 := &weixin.Client{
        AccessTokenAPI: accessTokenAPI,
        APIURL:         sendMessageAPIURL,
        CorpID:         corpID,
        CorpSecret:     appSecret,
        Message:        message,
    }
    ok, err := client2.SendMessage()
    if err != nil {
        log.Fatalln(err)
    }
    fmt.Println(ok)
}

Documentation

Index

Constants

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

	// VIDEO 微信视频消息
	VIDEO = "video"

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

	// VOICE 微信语音消息
	VOICE = "voice"

	// FILE 微信文件消息
	FILE = "file"

	// TEXTCARD 微信文本卡片消息
	TEXTCARD = "textcard"

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

	// MPNEWS 微信图文消息
	MPNEWS = "mpnews"
)

Variables

View Source
var (

	// WeixinErr 微信错误信息码
	WeixinErr = func(errcode int64, errmsg string) error {
		return fmt.Errorf("weixin return error, errcode: %d, errmsg: %s", errcode, errmsg)
	}
)

Functions

This section is empty.

Types

type Client

type Client struct {
	AccessTokenAPI string
	APIURL         string
	CorpID         string
	CorpSecret     string
	Message        *Message
}

Client 微信

func (*Client) GetAccessToken

func (c *Client) GetAccessToken() (string, error)

GetAccessToken 获取AccessToken corpid 每个企业都拥有唯一的corpid,获取此信息可在管理后台“我的企业”-“企业信息”下查看(需要有管理员权限) corpsecret 每个应用有独立的secret,所以每个应用的access_token应该分开来获取 在管理后台->“企业应用”->点进应用

func (*Client) SendMessage

func (c *Client) SendMessage() (bool, error)

SendMessage 发送消息

type ImageVoiceFile

type ImageVoiceFile struct {

	// mediaID 图片媒体文件id
	MediaID string `json:"media_id"`
}

ImageVoiceFile 图片|语音|文件 消息 统一用这个

type MPNew

type MPNew struct {

	// title 标题
	Title string `json:"title"`

	// thumbMediaID 图文消息缩略图的media_id
	ThumbMediaID string `json:"thumb_media_id"`

	// author 图文消息的作者
	Author string `json:"author"`

	// contentSourceURL 图文消息点击“阅读原文”之后的页面链接
	ContentSourceURL string `json:"content_source_url"`

	// content 图文消息的内容,支持html标签,不超过666 K个字节
	Content string `json:"content"`

	// digest 图文消息的描述
	Digest string `json:"digest"`
}

MPNew 单个图文消息 跟普通的图文消息一致,唯一的差异是图文内容存储在企业微信

type MPNews

type MPNews struct {
	Articles []MPNew `json:"articles"`
}

MPNews 多次发送mpnews,会被认为是不同的图文,阅读、点赞的统计会被分开计算

type Message

type Message struct {

	// 消息类型 text|voice|image|video|textcard|new|mpnew|file
	MsgType string `json:"msgtype"`

	// 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)特殊情况:指定为@all,则向该企业应用的全部成员发送
	ToUser string `json:"touser"`

	// 部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
	ToParty string `json:"toparty"`

	// 标签ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
	ToTag string `json:"totag"`

	// 企业应用的id,整型。可在应用的设置页面查看
	AgentID int64 `json:"agentid"`

	// 表示是否是保密消息,0表示否,1表示是,默认0
	Safe     int64           `json:"safe"`
	Text     *Text           `json:"text"`
	Image    *ImageVoiceFile `json:"image"`
	Voice    *ImageVoiceFile `json:"voice"`
	File     *ImageVoiceFile `json:"file"`
	Video    *Video          `json:"video"`
	TextCard *TextCard       `json:"textcard"`
	News     []News          `json:"news"`
	MPNews   []MPNews        `json:"mpnews"`
}

Message 微信消息

type New

type New struct {

	// btntxt 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断
	Btntxt string `json:"btntxt"`

	// desc  描述
	Description string `json:"description"`

	// picurl 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图640320,小图8080。
	Picurl string `json:"picurl"`

	// title 标题
	Title string `json:"title"`

	// url   点击后跳转的链接
	URL string `json:"url"`
}

New 单个图文消息

type News

type News struct {
	Articles []New `json:"articles"`
}

News 图文消息

type Text

type Text struct {
	Content string `json:"content"`
}

Text 文本消息

type TextCard

type TextCard struct {

	// title 标题
	Title string `json:"title"`

	// desc  描述
	Description string `json:"description"`

	// url   点击后跳转的链接
	URL string `json:"url"`

	// btntxt 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断
	BtnTxt string `json:"btntxt"`
}

TextCard 文本卡片消息

type Video

type Video struct {

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

	// title 视频消息的标题
	Title string `json:"title"`

	// desc 视频消息的描述
	Description string `json:"description"`
}

Video 视频消息

Jump to

Keyboard shortcuts

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