weixinmp

package
v0.0.0-...-2ec961a Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2023 License: MIT, MIT Imports: 18 Imported by: 0

README

微信公众平台SDK for Go

GoDoc Build Status

这是一个使用Go语言编写的微信公众平台开发接口封装包.

Hello, 世界

获取weixinmp

go get -u github.com/sidbusy/weixinmp

创建server.go

package main

import (
	"log"
	"net/http"
	"github.com/sidbusy/weixinmp"
)

func main() {
	// 注册处理函数
	http.HandleFunc("/receiver", receiver)
	log.Fatal(http.ListenAndServe(":80", nil))
}

func receiver(w http.ResponseWriter, r *http.Request) {
	token := "" // 微信公众平台的Token
	appid := "" // 微信公众平台的AppID
	secret := "" // 微信公众平台的AppSecret
	// 仅被动响应消息时可不填写appid、secret
	// 仅主动发送消息时可不填写token
	mp := weixinmp.New(token, appid, secret)
	// 检查请求是否有效
	// 仅主动发送消息时不用检查
	if !mp.Request.IsValid(w, r) {
		return
	}
	// 判断消息类型
	if mp.Request.MsgType == weixinmp.MsgTypeText {
		// 回复消息
		mp.ReplyTextMsg(w, "Hello, 世界")
	}
}

运行监听服务

go run server.go

客户端消息类型

weixinmp.MsgTypeText 文本消息

weixinmp.MsgTypeImage 图片消息

weixinmp.MsgTypeVoice 语音消息

weixinmp.MsgTypeVideo 视频消息

weixinmp.MsgTypeShortVideo 短视频消息

weixinmp.MsgTypeLocation 地理位置消息

weixinmp.MsgTypeLink 链接消息

weixinmp.MsgTypeEvent 事件推送

weixinmp.EventSubscribe 关注/用户未关注时扫描带参数二维码事件

weixinmp.EventUnsubscribe 取消关注事件

weixinmp.EventScan 用户已关注时扫描带参数二维码事件

weixinmp.EventLocation 上报地理位置事件

weixinmp.EventClick 菜单拉取消息事件

weixinmp.EventView 菜单跳转链接事件

回复消息

mp.ReplyTextMsg(w, "content") 回复文本消息

mp.ReplyImageMsg(w, mediaId) 回复图片消息

mp.ReplyVoiceMsg(w, mediaId) 回复语音消息

mp.ReplyVideoMsg(w, &weixinmp.Video) 回复视频消息

mp.ReplyMusicMsg(w, &weixinmp.Music) 回复音乐消息

mp.ReplyNewsMsg(w, &[]weixinmp.Article) 回复图文消息

mediaId 媒体文件上传后获取的唯一标识

返回error类型值

发送消息

mp.SendTextMsg(touser, "content") 发送文本消息

mp.SendImageMsg(touser, mediaId) 发送图片消息

mp.SendVoiceMsg(touser, mediaId) 发送语音消息

mp.SendVideoMsg(touser, &weixinmp.Video) 发送视频消息

mp.SendMusicMsg(touser, &weixinmp.Music) 发送音乐消息

mp.SendNewsMsg(touser, &[]weixinmp.Article) 发送图文消息

touser 普通用户openid

mediaId 媒体文件上传后获取的唯一标识

返回error类型值

视频、音乐、图文消息结构

视频消息

type Video struct {
	MediaId     string
	Title       string
	Description string
}

音乐消息

type Music struct {
	Title        string
	Description  string
	MusicUrl     string
	HQMusicUrl   string
	ThumbMediaId string
}

图文消息

type Article struct {
	Title       string
	Description string
	PicUrl      string
	Url         string
}

上传多媒体文件

mp.UploadMediaFile(mediaType, filePath) 上传多媒体文件

mediaType 多媒体文件类型(weixinmp.MediaTypeImageweixinmp.MediaTypeVoiceweixinmp.MediaTypeVideoweixinmp.MediaTypeThumb)

filePath 多媒体文件路径

返回(string, error)类型值, 返回的string类型值为媒体文件上传后获取的唯一标识.

下载多媒体文件

mp.DownloadMediaFile(mediaId, filePath) 下载多媒体文件

mediaId 媒体文件上传后获取的唯一标识

返回error类型值

创建二维码

mp.CreateQRScene(sceneId) 创建临时二维码

