message

package
v2.1.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 1 Imported by: 2

README

消息管理

消息管理

通过wechat.GetServer(request,responseWriter)获取到server对象之后

调用SetMessageHandler(func(msg message.MixMessage){})设置消息的处理函数,函数参数为message.MixMessage 结构如下:

//存放所有微信发送过来的消息和事件
type MixMessage struct {
	MsgCommon

	//基本消息
	MsgID        	int64   	`xml:"MsgId"`
	Content      	string  	`xml:"Content"`
	Recognition  	string  	`xml:"Recognition"`
	PicURL       	string  	`xml:"PicUrl"`
	MediaID      	string  	`xml:"MediaId"`
	Format       	string  	`xml:"Format"`
	ThumbMediaID 	string  	`xml:"ThumbMediaId"`
	LocationX    	float64 	`xml:"Location_X"`
	LocationY    	float64 	`xml:"Location_Y"`
	Scale        	float64 	`xml:"Scale"`
	Label        	string  	`xml:"Label"`
	Title        	string  	`xml:"Title"`
	Description  	string  	`xml:"Description"`
	URL          	string  	`xml:"Url"`

	//事件相关
	Event     		EventType 	`xml:"Event"`
	EventKey  		string    	`xml:"EventKey"`
	Ticket    		string    	`xml:"Ticket"`
	Latitude  		float64    	`xml:"Latitude"`
	Longitude 		float64    	`xml:"Longitude"`
	Precision 		float64    	`xml:"Precision"`
	MenuID    		string    	`xml:"MenuId"`
	Status    		string    	`xml:"Status"`
	SessionFrom     string  	`xml:"SessionFrom"`

	ScanCodeInfo struct {
		ScanType   		string 		`xml:"ScanType"`
		ScanResult 		string 		`xml:"ScanResult"`
	} `xml:"ScanCodeInfo"`

	SendPicsInfo struct {
		Count   	int32      		`xml:"Count"`
		PicList 	[]EventPic 		`xml:"PicList>item"`
	} `xml:"SendPicsInfo"`

	SendLocationInfo struct {
		LocationX 		float64 	`xml:"Location_X"`
		LocationY 		float64 	`xml:"Location_Y"`
		Scale     		float64 	`xml:"Scale"`
		Label     		string  	`xml:"Label"`
		Poiname   		string  	`xml:"Poiname"`
	} `xml:"SendLocationInfo"`
}

具体参数请参考微信文档:接收普通消息

接收普通消息和事件

