|
@@ -1,9 +1,13 @@
|
|
|
package weixin
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
"fmt"
|
|
|
+ "io/ioutil"
|
|
|
_ "log"
|
|
|
"math/rand"
|
|
|
+ "net"
|
|
|
+ "net/http"
|
|
|
|
|
|
"qfw/weixin/dao"
|
|
|
wf "qfw/weixinconfig"
|
|
@@ -12,6 +16,29 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+//图灵123机器人配置
|
|
|
+const (
|
|
|
+ APIKEY = "893b61852a7de8e3194194a0fe11917c"
|
|
|
+ APIURL = "http://www.tuling123.com/openapi/api"
|
|
|
+)
|
|
|
+
|
|
|
+var robotclient *http.Client
|
|
|
+
|
|
|
+//初始化图灵123 HttpClient加上超时
|
|
|
+func InitRobotHttpClient() {
|
|
|
+ robotclient = &http.Client{Transport: &http.Transport{
|
|
|
+ Dial: func(netw, addr string) (net.Conn, error) {
|
|
|
+ deadline := time.Now().Add(2 * time.Second)
|
|
|
+ c, err := net.DialTimeout(netw, addr, 2*time.Second)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ c.SetDeadline(deadline)
|
|
|
+ return c, nil
|
|
|
+ },
|
|
|
+ }}
|
|
|
+}
|
|
|
+
|
|
|
//文本消息处理
|
|
|
func MsgTxtHandler(w ResponseWriter, r *Request) {
|
|
|
openid := r.FromUserName
|
|
@@ -19,15 +46,12 @@ func MsgTxtHandler(w ResponseWriter, r *Request) {
|
|
|
//在会话中
|
|
|
if us, ok := UserSession[openid]; ok {
|
|
|
if r.Content == "q" || r.Content == "Q" {
|
|
|
- if us.Type == "robot" {
|
|
|
- w.ReplyText("再见了小主人,记得想我呦。")
|
|
|
+ if us.Type == "faq" {
|
|
|
+ w.ReplyText("您已经退出智能咨询服务。")
|
|
|
+ } else if us.Type == "wxadmin" {
|
|
|
+ w.ReplyText("您已经退出微信管理。")
|
|
|
} else {
|
|
|
- if us.Type == "wxadmin" {
|
|
|
- w.ReplyText("您已经退出微信管理。")
|
|
|
-
|
|
|
- } else {
|
|
|
- w.ReplyText("您已经退出企明星会员认证程序。")
|
|
|
- }
|
|
|
+ w.ReplyText("您已经退出企明星会员认证程序。")
|
|
|
}
|
|
|
delete(UserSession, r.FromUserName)
|
|
|
}
|
|
@@ -39,6 +63,8 @@ func MsgTxtHandler(w ResponseWriter, r *Request) {
|
|
|
processOIdentifyMsgTxt(us, w, r)
|
|
|
} else if us.Type == "wxadmin" {
|
|
|
processWxAdmin(openid, r.Content, w, r)
|
|
|
+ } else if us.Type == "faq" {
|
|
|
+ processFaq(openid, r.Content, w, r)
|
|
|
}
|
|
|
} else if r.Content == "微信管理" && mids[openid] {
|
|
|
UserSession[r.FromUserName] = NewUserSession("wxadmin")
|
|
@@ -154,3 +180,33 @@ func processPIdentifyMsgTxt(us *usersession, w ResponseWriter, r *Request) {
|
|
|
w.ReplyOK()
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+//处理咨询问题
|
|
|
+func processFaq(userid, content string, w ResponseWriter, r *Request) {
|
|
|
+ rurl := fmt.Sprintf("%s?key=%s&info=%s&userid=%s", APIURL, APIKEY, content, userid)
|
|
|
+ resp, err1 := robotclient.Get(rurl)
|
|
|
+ if err1 != nil {
|
|
|
+ w.ReplyText("对不起,我无法理解您所咨询的问题。")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+ bs, err2 := ioutil.ReadAll(resp.Body)
|
|
|
+ if err2 != nil {
|
|
|
+ w.ReplyText("对不起,我无法理解您所咨询的问题。")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var tmp map[string]interface{}
|
|
|
+ err3 := json.Unmarshal(bs, &tmp)
|
|
|
+ if err3 != nil {
|
|
|
+ w.ReplyText("对不起,我无法理解您所咨询的问题。")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ code, _ := tmp["code"].(float64)
|
|
|
+ if code == 100000 {
|
|
|
+ repl, _ := tmp["text"].(string)
|
|
|
+ w.ReplyText(repl)
|
|
|
+ } else {
|
|
|
+ w.ReplyText("对不起,我无法理解您所咨询的问题。")
|
|
|
+ return
|
|
|
+ }
|
|
|
+}
|