mp.CreateQRLimitScene(expireSeconds, sceneId) 创建永久二维码

expireSeconds 临时二维码有效时间, 以秒为单位, 最大不超过1800.

sceneId 场景值ID, 临时二维码时为32位非0整型, 永久二维码时最大值为100000 ( 目前参数只支持1-100000 ).

返回(string, error)类型值, 返回的string类型值为获取的二维码ticket, 凭借此ticket可以在有效时间内换取二维码.

换取二维码URL

mp.GetQRCodeURL(ticket)

返回string类型值

创建自定义菜单

mp.CreateCustomMenu(&[]weixinmp.Button) 创建自定义菜单

返回error类型值

查询自定义菜单

mp.GetCustomMenu() 查询自定义菜单

返回([]weixinmp.Button, error)类型值

删除自定义菜单

mp.DeleteCustomMenu() 删除自定义菜单

返回error类型值

相关链接

微信公众平台

微信公众平台开发者文档

微信公众平台接口调试工具

微信公众平台接口测试帐号申请

许可协议

The MIT License (MIT)

更新日志

Release 20151002

  • 修改accesstoken存储规则,支持多个公众号并存

Release 20140608

  • 创建/查询/删除自定义菜单

Release 20140322

  • 上传/下载多媒体文件
  • 创建/换取带参数的二维码

Release 20140318

  • 效验请求真实性
  • 解析请求
  • 获取/缓存access token
  • 回复/发送消息

Documentation

Index

Constants

View Source
const (
	// request message types
	MsgTypeText       = "text"
	MsgTypeImage      = "image"
	MsgTypeVoice      = "voice"
	MsgTypeVideo      = "video"
	MsgTypeShortVideo = "shortvideo"
	MsgTypeLocation   = "location"
	MsgTypeLink       = "link"
	MsgTypeEvent      = "event"
	// event types
	EventSubscribe   = "subscribe"
	EventUnsubscribe = "unsubscribe"
	EventScan        = "SCAN"
	EventLocation    = "LOCATION"
	EventClick       = "CLICK"
	EventView        = "VIEW"
	// media types
	MediaTypeImage = "image"
	MediaTypeVoice = "voice"
	MediaTypeVideo = "video"
	MediaTypeThumb = "thumb"
	// button types
	ButtonTypeClick = "click"
	ButtonTypeView  = "view"
	// environment constants
	UrlPrefix      = "https://api.weixin.qq.com/cgi-bin/"
	MediaUrlPrefix = "http://file.api.weixin.qq.com/cgi-bin/media/"
)

Variables

This section is empty.

Functions

func Http_post_json

func Http_post_json(url_add string, data string) (string, error)

Types

type AccessToken

type AccessToken struct {
	AppId     string
	AppSecret string
	TmpName   string
	LckName   string
	Is_Redis  int
}

func (*AccessToken) Fresh

func (this *AccessToken) Fresh() (string, error)

get fresh access_token string

func (*AccessToken) Get_Jsapi_ticket

func (this *AccessToken) Get_Jsapi_ticket() (string, error)

type Article

type Article struct {
	Title       string `json:"title"`
	Description string `json:"description"`
	PicUrl      string `json:"picurl"`
	Url         string `json:"url"`
}

type Button

type Button struct {
	Type      string   `json:"type,omitempty"`
	Name      string   `json:"name"`
	Key       string   `json:"key,omitempty"`
	Url       string   `json:"url,omitempty"`
	SubButton []Button `json:"sub_button,omitempty"`
}

type Music

type Music struct {
	Title        string `json:"title"`
	Description  string `json:"description"`
	MusicUrl     string `json:"musicurl"`
	HQMusicUrl   string `json:"hqmusicurl"`
	ThumbMediaId string `json:"thumb_media_id"`
}

type Request

type Request struct {
	Token string
	// request common fields
	ToUserName   string
	FromUserName string
	CreateTime   int64
	MsgType      string
	// message request fields
	Content      string
	MsgId        int64
	PicUrl       string
	MediaId      string
	Format       string
	ThumbMediaId string
	LocationX    float64 `xml:"Location_X"`
	LocationY    float64 `xml:"Location_Y"`
	Scale        float64
	Label        string
	Title        string
	Description  string
	Url          string
	Recognition  string
	// event request fields
	Event     string
	EventKey  string
	Ticket    string
	Latitude  float64
	Longitude float64
	Precision float64
}

