wechatmp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: MIT Imports: 19 Imported by: 1

README

wechatmp

wechatmp api

rack 服务微信公众号模块

仅供测试使用,更新中

个人微信公众号

简介

根据公众号开发者文档 微信公众平台开发者文档 说明实现

  • 支持 gemini 文本对话
  • 支持自动回复消息
  • 支持超时重试,返回超时信息
demo

微信个人公众号:妙妙答

qr

Documentation

Index

Constants

View Source
const (
	ID            = "wechatmp"
	Comment       = "[module] wechatmp api service"
	RoutePrefix   = "/"
	DefaultEnable = false
)

Variables

View Source
var Module = module.Module{
	ID:      ID,
	Comment: Comment,

	RouteFunc:   AddRoute,
	RoutePrefix: RoutePrefix,

	FlagFunc: ServeFlag,
}

Functions

func AddRoute

func AddRoute(g *gin.Engine)

func CheckSignature

func CheckSignature(c *gin.Context)

func GetAccessToken

func GetAccessToken() (accessToken string, err error)

GetAccessToken 获取 access_token

func SendWechatmpMessageToUser

func SendWechatmpMessageToUser(message WechatmpMessage) error

公众号发送普通文本消息(由于没有认证公众号,暂时未测试)

func ServeFlag

func ServeFlag(serveCmd *cobra.Command)

Types

type AccessToken

type AccessToken struct {
	Token  string
	Mutex  sync.RWMutex
	Expiry time.Time
}
var (
	// 存储消息
	MsgContext = gmap.New()
	// 存储 access_token,用于请求订阅号 api
	WechatmpAccessToken AccessToken
)

type LLMInstance

type LLMInstance interface {
	Ask(question string) (answer string, err error)
	SetModelName(name string)
	SetModelEndpoint(endpoint string)
	SetModelPrompt(prompt string)
	SetSafetyMode(enabled bool)
}
var LLM LLMInstance

type Signature

type Signature struct {
	Signature     string `json:"signature" form:"signature" xml:"signature" validate:"required"`
	Timestamp     string `json:"timestamp" form:"timestamp" xml:"timestamp" validate:"required"`
	Nonce         string `json:"nonce" form:"nonce" xml:"nonce" validate:"required"`
	Echostr       string `json:"echostr" form:"echostr" xml:"echostr" `
	Openid        string `json:"openid" form:"openid" xml:"openid" `
	Encrtpt_type  string `json:"encrtpt_type" form:"encrtpt_type" xml:"encrtpt_type" `
	Msg_signature string `json:"msg_signature" form:"msg_signature" xml:"msg_signature" `
}

signature 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 timestamp 时间戳 nonce 随机数 echostr 随机字符串

type WechatmpMessage

type WechatmpMessage struct {
	XMLName xml.Name `json:"-" xml:"xml"` // 指定 xml 根标签

	ToUserName   string `json:"ToUserName" xml:"ToUserName"`
	FromUserName string `json:"FromUserName" xml:"FromUserName"`
	CreateTime   int64  `json:"CreateTime" xml:"CreateTime"`
	MsgType      string `json:"MsgType" xml:"MsgType"` // event, text, image, voice, video, shortvideo, location, link

	// event
	Event    string `json:"Event" xml:"Event"`       // subscribe, unsubscribe, SCAN, LOCATION, CLICK, VIEW
	EventKey string `json:"EventKey" xml:"EventKey"` // 事件KEY值,qrscene_为前缀,后面为二维码的参数值,或者与自定义菜单接口中KEY值对应

	// text
	Content string `json:"Content" xml:"Content"`
	MsgId   int64  `json:"MsgId" xml:"MsgId"`

	MediaId string `json:"MediaId" xml:"MediaId"` // image, voice, video, shortvideo
	// image
	PicUrl string `json:"PicUrl" xml:"PicUrl"`
	// voice
	Format string `json:"Format" xml:"Format"` // amr, speex
	// video shortvideo
	ThumbMediaId string `json:"ThumbMediaId" xml:"ThumbMediaId"`

	// location
	Location_X float64 `json:"Location_X" xml:"Location_X"`
	Location_Y float64 `json:"Location_Y" xml:"Location_Y"`
	Scale      int64   `json:"Scale" xml:"Scale"`
	Label      string  `json:"Label" xml:"Label"`
	// link
	Title       string `json:"Title" xml:"Title"`
	Description string `json:"Description" xml:"Description"`
	URL         string `json:"URL" xml:"URL"`
}

微信公众号消息结构体

func (*WechatmpMessage) HandleEvent

func (m *WechatmpMessage) HandleEvent() (reply string, err error)

处理消息事件

func (*WechatmpMessage) HandleText

func (m *WechatmpMessage) HandleText() (reply string, err error)

处理文本消息

func (*WechatmpMessage) ReplyTextMessage

func (message *WechatmpMessage) ReplyTextMessage() (reply string, err error)

构造回复消息 - 文本消息

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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