go-wx-gateway
is a WeChat public account gateway service implemented based on go-wx-api.go-wx-gateway
is a reverse proxy program for the API interface of WeChat official account platform.go-wx-gateway
does not simply transfer the data related to the WeChat public account API, but unpacks and packages the packets it sends and receives, so that the business behind the go-wx-gateway
reverse proxy can be realized greatly simplifiedgo-wx-gateway
is run, it communicates with the back-end business service through http, and any language that supports http service can implement business code. go-wx-gateway
, the development of WeChat public account services is the development of ordinary web services.go-wx-gateway
go-wx-gateway
take over the unified management. go-wx-gateway
will forward the user information and menu state value to the menu processing service.go-wx-gateway
to handle web page authorization, the public account can also point to the service you need, and then transfer the obtained code go-wx-gateway
in exchange for accessing the user's openId to obtain complete user information. For this method, refer to the configuration information "sns-auth2".go-wx-gateway
and business code, and the data format is JSON. Prerequisite: go 1.11.x and above, git, make have been installed
Enter any folder and execute the command
$ git clone https://github.com/rosbit/go-wx-gateway
$ cd go-wx-gateway
$ make
If the compilation is successful, the executable program of wx-gateway
will be obtained. You can execute ./wx-gateway -v
to display program information.
The binary version of Linux can be downloaded directly from releases.
is a JSON
Example, can be modified through sample.conf.json
:
{
"listen-host" : " " ,
"listen-port" : 7080 ,
"services" : [
{
"name" : "一个名字,随便取" ,
"workerNum" : 5 ,
"timeout" : 0 ,
"wx-params" : {
"token" : "开发/服务器配置中的Token " ,
"app-id" : "开发/公众号开发信息中的AppId " ,
"app-secret" : "开发/公众号开发信息中的AppSecret " ,
"aes-key" : "开发/服务器配置中的EncodingAESKey。如果是明文传输,则该串为空或null "
},
"listen-endpoints" : {
"service-path" : " /wx --开发/服务器配置/服务器地址中的路径部分" ,
"redirect-path" : " /redirect --这个是微信网页授权用到的,设置菜单时都用这个路径,可选"
},
"msg-proxy-pass" : " http://yourhost.or.ip.here --这个地址指向消息/事件处理的服务,如果不处理可以为空" ,
"redirect-url" : " http://yourhost.or.ip/path/to/redirect --完全转发http请求,响应将返回微信服务号,可选" ,
"redirect-userinfo-flag" : "如果通过 snsapi_userinfo 获取参数,在redrect-url中加上特殊字符串参数,用于区分,比如 login。如果为空,使用 snsapi_base 方式获取用户参数"
},
{
"name" : "如果有其它的公众号服务,可以参考上面的信息配置" ,
"注意" : " listen-endpoints中的路径 不能 相同" ,
"视频号小店支持" : "如果是视频号小店,加上 " is-channels-ec " : true 配置项,其中的 " msg-proxy-pass " 配置的是接收视频号小店推送的事件"
}
],
"token-cache-dir" : "缓存access的根路径,这是可选的配置" ,
"common-endpoints" : {
"health-check" : " /health --这是可选的配置,用于http健康检查,该路由配置成内部可访问" ,
"wx-qr" : " /qr --这是可选的路由配置,可以配置成内部可访问的,用于生成微信二维码链接" ,
"wx-qr的参数说明" : " s=<服务名,对应services中的name>&t=temp|forever[&sceneid=<场景id>][&e=<t为temp时的有效秒数>] " ,
"wx-user" : " /user --这是可选的路由配置,可以配置成内部可访问的,用于获取用户信息,参数:s=<服务名>&o=<openId> " ,
"sns-auth2" : " /sns-auth2 -- 这是可选的路由配置,如果网页授权由其它服务接收,可以通过网页授权参数code获取用户信息" ,
"sns-auth2参数说明" : " s=<服务名,对应services中的name>&code=<网页授权得到的code>&[scope=userinfo|base|snsapi_userinfo|snsn_api_base] " ,
"short-url" : " /short-url -- 这是可选的路由配置,用于把长url生成短链接。【注】微信已经停止该服务" ,
"short-url参数说明" : "访问方法POST, POST body: s=<服务名,对应services中的name>&u=<url编码的长URL> " ,
"tmpl-msg" : " /tmpl-msg -- 这是可选的路由配置,用于发送模版消息" ,
"tmpl-msg参数说明" : "访问方法POST, POST body是JSON " ,
"tmpl-msg body例子" : { "s" : "服务名,对应services中的name " , "to" : "用户openid " , "tid" : "模版id " , "url" : "可选,跳转url " , "mp" :{ "说明" : "可选的小程序参数" , "appid" : "小程序appid " , "pagepath" : "页面路径" }, "data" :{ "模版数据key" : "数据" , "数据key2" : " ... " }},
"sign-jsapi" : " /sign-jsapi -- 这是可选的路由配置,用于生产jsapi签名" ,
"sign-jsapi参数说明" : "访问方法POST, POST body: s=<service-name-in-conf>&u=<url-calling-jsapi-in-urlencoding>,结果会返回noncestr, timestamp, signature等结果" ,
"channels-ec-order-detail" : " /channles-ec-order-detail -- 可选,用于获取视频号小店订单详情" ,
"channels-ec-order-detail参数说明" : " s=<服务名,对应services中的name>&o=<订单id> " ,
"channels-ec-refund-detail" : " /channels-ec-refund-detail -- 可选,用于获取视频号小店售后单详情" ,
"channels-ec-refund-detail参数说明" : " s=<服务名,对应services中的name>&o=<售后订单id> " ,
},
"dont-append-userinfo" : " true|false, 各种消息事件是否不增加用户信息,缺省是false,表示追加"
}
$ CONF_FILE=./wx-gateway-conf.json ./wx-gateway
If msg-proxy-pass
is not configured, wx-gateway
can be used as a tool to enable开发/服务器配置
, and can implement echo responses through text boxes for users who follow public accounts. It is an essential debugging tool for public account development.
Communication with msg-proxy-pass
msg-porxy-pass
is configured with a URL address prefix, wx-gateway
will add the message and event type to the end to form a complete URL. for example:msg-proxy-pass
is configured at http://wx.myhost.com/msghandler
wx-gateway
receives the user's text message, it will forward the message to http://wx.myhost.com/msghandler/msg/text
wx-gateway
receives a new user's attention event, it will forward the message to http://wx.myhost.com/msghandler/event/subscribe
msg
and event
are forwarding types, text
and subscribe
are specific message or event names. All names correspond to the messages corresponding to the WeChat public platform.POST
, and all request/response results are JSON
. The JSON of the request body is converted from the XML format of the WeChat official account API message. For specific fields, you can still refer to the WeChat official account API.msg
message names include text , image , voice , video , shortvideo , location , linkevent
event names include CLICK , VIEW , SCAN , subscribe , unsubscribe , location , LOCATION , pic_sysphoto , pic_photo_or_album , pic_weixin , scancode_waitmsg , scancode_push , MASSSENDJOBFINISH , TEMPLATESENDJOBFINISHmsg
message request/response example, name text
url: <msg-proxy-pass>/msg/text
Request message body:
{
"ToUserName" : "公众号的id " ,
"FromUserName" : "发送消息的用户的openId " ,
"CreateTime" : 1556088649 ,
"MsgType" : " text " ,
"Content" : "用户发送的文本内容" ,
"MsgId" : " 22277834746191186 " ,
"说明" : "以上为必传信息,如果配置文件中dont-append-userinfo为false,则有下面的信息" ,
"userInfo" : {
"openid" : " " ,
"nickname" : " " ,
"headimgurl" : " " ,
"city" : " " , "province" : " " , "country" : " " ,
"sex" : 1 ,
"language" : " " ,
"等等" : "各种信息"
},
"userInfoError" : "如果取userInfo发生错误,则有错误信息,否则该值为空"
}
response
If successful, it must be a 200 message reply, message body format
{
"type" : " text " ,
"msg" : "需要返回给用户的消息内容"
}
"type" can be "voice", "video", "image", etc., and "msg" is their corresponding "mediaId"
event
message request/response example, name subscribe
Request message body:
{
"ToUserName" : "公众号的id " ,
"FromUserName" : "关注公众号的用户的openId " ,
"CreateTime" : 1556088649 ,
"MsgType" : " event " ,
"Event" : " subscribe " ,
"EventKey" : " qrscene_场景ID(新关注) 或 场景ID(扫码) " ,
"Ticket" : "如果是二维码扫码,是二维码的票据" ,
"说明" : "以上为必传信息,如果配置文件中dont-append-userinfo为false,则有下面的信息" ,
"userInfo" : {
"openid" : " " ,
"nickname" : " " ,
"headimgurl" : " " ,
"city" : " " , "province" : " " , "country" : " " ,
"sex" : 1 ,
"language" : " " ,
"等等" : "各种信息"
},
"userInfoError" : "如果取userInfo发生错误,则有错误信息,否则该值为空"
}
response
same as message
If successful, it must be a 200 message reply, message body format
{
"type" : " text " ,
"msg" : "需要返回给用户的消息内容"
}
"type" can be "voice", "video", "image", etc., and "msg" is their corresponding "mediaId"
event
message request/response example, name channels_ec_order_pay
Request message body:
{
"ToUserName" : "视频号小店的id " ,
"FromUserName" : "在视频号小店购物的用户的openId " ,
"CreateTime" : 1556088649 ,
"MsgType" : " event " ,
"Event" : " channels_ec_order_pay " ,
"order_info" : {
"order_id" : 3705115058471208928 , // NOTE: 文档处是字符串,实际返回的整数
"pay_time" : 1658509200
}
}
response
Communication with redirect-url
Handle web page authorization requests. For the relevant configuration of the public account, please refer to the WeChat documentation. The callback URL only needs the domain name of wx-gateway
redirect-url
configures a URL, such as
redirect-url
configuration is http://wx.myhost.com/menu/path/to/redirect
wx-gateway
receives a web page request, it will forward the message to the above URL.POST
. The response result is completely determined by the forwarding processing service. Its HTTP response result will be reflected to the public account browser. In order for wx-gateway
to receive menu click events, the URL must be written in the format authorized by the official account web page:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=在这里填公众号的AppId&redirect_uri=http%3A//wx.myhost.com/这是redirect-path配置的值&response_type=code&scope=snsapi_base&state=这个值用于区分菜单项#wechat_redirect
wx-gateway
is configured correctly can it receive menu events, obtain the openId of the user who clicked the menu through code, and forward it to redirect-url
Request message format for requesting redirect-url
{
"requestURI" : "转发请求的URI,这是微信服务器访问gateway的URI,可以根据实际情况做进一步判断" ,
"appId" : "公众号的AppId,如果同时处理多个公众号,可以用来区分来源" ,
"openId" : "点击菜单的用户的openId " ,
"state" : "在菜单配置中的state的值,用于区分菜单项" ,
"userInfo" : {
"subscribe" : 1 ,
"openid" : " " ,
"nickname" : " " ,
"sex" : 1 ,
"language" : " " ,
"province" : " " ,
"city" : " " ,
"country" : " " ,
"headimgurl" : " " ,
"subscribe_time" : 1386160805 ,
},
"userInfoError" : "请求userInfo时的错误信息,如果为空表示没有错误"
}
All HTTP request headers and cookies will be forwarded to redirect-url
, which can be processed according to needs
Response result message format
redirect-url
, including