request from weixinmp

func (*Request) IsValid

func (this *Request) IsValid(rw http.ResponseWriter, req *http.Request) bool

validate request

type UserInfo

type UserInfo struct {
	Subscribe     int64  `json:"subscribe"`
	Openid        string `json:"openid"`
	Nickname      string `json:"nickname"`
	Sex           int64  `json:"sex"`
	Language      string `json:"language"`
	City          string `json:"city"`
	Province      string `json:"province"`
	Country       string `json:"country"`
	Headimgurl    string `json:"headimgurl"`
	SubscribeTime int64  `json:"subscribe_time"`
}

type Video

type Video struct {
	MediaId     string `json:"media_id"`
	Title       string `json:"title"`
	Description string `json:"description"`
}

type Weixinmp

type Weixinmp struct {
	Request     Request
	AccessToken AccessToken
}

func New

func New(token, appId, appSecret string) *Weixinmp

func (*Weixinmp) CreateCustomMenu

func (this *Weixinmp) CreateCustomMenu(btn *[]Button) error

create custom menu

func (*Weixinmp) CreateQRLimitScene

func (this *Weixinmp) CreateQRLimitScene(expireSeconds, sceneId int64) (string, error)

create temporary qrcode

func (*Weixinmp) CreateQRScene

func (this *Weixinmp) CreateQRScene(sceneId int64) (string, error)

create permanent qrcode

func (*Weixinmp) DeleteCustomMenu

func (this *Weixinmp) DeleteCustomMenu() error

delete custom menu

func (*Weixinmp) DownloadMediaFile

func (this *Weixinmp) DownloadMediaFile(mediaId, fileName string) error

download media to file

func (*Weixinmp) GetCustomMenu

func (this *Weixinmp) GetCustomMenu() ([]Button, error)

get custom menu

func (*Weixinmp) GetQRCodeURL

func (this *Weixinmp) GetQRCodeURL(ticket string) string

get qrcode url

func (*Weixinmp) GetUserInfo

func (this *Weixinmp) GetUserInfo(openId string) (UserInfo, error)

get user info

func (*Weixinmp) ReplyImageMsg

func (this *Weixinmp) ReplyImageMsg(rw http.ResponseWriter, mediaId string) error

reply image message

func (*Weixinmp) ReplyMusicMsg

func (this *Weixinmp) ReplyMusicMsg(rw http.ResponseWriter, music *Music) error

reply music message

func (*Weixinmp) ReplyNewsMsg

func (this *Weixinmp) ReplyNewsMsg(rw http.ResponseWriter, articles *[]Article) error

reply news message

func (*Weixinmp) ReplyTextMsg

func (this *Weixinmp) ReplyTextMsg(rw http.ResponseWriter, content string) error

reply text message

func (*Weixinmp) ReplyVideoMsg

func (this *Weixinmp) ReplyVideoMsg(rw http.ResponseWriter, video *Video) error

reply video message

func (*Weixinmp) ReplyVoiceMsg

func (this *Weixinmp) ReplyVoiceMsg(rw http.ResponseWriter, mediaId string) error

reply voice message

func (*Weixinmp) SendImageMsg

func (this *Weixinmp) SendImageMsg(touser string, mediaId string) error

send image message

func (*Weixinmp) SendMusicMsg

func (this *Weixinmp) SendMusicMsg(touser string, music *Music) error

send music message

func (*Weixinmp) SendNewsMsg

func (this *Weixinmp) SendNewsMsg(touser string, articles *[]Article) error

send news message

func (*Weixinmp) SendTextMsg

func (this *Weixinmp) SendTextMsg(touser string, content string) error

send text message

func (*Weixinmp) SendVideoMsg

func (this *Weixinmp) SendVideoMsg(touser string, video *Video) error

send video message

func (*Weixinmp) SendVoiceMsg

func (this *Weixinmp) SendVoiceMsg(touser string, mediaId string) error

send voice message

func (*Weixinmp) Transfer_cust_Msg

func (this *Weixinmp) Transfer_cust_Msg(rw http.ResponseWriter, content string) error

func (*Weixinmp) UploadMediaFile

func (this *Weixinmp) UploadMediaFile(mediaType, fileName string) (string, error)

upload media to file

Jump to

Keyboard shortcuts

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