message

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

message 包提供了基本的消息处理

Index

Constants

View Source
const (
	TemplateSendStatusSuccess int8 = 1
	TemplateSendStatusUserBlock
	TemplateSendStatusSystemFailed
)

模板的发送状态值

View Source
const (
	EventTypeSubscribe             = "subscribe"
	EventTypeUnsubscribe           = "unsibuscribe"
	EventTypeScan                  = "SCAN"
	EventTypeLocation              = "LOCATION"
	EventTypeClick                 = "CLICK"
	EventTypeView                  = "VIEW"
	EventTypeTemplateSendJobFinish = "TEMPLATESENDJOBFINISH"
)

模板的事件类型

View Source
const (
	TypeText                    = "text"
	TypeImage                   = "image"
	TypeVoice                   = "voice"
	TypeVideo                   = "video"
	TypeShortVideo              = "shortvideo"
	TypeLocation                = "location"
	TypeLink                    = "link"
	TypeEvent                   = "event"
	TypeTransferCustomerService = "transfer_customer_service" // 只能用于回复消息中
)

消息类型

Variables

View Source
var ReplySuccess = []byte("success")

ReplySuccess 成功返回的内容

Functions

func TransferCustomerService

func TransferCustomerService(m Messager) ([]byte, error)

TransferCustomerService 是 Handler 的一种实现,实现了对消息的转发。

Types

type EventClickView

type EventClickView struct {
	EventKey string `xml:"EventKey"`
	// contains filtered or unexported fields
}

EventClickView 表示点击事件,可以菜单或是链接。

若是点击菜单,则 EventKey 表示菜单的 key,若 点击的是链接,则 EventKey 表示的是要点击的链接。

func (*EventClickView) EventType

func (e *EventClickView) EventType() string

type EventLocation

type EventLocation struct {
	Latitude  float64 `xml:"Latitude"`  // 纬度
	Longitude float64 `xml:"Longitude"` // 经度
	Precision float64 `xml:"Precision"` // 精度
	// contains filtered or unexported fields
}

EventLocation 表示通过扫描带参数的二维码事件

func (*EventLocation) EventType

func (e *EventLocation) EventType() string

type EventScan

type EventScan struct {
	EventKey string `xml:"EventKey"`
	Ticket   string `xml:"Ticket"`
	// contains filtered or unexported fields
}

EventScan 表示通过扫描带参数的二维码事件

subscribe 表示已关注下的扫描事件,SCAN 未关注下的扫描事件 若 IsScan() 为 false,则 subscribe 表示关注,unsbuscribe 表示取消关注

func (*EventScan) EventType

func (e *EventScan) EventType() string

func (*EventScan) IsScan

func (e *EventScan) IsScan() bool

IsScan 是扫描产生的事件还是普通的关注事件

type EventTemplateSendJobFinish

type EventTemplateSendJobFinish struct {
	MsgID  int64  `xml:"MsgID"`
	Status string `xml:"Status"`
	// contains filtered or unexported fields
}

EventTemplateSendJobFinish 模板消息发送事件

func (*EventTemplateSendJobFinish) EventType

func (e *EventTemplateSendJobFinish) EventType() string

func (*EventTemplateSendJobFinish) StatusType

func (e *EventTemplateSendJobFinish) StatusType() int8

StatusType 当前事例的状态

type Eventer

type Eventer interface {
	Messager
	EventType() string
}

Eventer 事件接口

type Handler

type Handler func(Messager) ([]byte, error)

Handler 消息处理函数。 通过向 NewServer 注册 Handler 函数,获取对消息处理的权限。

参数 Messager 为从微信端传递过来的 xml 数据对象实例,都已定义在 messages.go 文件中。

函数的返回值,被当作消息被动回复内容传递给微信调用方。在 reply.go 中 定义了大部分可能用到返回类型,可以拿来直接使用。

NOTE 所有的 Handler 必须在 5 秒内有返回数据,否则微信端会再次发起同样的请求

type HandlerBus

type HandlerBus struct {
	// contains filtered or unexported fields
}

HandlerBus 为 Handler 接口的管理器,方便用户按类别来注册消息处理。

h := &HandlerBus{}
h.RegisterMessage(TypeText, h1)
h.RegisterMessage(TypeImage, h2)
srv := NewServer("token", h.Handler, nil)

func NewHandlerBus

func NewHandlerBus() *HandlerBus

NewHandlerBus 声明一个新的 HandlerBus。

func (*HandlerBus) Handler

