gowechat / example Goto Github PK
View Code? Open in Web Editor NEW【例子】微信开发SDK(Wechat SDK For Golang)
Home Page: http://silenceper.com/wechat
License: Apache License 2.0
【例子】微信开发SDK(Wechat SDK For Golang)
Home Page: http://silenceper.com/wechat
License: Apache License 2.0
➜ wxoff go run main.go
wechat server listener at :8001
服务器已经抛弃了了,但是微信后台提示提交时失败
package main
import (
"fmt"
"net/http"
wechat "github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
offConfig "github.com/silenceper/wechat/v2/officialaccount/config"
"github.com/silenceper/wechat/v2/officialaccount/message"
)
func serveWechat(rw http.ResponseWriter, req *http.Request) {
wc := wechat.NewWechat()
//这里本地内存保存access_token,也可选择redis,�memcache或者自定cache
memory := cache.NewMemory()
cfg := &offConfig.Config{
AppID: "xxx",
AppSecret: "xxx",
Token: "xxx",
//EncodingAESKey: "xxxx",
Cache: memory,
}
officialAccount := wc.GetOfficialAccount(cfg)
// 传入request和responseWriter
server := officialAccount.GetServer(req, rw)
//设置接收消息的处理方法
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
//TODO
//回复消息:演示回复用户发送的消息
text := message.NewText(msg.Content)
return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
})
//处理消息接收以及回复
err := server.Serve()
if err != nil {
fmt.Println(err)
return
}
//发送回复的消息
server.Send()
}
func main() {
http.HandleFunc("/", serveWechat)
fmt.Println("wechat server listener at", ":8001")
err := http.ListenAndServe(":8001", nil)
if err != nil {
fmt.Printf("start server error , err=%v", err)
}
}
参数我已经修改了
实例里返回消息时返回类型写错了
func (ex *ExampleOfficialAccount) Serve(c *gin.Context) { // 传入request和responseWriter server := ex.officialAccount.GetServer(c.Request, c.Writer) server.SkipValidate(true) //设置接收消息的处理方法 server.SetMessageHandler(func(msg *message.MixMessage) *message.Reply { //TODO //回复消息:演示回复用户发送的消息 text := message.NewText(msg.Content) // text := message.NewText("") -- return &message.Reply{MsgType: message.MsgTypeImage, MsgData: text} ++ return &message.Reply{MsgType: message.MsgTypeText, MsgData: text} ......
cannot use c.Request (variable of type *http.Request) as *http.Request value in argument to officialAccount.GetServercompiler
field Request *http.Request
(gin.Context).Request on pkg.go.dev
No quick fixes available
func (mp *MP) Serve(c *gin.Context) {
newMP := mp.NewMP()
officialAccount := newMP.OpenPlatform.GetOfficialAccount(newMP.Options.AppId)
officialAccount.GetServer(c.Request, c.Writer)
}
RT
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.