- 当前 Bug 的表现(可附上截图)
app_id: wxbec7aebf80022eb2
- 预期表现
服务器接收用户在客服里发送的各类消息
- 复现路径
该app_id对应的服务器URL配置
POST /vx-pub/bridge-in/
昨天调试的时候,是正常的,可以接受消息。
今天来试,就所有消息都接受不到了,已测:
服务器配置URL/vx-pub/bridge-in/ 接口完好
公众号已认证
消息推送已启用
公众号后台,可接收用户消息
服务器日志结果:
服务器仅在昨日调试时,接入成功了 GET URL/vx-pub/bridge-in/, 并且能通过POST URL/vx-pub/bridge-in/ 收到用户消息,设置好transfer_customer_service后,服务器和公众号后台都可以收到消息。正常
今日再来用时,服务端代码没变,收不到任何消息,偶尔收到几条事件,比如:
```
{“ToUserName”:“gh_043fa918d149”,“FromUserName”:“oZKx35IzemCgIIkQr2Bq7sRz0iho”,“CreateTime”:1560909204,“MsgType”:“event”,“Event”:“kf_close_session”,“KfAccount”:“kf2001@gh_043fa918d149”,“CloseType”:“TIMEOUT”}
[GIN] 2019/06/19 - 09:53:24 | 200 | 156.653µs | 223.166.222.112 | POST /vx-pub/bridge-in/?signature=72aa026743513ff410f4a496afab0c24cef827dc×tamp=1560909204&nonce=1022030466
```
**但是,公众号后台的消息还是能收到的**
- 提供一个最简复现 Demo
```go
package main
import (
“crypto/sha1”
“encoding/hex”
“encoding/json”
“fmt”
“github.com/fwhezfwhez/errorx”
“io/ioutil”
“log”
“net/http”
“shangraomajiang/util/dependent-services/vx-pub/config”
“sort”
“strings”
“time”
)
func main() {
r := gin.Default()
r.GET("/vx-pub/bridge-in/", BridgeIn)
r.POST("/vx-pub/bridge-in/", service)
s := &http.Server{
// dev-8301 pro-8300
Addr: config.Port,
Handler: cors.AllowAll().Handler®,
ReadTimeout: 60 * time.Second,
WriteTimeout: 60 * time.Second,
MaxHeaderBytes: 1 << 21,
}
s.ListenAndServe()
}
func BridgeIn(c *gin.Context) {
signature := c.DefaultQuery(“signature”, “”)
timestamp := c.DefaultQuery(“timestamp”, “”)
nonce := c.DefaultQuery(“nonce”, “”)
echostr := c.DefaultQuery(“echostr”, “”)
log.Println(fmt.Sprintf(“receive: signature=%s, timestamp=%s, nonce=%s, echostr=%s”, signature, timestamp, nonce, echostr))
log.Println(fmt.Sprintf(“token=%s”, config.Cfg.GetString(“token”)))
arr := []string{timestamp, nonce, config.Cfg.GetString(“token”)}
sort.Strings(arr)
sha1Hash := Sha1(strings.Join(arr, “”))
log.Println(“sha1Hash=”, sha1Hash)
if sha1Hash != signature {
log.Println(“invalid source sha1Hash != signature”)
}
c.String(200, echostr)
}
func service(c *gin.Context) {
type Message struct {
// 公有的
ToUserName string
FromUserName string
CreateTime int64
MsgType string // text,image,miniprogrampage, event
MsgId int64
// 文本消息,text
Content string
// 图片消息, image
PicUrl string
MediaId string
// 小卡片消息,miniprogrampage
Title string
AppId string
PagePath string
ThumbUrl string
ThumbMediaId string
// event
// do nothing
}
type Result struct {
ToUserName string
FromUserName string
CreateTime int64
MsgType string
}
var message Message
defer func() {
var in = func(str string, arr []string) bool {
for _, v := range arr {
if v == str {
return true
}
}
return false
}
if in(message.MsgType, []string{“text”, “image”, “miniprogrampage”}) {
c.JSON(200, Result{
ToUserName: message.FromUserName,
FromUserName: message.ToUserName,
CreateTime: time.Now().Unix(),
MsgType: “transfer_customer_service”,
})
} else {
c.String(200, “success”)
}
}()
buf, e := ioutil.ReadAll(c.Request.Body)
if e != nil {
log.Println(errorx.Wrap(e).Error())
return
}
if e := json.Unmarshal(buf, &message); e != nil {
log.Println(errorx.Wrap(e).Error())
return
}
fmt.Println(string(buf))
}
func Sha1(data string) string {
sha := sha1.New()
sha.Write([]byte(data))
return hex.EncodeToString(sha.Sum([]byte(nil)))
}
```