server.SetMessageHandler(func(v message.MixMessage) *message.Reply {

		//根据微信回调时的消息类型,来相应获取对应消息明细
		switch msg.MsgCommon.MsgType {

			//消息类型
			case "text":
				reMsg = request.GetText(&msg)
			case "image":
				reMsg = request.GetImage(&msg)
			case "voice":
				reMsg = request.GetVoice(&msg)
			case "video":
				reMsg = request.GetVideo(&msg)
			case "shortvideo":
				reMsg = request.GetShortVideo(&msg)
			case "location":
				reMsg = request.GetLocation(&msg)
			case "link":
				reMsg = request.GetLink(&msg)

			//事件类型
			case "event":
				switch msg.Event {
    				case "subscribe":
    					reMsg = request.GetSubscribeEvent(&msg)
    				case "unsubscribe":
    					reMsg = request.GetUnsubscribeEvent(&msg)
    				case "SCAN":
    					reMsg = request.GetScanEvent(&msg)
    				case "CLICK","VIEW":
    					reMsg = request.GetMenuEvent(&msg)
    				case "TEMPLATESENDJOBFINISH":
    					reMsg = request.GetTemplateSendJobFinishEvent(&msg)
				}
		}

		log.Println("消息明细:",reMsg)
}


被动回复消息

回复消息需要返回 *response.Reply 对象结构体如下:

type Reply struct{
	MsgType message.MsgType  //消息类型
	MsgData interface{}     //消息内容
}

注意:return nil表示什么也不做

回复文本消息
	text := response.NewText("回复文本消息")
	return &response.Reply{message.MsgTypeText, text}
回复图片消息
image :=response.NewImage("mediaID")
return &response.Reply{message.MsgTypeVideo, image}
回复语音消息
voice :=response.NewVoice("mediaID")
return &response.Reply{message.MsgTypeVoice, voice}
回复视频消息
video := response.NewVideo("mediaID", "视频标题", "视频描述")
return &response.Reply{message.MsgTypeVideo, video}
回复音乐消息
music := response.NewMusic("title", "description", "musicURL", "hQMusicURL", "thumbMediaID")
return &response.Reply{message.MsgTypeMusic,music}

字段说明:

Title:音乐标题 Description:音乐描述 MusicURL:音乐链接 HQMusicUrl:高质量音乐链接,WIFI环境优先使用该链接播放音乐 ThumbMediaId:缩略图的媒体id,通过素材管理接口上传多媒体文件,得到的id

回复图文消息
articles := make([]*message.Article, 1)
article := response.NewArticle("图文标题","图文消息描述","https://www.baidu.com/img/bd_logo1.png","https://www.baidu.com/")
articles[0] = article
news := response.NewNews(articles)
return &response.Reply{message.MsgTypeNews,news}

字段说明:

Title:图文消息标题 Description:图文消息描述 PicUrl :图片链接,支持JPG、PNG格式,较好的效果为大图360200,小图200200 Url :点击图文消息跳转链接

将消息转发到客服
	transferKf := response.NewTransferKf("")  //可指定客服账号
	return &response.Reply{message.MsgTypeTransfer, transferKf}

模板消息

    config := &wechat.Config{
        AppID:          "your appId",
        AppSecret:      "your appSecret",
        Token:          "your token",
        EncodingAESKey: "your encoding aes key",
        CacheModel: 	"file",  //缓存方式 file,redis,redisCluster
        ThirdAccessToken: false,
    }

    wc := wechat.NewWechat(config)

	//如果ThirdAccessToken=true,则需要把其它项目中的access_token设置到缓存中
	//注意:为避免access_token失效,需要自己维护定时从其它项目中获取设置到缓存中
	//wc.Context.SetThirdAccessToken("your third_access_token",600) //缓存时间单位为秒

	//实例化发送模板消息
	tpl := wc.GetTemplate()


    //模板内容: {{first.DATA}} 投资标的:{{keyword1.DATA}} 当期收益:{{keyword2.DATA}} {{remark.DATA}}

    //模板消息样例
	first := &template.DataItem{
		"亲爱的xxx,您有一笔消费哦~",
		"#FF4040",
	}
	remark:=&template.DataItem{
		"感谢您的使用,祝您生活愉快~",
		"#FF00FF",
	}
	keyword1 := &template.DataItem{
		"500大洋",
		"#CAFF70",
	}
	keyword2:=&template.DataItem{
		"手机",
		"#9932CC",
	}

	msgTpl := new(template.Message)
	msgTpl.ToUser ="oZZwr0REd0cbVxxxQQmicxS3FrI0"
	msgTpl.TemplateID ="CYM5ydWPBFvx__SDFFQN_DwHH8pjO3o1RyjvYqCTdc"
	msgTpl.Data = make(map[string]*template.DataItem)
	msgTpl.Data["first"] = first
	msgTpl.Data["keyword1"] = keyword1
	msgTpl.Data["keyword2"] = keyword2
	msgTpl.Data["remark"] = remark


    //发送模板消息
	re,_ := tpl.Send(msgTpl)
	log.Println("返回发送模板消息的结果:",re)

详细Demo:examples/example/template.go

Documentation

Overview

消息管理

消息中通用信息

Index

Constants

View Source
const (
	// 普通消息类型
	MsgTypeText       MsgType = "text"                      // 文本消息
	MsgTypeImage              = "image"                     // 图片消息
	MsgTypeVoice              = "voice"                     // 语音消息
	MsgTypeVideo              = "video"                     // 视频消息
	MsgTypeShortVideo         = "shortvideo"                // 小视频消息
	MsgTypeLocation           = "location"                  // 地理位置消息
	MsgTypeLink               = "link"                      // 链接消息
	MsgTypeMusic              = "music"                     //音乐消息[限回复]
	MsgTypeNews               = "news"                      //图文消息[限回复]
	MsgTypeTransfer           = "transfer_customer_service" //消息转发到客服
	MsgTypeEvent              = "event"                     //事件推送消息
	MsgTypeNothing            = "nothing"                   //回复空或success
)
View Source
const (
	//普通事件类型
	EventSubscribe             EventType = "subscribe"             //订阅
	EventUnsubscribe                     = "unsubscribe"           //取消订阅
	EventScan                            = "SCAN"                  //已经关注的用户扫描带参数二维码事件
	EventLocation                        = "LOCATION"              //上报地理位置事件
	EventClick                           = "CLICK"                 //点击菜单拉取消息时的事件推送
	EventView                            = "VIEW"                  //点击菜单跳转链接时的事件推送
	EventScancodePush                    = "scancode_push"         //扫码推事件的事件推送
	EventScancodeWaitmsg                 = "scancode_waitmsg"      //扫码推事件且弹出“消息接收中”提示框的事件推送
	EventPicSysphoto                     = "pic_sysphoto"          //弹出系统拍照发图的事件推送
	EventPicPhotoOrAlbum                 = "pic_photo_or_album"    //弹出拍照或者相册发图的事件推送
	EventPicWeixin                       = "pic_weixin"            //弹出微信相册发图器的事件推送
	EventLocationSelect                  = "location_select"       //弹出地理位置选择器的事件推送
	EventTemplateSendJobFinish           = "TEMPLATESENDJOBFINISH" //发送模板消息推送通知
)

Variables

This section is empty.

Functions

This section is empty.

Types

type EncryptedXMLMsg

type EncryptedXMLMsg struct {
	XMLName      struct{} `xml:"xml"  json:"-"`
	ToUserName   string   `xml:"ToUserName" json:"ToUserName"`
	EncryptedMsg string   `xml:"Encrypt"  json:"Encrypt"`
}

安全模式下的消息

type EventPic

type EventPic string

图片事件

type EventType

type EventType string

事件类型

type MixMessage

type MixMessage struct {
	MsgCommon

	//基本消息
	MsgID        int64   `xml:"MsgId"`
	Content      string  `xml:"Content"`
	Recognition  string  `xml:"Recognition"`
	PicURL       string  `xml:"PicUrl"`
	MediaID      string  `xml:"MediaId"`
	Format       string  `xml:"Format"`
	ThumbMediaID string  `xml:"ThumbMediaId"`
	LocationX    float64 `xml:"Location_X"`
	LocationY    float64 `xml:"Location_Y"`
	Scale        float64 `xml:"Scale"`
	Label        string  `xml:"Label"`
	Title        string  `xml:"Title"`
	Description  string  `xml:"Description"`
	URL          string  `xml:"Url"`

	//事件相关
	Event       EventType `xml:"Event"`
	EventKey    string    `xml:"EventKey"`
	Ticket      string    `xml:"Ticket"`
	Latitude    float64   `xml:"Latitude"`
	Longitude   float64   `xml:"Longitude"`
	Precision   float64   `xml:"Precision"`
	MenuID      string    `xml:"MenuId"`
	Status      string    `xml:"Status"`
	SessionFrom string    `xml:"SessionFrom"`

	ScanCodeInfo struct {
		ScanType   string `xml:"ScanType"`
		ScanResult string `xml:"ScanResult"`
	} `xml:"ScanCodeInfo"`

	SendPicsInfo struct {
		Count   int32      `xml:"Count"`
		PicList []EventPic `xml:"PicList>item"`
	} `xml:"SendPicsInfo"`

	SendLocationInfo struct {
		LocationX float64 `xml:"Location_X"`
		LocationY float64 `xml:"Location_Y"`
		Scale     float64 `xml:"Scale"`
		Label     string  `xml:"Label"`
		Poiname   string  `xml:"Poiname"`
	} `xml:"SendLocationInfo"`
}

存放所有微信发送过来的消息和事件

type MsgCommon

type MsgCommon struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string   `xml:"ToUserName"`
	FromUserName string   `xml:"FromUserName"`
	CreateTime   int64    `xml:"CreateTime"`
	MsgType      MsgType  `xml:"MsgType"`
}

