mirror of
https://github.com/silenceper/wechat.git
synced 2026-02-04 12:52:27 +08:00
Compare commits
6 Commits
v2.0.2
...
v2.0.3-bet
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0505969439 | ||
|
|
3014901b48 | ||
|
|
2e191c0a44 | ||
|
|
4c35924b8c | ||
|
|
ed508654a1 | ||
|
|
a18fe5b58a |
@@ -50,4 +50,12 @@ issues:
|
||||
linters-settings:
|
||||
funlen:
|
||||
lines: 66
|
||||
statements: 40
|
||||
statements: 40
|
||||
|
||||
issues:
|
||||
include:
|
||||
- EXC0002 # disable excluding of issues about comments from golint
|
||||
exclude-rules:
|
||||
- linters:
|
||||
- stylecheck
|
||||
text: "ST1000:"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//Package config 小程序config配置
|
||||
package config
|
||||
|
||||
import (
|
||||
|
||||
57
miniprogram/message/consts.go
Normal file
57
miniprogram/message/consts.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package message
|
||||
|
||||
import "encoding/xml"
|
||||
|
||||
// MsgType 基本消息类型
|
||||
type MsgType string
|
||||
|
||||
// EventType 事件类型
|
||||
type EventType string
|
||||
|
||||
// InfoType 第三方平台授权事件类型
|
||||
type InfoType string
|
||||
|
||||
const (
|
||||
//MsgTypeText 文本消息
|
||||
MsgTypeText MsgType = "text"
|
||||
//MsgTypeImage 图片消息
|
||||
MsgTypeImage = "image"
|
||||
//MsgTypeLink 图文链接
|
||||
MsgTypeLink = "link"
|
||||
//MsgTypeMiniProgramPage 小程序卡片
|
||||
MsgTypeMiniProgramPage = "miniprogrampage"
|
||||
)
|
||||
|
||||
// CommonToken 消息中通用的结构
|
||||
type CommonToken struct {
|
||||
XMLName xml.Name `xml:"xml"`
|
||||
ToUserName string `xml:"ToUserName"`
|
||||
FromUserName string `xml:"FromUserName"`
|
||||
CreateTime int64 `xml:"CreateTime"`
|
||||
MsgType MsgType `xml:"MsgType"`
|
||||
}
|
||||
|
||||
// MiniProgramMixMessage 小程序回调的消息结构
|
||||
type MiniProgramMixMessage struct {
|
||||
CommonToken
|
||||
|
||||
MsgID int64 `xml:"MsgId"`
|
||||
|
||||
// 文本消息
|
||||
Content string `xml:"Content"`
|
||||
|
||||
// 图片消息
|
||||
PicURL string `xml:"PicUrl"`
|
||||
MediaID string `xml:"MediaId"`
|
||||
|
||||
// 小程序卡片消息
|
||||
Title string `xml:"Title"`
|
||||
AppID string `xml:"AppId"`
|
||||
PagePath string `xml:"PagePath"`
|
||||
ThumbURL string `xml:"ThumbUrl"`
|
||||
ThumbMediaID string `xml:"ThumbMediaId"`
|
||||
|
||||
// 进入会话事件
|
||||
Event string `xml:"Event"`
|
||||
SessionFrom string `xml:"SessionFrom"`
|
||||
}
|
||||
124
miniprogram/message/customer_message.go
Normal file
124
miniprogram/message/customer_message.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||
"github.com/silenceper/wechat/v2/util"
|
||||
)
|
||||
|
||||
const (
|
||||
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
|
||||
)
|
||||
|
||||
//Manager 消息管理者,可以发送消息
|
||||
type Manager struct {
|
||||
*context.Context
|
||||
}
|
||||
|
||||
//NewCustomerMessageManager 实例化消息管理者
|
||||
func NewCustomerMessageManager(context *context.Context) *Manager {
|
||||
return &Manager{
|
||||
context,
|
||||
}
|
||||
}
|
||||
|
||||
//MediaText 文本消息的文字
|
||||
type MediaText struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
//MediaResource 消息使用的临时素材id
|
||||
type MediaResource struct {
|
||||
MediaID string `json:"media_id"`
|
||||
}
|
||||
|
||||
//MediaMiniprogrampage 小程序卡片
|
||||
type MediaMiniprogrampage struct {
|
||||
Title string `json:"title"`
|
||||
Appid string `json:"appid"`
|
||||
Pagepath string `json:"pagepath"`
|
||||
ThumbMediaID string `json:"thumb_media_id"`
|
||||
}
|
||||
|
||||
// MediaLink 发送图文链接
|
||||
type MediaLink struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
URL string `json:"url"`
|
||||
ThumbURL string `json:"thumb_url"`
|
||||
}
|
||||
|
||||
//CustomerMessage 客服消息
|
||||
type CustomerMessage struct {
|
||||
ToUser string `json:"touser"` //接受者OpenID
|
||||
Msgtype MsgType `json:"msgtype"` //客服消息类型
|
||||
Text *MediaText `json:"text,omitempty"` //可选
|
||||
Image *MediaResource `json:"image,omitempty"` //可选
|
||||
Link *MediaLink `json:"link,omitempty"` //可选
|
||||
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
|
||||
}
|
||||
|
||||
//NewCustomerTextMessage 文本消息结构体构造方法
|
||||
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
|
||||
return &CustomerMessage{
|
||||
ToUser: toUser,
|
||||
Msgtype: MsgTypeText,
|
||||
Text: &MediaText{
|
||||
Content: text,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//NewCustomerImgMessage 图片消息的构造方法
|
||||
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
|
||||
return &CustomerMessage{
|
||||
ToUser: toUser,
|
||||
Msgtype: MsgTypeImage,
|
||||
Image: &MediaResource{
|
||||
MediaID: mediaID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//NewCustomerLinkMessage 图文链接消息的构造方法
|
||||
func NewCustomerLinkMessage(toUser, title, description, url, thumbURL string) *CustomerMessage {
|
||||
return &CustomerMessage{
|
||||
ToUser: toUser,
|
||||
Msgtype: MsgTypeLink,
|
||||
Link: &MediaLink{
|
||||
Title: title,
|
||||
Description: description,
|
||||
URL: url,
|
||||
ThumbURL: thumbURL,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
|
||||
func NewCustomerMiniprogrampageMessage(toUser, title, pagepath, thumbMediaID string) *CustomerMessage {
|
||||
return &CustomerMessage{
|
||||
ToUser: toUser,
|
||||
Msgtype: MsgTypeMiniProgramPage,
|
||||
Miniprogrampage: &MediaMiniprogrampage{
|
||||
Title: title,
|
||||
Pagepath: pagepath,
|
||||
ThumbMediaID: thumbMediaID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//Send 发送客服消息
|
||||
func (manager *Manager) Send(msg *CustomerMessage) error {
|
||||
accessToken, err := manager.Context.GetAccessToken()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
|
||||
response, err := util.PostJSON(uri, msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return util.DecodeWithCommonError(response, "SendCustomerMessage")
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/silenceper/wechat/v2/miniprogram/config"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/context"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/encryptor"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/message"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/qrcode"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/subscribe"
|
||||
"github.com/silenceper/wechat/v2/miniprogram/tcb"
|
||||
@@ -66,3 +67,8 @@ func (miniProgram *MiniProgram) GetTcb() *tcb.Tcb {
|
||||
func (miniProgram *MiniProgram) GetSubscribe() *subscribe.Subscribe {
|
||||
return subscribe.NewSubscribe(miniProgram.ctx)
|
||||
}
|
||||
|
||||
// GetCustomerMessage 客服消息接口
|
||||
func (miniProgram *MiniProgram) GetCustomerMessage() *message.Manager {
|
||||
return message.NewCustomerMessageManager(miniProgram.ctx)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ const (
|
||||
//发送订阅消息
|
||||
//https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
|
||||
subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
|
||||
|
||||
// 获取当前帐号下的个人模板列表
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
||||
getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
|
||||
)
|
||||
|
||||
// Subscribe 订阅消息
|
||||
@@ -38,6 +42,21 @@ type DataItem struct {
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
//TemplateItem template item
|
||||
type TemplateItem struct {
|
||||
PriTmplID string `json:"priTmplId"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
Example string `json:"example"`
|
||||
Type int64 `json:"type"`
|
||||
}
|
||||
|
||||
//TemplateList template list
|
||||
type TemplateList struct {
|
||||
util.CommonError
|
||||
Data []TemplateItem `json:"data"`
|
||||
}
|
||||
|
||||
// Send 发送订阅消息
|
||||
func (s *Subscribe) Send(msg *Message) (err error) {
|
||||
var accessToken string
|
||||
@@ -52,3 +71,23 @@ func (s *Subscribe) Send(msg *Message) (err error) {
|
||||
}
|
||||
return util.DecodeWithCommonError(response, "Send")
|
||||
}
|
||||
|
||||
//ListTemplates 获取当前帐号下的个人模板列表
|
||||
// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
||||
func (s *Subscribe) ListTemplates() (*TemplateList, error) {
|
||||
accessToken, err := s.GetAccessToken()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uri := fmt.Sprintf("%s?access_token=%s", getTemplateURL, accessToken)
|
||||
response, err := util.HTTPGet(uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
templateList := TemplateList{}
|
||||
err = util.DecodeWithError(response, &templateList, "ListTemplates")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &templateList, nil
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//Package device 设备相关接口
|
||||
package device
|
||||
|
||||
import (
|
||||
|
||||
@@ -73,6 +73,20 @@ func NewCustomerVoiceMessage(toUser, mediaID string) *CustomerMessage {
|
||||
}
|
||||
}
|
||||
|
||||
//NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
|
||||
func NewCustomerMiniprogrampageMessage(toUser, title, appID, pagePath, thumbMediaID string) *CustomerMessage {
|
||||
return &CustomerMessage{
|
||||
ToUser: toUser,
|
||||
Msgtype: MsgTypeMiniprogrampage,
|
||||
Miniprogrampage: &MediaMiniprogrampage{
|
||||
Title: title,
|
||||
AppID: appID,
|
||||
Pagepath: pagePath,
|
||||
ThumbMediaID: thumbMediaID,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
//MediaText 文本消息的文字
|
||||
type MediaText struct {
|
||||
Content string `json:"content"`
|
||||
@@ -134,7 +148,7 @@ type MediaWxcard struct {
|
||||
//MediaMiniprogrampage 小程序消息
|
||||
type MediaMiniprogrampage struct {
|
||||
Title string `json:"title"`
|
||||
Appid string `json:"appid"`
|
||||
AppID string `json:"appid"`
|
||||
Pagepath string `json:"pagepath"`
|
||||
ThumbMediaID string `json:"thumb_media_id"`
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ const (
|
||||
MsgTypeVoice = "voice"
|
||||
//MsgTypeVideo 表示视频消息
|
||||
MsgTypeVideo = "video"
|
||||
//MsgTypeMiniprogrampage 表示小程序卡片消息
|
||||
MsgTypeMiniprogrampage = "miniprogrampage"
|
||||
//MsgTypeShortVideo 表示短视频消息[限接收]
|
||||
MsgTypeShortVideo = "shortvideo"
|
||||
//MsgTypeLocation 表示坐标消息[限接收]
|
||||
|
||||
@@ -106,7 +106,7 @@ func (tpl *Template) List() (templateList []*TemplateItem, err error) {
|
||||
return
|
||||
}
|
||||
var res resTemplateList
|
||||
err = util.DecodeWithError(response, res, "ListTemplate")
|
||||
err = util.DecodeWithError(response, &res, "ListTemplate")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -93,6 +93,11 @@ func (officialAccount *OfficialAccount) GetTemplate() *message.Template {
|
||||
return message.NewTemplate(officialAccount.ctx)
|
||||
}
|
||||
|
||||
// GetCustomerMessageManager 客服消息接口
|
||||
func (officialAccount *OfficialAccount) GetCustomerMessageManager() *message.Manager {
|
||||
return message.NewMessageManager(officialAccount.ctx)
|
||||
}
|
||||
|
||||
// GetDevice 获取智能设备的实例
|
||||
func (officialAccount *OfficialAccount) GetDevice() *device.Device {
|
||||
return device.NewDevice(officialAccount.ctx)
|
||||
|
||||
@@ -132,7 +132,7 @@ func (user *User) GetTag() (tags []*TagInfo, err error) {
|
||||
return result.Tags, nil
|
||||
}
|
||||
|
||||
//OpenidListByTag 获取标签下粉丝列表
|
||||
//OpenIDListByTag 获取标签下粉丝列表
|
||||
func (user *User) OpenIDListByTag(tagID int32, nextOpenID ...string) (userList *TagOpenIDList, err error) {
|
||||
accessToken, err := user.GetAccessToken()
|
||||
if err != nil {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//Package context 开放平台相关context
|
||||
package context
|
||||
|
||||
import (
|
||||
|
||||
@@ -2,7 +2,7 @@ package util
|
||||
|
||||
import "time"
|
||||
|
||||
//GetCurrTs return current timestamps
|
||||
//GetCurrTS return current timestamps
|
||||
func GetCurrTS() int64 {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user