ソースを参照

改faq回应模式

zhanghongbo 9 年 前
コミット
f4208f2728
1 ファイル変更29 行追加8 行削除
  1. 29 8
      weixin/src/qfw/weixin/msgtxtchandler.go

+ 29 - 8
weixin/src/qfw/weixin/msgtxtchandler.go

@@ -23,6 +23,7 @@ const (
 )
 
 var robotclient *http.Client
+var robotlock chan bool = make(chan bool, 100) //100个并发
 
 //初始化图灵123 HttpClient加上超时
 func InitRobotHttpClient() {
@@ -64,7 +65,9 @@ func MsgTxtHandler(w ResponseWriter, r *Request) {
 		} else if us.Type == "wxadmin" {
 			processWxAdmin(openid, r.Content, w, r)
 		} else if us.Type == "faq" {
-			processFaq(openid, r.Content, w, r)
+			robotlock <- true
+			w.ReplyOK() //直接回应
+			go processFaq(openid, r.Content)
 		}
 	} else if r.Content == "微信管理" && mids[openid] {
 		UserSession[r.FromUserName] = NewUserSession("wxadmin")
@@ -181,32 +184,50 @@ func processPIdentifyMsgTxt(us *usersession, w ResponseWriter, r *Request) {
 	}
 }
 
-//处理咨询问题
-func processFaq(userid, content string, w ResponseWriter, r *Request) {
+//处理咨询问题,发的异步客服消息
+func processFaq(userid, content string) {
+	defer func() { <-robotlock }()
 	rurl := fmt.Sprintf("%s?key=%s&info=%s&userid=%s", APIURL, APIKEY, content, userid)
 	resp, err1 := robotclient.Get(rurl)
 	if err1 != nil {
-		w.ReplyText("对不起,我无法理解您所咨询的问题。")
+		CustomReply(userid, "对不起,我无法理解您所咨询的问题。")
 		return
 	}
 	defer resp.Body.Close()
 	bs, err2 := ioutil.ReadAll(resp.Body)
 	if err2 != nil {
-		w.ReplyText("对不起,我无法理解您所咨询的问题。")
+		CustomReply(userid, "对不起,我无法理解您所咨询的问题。")
 		return
 	}
 	var tmp map[string]interface{}
 	err3 := json.Unmarshal(bs, &tmp)
 	if err3 != nil {
-		w.ReplyText("对不起,我无法理解您所咨询的问题。")
+		CustomReply(userid, "对不起,我无法理解您所咨询的问题。")
 		return
 	}
 	code, _ := tmp["code"].(float64)
 	if code == 100000 {
 		repl, _ := tmp["text"].(string)
-		w.ReplyText(repl)
+		CustomReply(userid, repl)
 	} else {
-		w.ReplyText("对不起,我无法理解您所咨询的问题。")
+		CustomReply(userid, "对不起,我无法理解您所咨询的问题。")
 		return
 	}
 }
+
+//通过客服接口回复
+func CustomReply(openid, content string) {
+	//发送客服消息
+	var msg struct {
+		ToUser  string `json:"touser"`
+		MsgType string `json:"msgtype"`
+		Text    struct {
+			Content string `json:"content"`
+		} `json:"text"`
+	}
+	msg.ToUser = openid
+	msg.MsgType = "text"
+	msg.Text.Content = content
+	Mux.PostCustomMsg("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=", msg)
+
+}