func (b *HandlerBus) Handler(m Messager) ([]byte, error)

Handler 实现的 Hnadler 接口

func (*HandlerBus) RegisterEvent

func (b *HandlerBus) RegisterEvent(event string, h Handler)

RegisterEvent 注册事件处理函数

func (*HandlerBus) RegisterMessage

func (b *HandlerBus) RegisterMessage(typ string, h Handler)

RegisterMessage 注册消息处理函数。 typ 的值若为 event,可以注册,但不会实际有作用。

type Image

type Image struct {
	PicURL  string `xml:"PicUrl"`
	MediaID string `xml:"MediaId"`
	// contains filtered or unexported fields
}

Image 图片消息

func (*Image) ID

func (m *Image) ID() int64
type Link struct {
	Title       string `xml:"Title"`
	Description string `xml:"Description"`
	URL         string `xml:"Url"`
	// contains filtered or unexported fields
}

Link 链接消息

func (*Link) ID

func (m *Link) ID() int64

type Location

type Location struct {
	X     float64 `xml:"Location_X"` // 维度
	Y     float64 `xml:"Location_Y"` // 经度
	Scale int     `xml:"Scale"`
	Label string  `xml:"Label"` // 地理位置信息
	// contains filtered or unexported fields
}

Location 位置消息

func (*Location) ID

func (m *Location) ID() int64

type Message

type Message interface {
	Messager

	// 表示消息的 ID
	ID() int64
}

Message 表示消息的基本结构,不包含事件

type Messager

type Messager interface {
	// 消息类型,对应 MsgType 字段
	Type() string

	// 开发者微信号,对应 ToUserName 字段
	To() string

	// 发送方账号,对应 FromUserName 字段
	From() string

	// 创建时间,对应 CreateTime 字段
	Created() int64
}

Messager 表示消息和事件的基本结构。

type ReplyTransferCustomerService

type ReplyTransferCustomerService struct {
	XMLName      xml.Name   `xml:"xml"`
	ToUserName   xxml.CData `xml:"ToUserName"`   // 开发者微信号
	FromUserName xxml.CData `xml:"FromUserName"` // 发送方帐号(一个 OpenID)
	MsgType      xxml.CData `xml:"MsgType"`      // 消息类型
	CreateTime   int64      `xml:"CreateTime"`   // 消息创建时间 (整型)
}

ReplyTransferCustomerService 转发消息

func NewReplyTranferCustomerService

func NewReplyTranferCustomerService(m Messager) *ReplyTransferCustomerService

NewReplyTranferCustomerService 将所有的消息进行转发

func (*ReplyTransferCustomerService) Bytes

func (t *ReplyTransferCustomerService) Bytes() ([]byte, error)

Bytes 返回 []byte 内容

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server 消息管理服务器。

func NewServer

func NewServer(token string, h Handler, errlog *log.Logger) *Server

NewServer 声明一个新的消息管理服务器。

若将 h 参数指定为 nil,则会被自动赋予 TransferCustomerService 函数。 若将 errlog 指定为 nil,则会将错误信息输出到 stderr 中。

func (*Server) Message

func (s *Server) Message(w http.ResponseWriter, r *http.Request)

Message 消息处理,POST 方法

func (*Server) Signature

func (s *Server) Signature(w http.ResponseWriter, r *http.Request)

Signature 验证签名,GET 方法

type ShortVideo

type ShortVideo struct {
	MediaID      string `xml:"MediaId"`
	ThumbMediaID string `xml:"ThumbMediaId"`
	// contains filtered or unexported fields
}

shortVideo 短视频消息

func (*ShortVideo) ID

func (m *ShortVideo) ID() int64

type Text

type Text struct {
	Content string `xml:"Content"` // 文本消息内容
	// contains filtered or unexported fields
}

Text 文本消息

func (*Text) ID

func (m *Text) ID() int64

type Video

type Video struct {
	MediaID      string `xml:"MediaId"`
	ThumbMediaID string `xml:"ThumbMediaId"`
	// contains filtered or unexported fields
}

Video 视频消息

func (*Video) ID

func (m *Video) ID() int64

type Voice

type Voice struct {
	MediaID     string `xml:"MediaId"`
	Format      string `xml:"Format"`
	Recognition string `xml:"Recognition"` // 语音识别结果
	// contains filtered or unexported fields
}

Voice 语音消息

func (*Voice) ID

func (m *Voice) ID() int64

Jump to

Keyboard shortcuts

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