消息公共字段

func (*MsgCommon) SetCreateTime

func (msg *MsgCommon) SetCreateTime(createTime int64)

设置创建时间戳

func (*MsgCommon) SetFromUserName

func (msg *MsgCommon) SetFromUserName(fromUserName string)

设置来源公众号

func (*MsgCommon) SetMsgType

func (msg *MsgCommon) SetMsgType(msgType MsgType)

设置消息类型

func (*MsgCommon) SetToUserName

func (msg *MsgCommon) SetToUserName(toUserName string)

设置接收对象

type MsgType

type MsgType string

消息类型

type ResponseEncryptedXMLMsg

type ResponseEncryptedXMLMsg struct {
	XMLName      struct{} `xml:"xml"  json:"-"`
	EncryptedMsg string   `xml:"Encrypt"     json:"Encrypt"`
	MsgSignature string   `xml:"MsgSignature" json:"MsgSignature"`
	Timestamp    int64    `xml:"TimeStamp"    json:"TimeStamp"`
	Nonce        string   `xml:"Nonce"        json:"Nonce"`
}

需要返回的消息体

Directories

Path Synopsis
微信回调消息处理
微信回调消息处理
request
接收微信回调的消息/事件
接收微信回调的消息/事件
response
接收微信回调消息对应的响应回复
接收微信回调消息对应的响应回复
客服消息
客服消息
群发消息
群发消息
模板消息
模板消息

Jump to

Keyboard shortcuts

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