浏览代码

添加智能机器人

zhanghongbo 9 年之前
父节点
当前提交
923e60155e
共有 3 个文件被更改,包括 72 次插入10 次删除
  1. 3 0
      weixin/src/qfw/weixin/clickhandler.go
  2. 5 2
      weixin/src/qfw/weixin/menu.go
  3. 64 8
      weixin/src/qfw/weixin/msgtxtchandler.go

+ 3 - 0
weixin/src/qfw/weixin/clickhandler.go

@@ -89,6 +89,9 @@ func ClickHandler(w ResponseWriter, r *Request) {
 					UserSession[r.FromUserName] = NewUserSession("oidentify")
 				}
 			}
+		case "faq": //faq,进入机器人会话
+			UserSession[r.FromUserName] = NewUserSession("faq")
+			w.ReplyText("欢迎使用企明星智能咨询服务,请输入您要咨询的问题,输入q或Q退出咨询。")
 		case "sinfo":
 			{ //用户请求进入服务消息
 

+ 5 - 2
weixin/src/qfw/weixin/menu.go

@@ -36,7 +36,7 @@ func CreateMenu(rw http.ResponseWriter, r *http.Request) {
 		menu.Buttons[1].SubButtons[2].Key = "oidentification"
 	*/
 	menu.Buttons[2].Name = "会员服务"
-	menu.Buttons[2].SubButtons = make([]MenuButton, 4)
+	menu.Buttons[2].SubButtons = make([]MenuButton, 5)
 	menu.Buttons[2].SubButtons[0].Name = "微官网"
 	menu.Buttons[2].SubButtons[0].Type = MenuButtonTypeKey
 	menu.Buttons[2].SubButtons[0].Key = "msite" //"http://" + wf.SysConfig.Domain + "/weixin/site/edit" //微官网编辑
@@ -53,7 +53,10 @@ func CreateMenu(rw http.ResponseWriter, r *http.Request) {
 	menu.Buttons[2].SubButtons[3].Name = "我的积分/签到"
 	menu.Buttons[2].SubButtons[3].Type = MenuButtonTypeUrl
 	menu.Buttons[2].SubButtons[3].Url = fmt.Sprintf(urlstr, "signature")
-
+	//
+	menu.Buttons[2].SubButtons[4].Name = "常见问题解答"
+	menu.Buttons[2].SubButtons[4].Type = MenuButtonTypeKey ///search/enterprise/m_ent.html
+	menu.Buttons[2].SubButtons[4].Key = "faq"
 	//menu.Buttons[2].SubButtons[3].Name = "领取红包"
 	//menu.Buttons[2].SubButtons[3].Type = MenuButtonTypeUrl
 	//menu.Buttons[2].SubButtons[3].Url = "http://mp.weixin.qq.com/s?__biz=MzA5MTk0MTk5Ng==&mid=401971726&idx=1&sn=89d2470f1de7e80975223041b06c106a#rd"

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

@@ -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
+	}
+}