Bladeren bron

using *InWechatMsg to prevent double reply.

master
Patrick Peng Sun 8 jaren geleden
bovenliggende
commit
19969654f0
13 gewijzigde bestanden met toevoegingen van 40 en 40 verwijderingen
  1. +1
    -1
      chatSession.go
  2. +3
    -3
      eventSubscribe.go
  3. +1
    -1
      location.go
  4. +1
    -1
      main.go
  5. +1
    -1
      menuClick.go
  6. +3
    -3
      procDummy.go
  7. +5
    -5
      procEcho.go
  8. +6
    -6
      procGetBasicUserInfo.go
  9. +4
    -4
      procedure.go
  10. +6
    -6
      serveCommand.go
  11. +1
    -1
      serveEvents.go
  12. +1
    -1
      server.go
  13. +7
    -7
      sessionManager.go

+ 1
- 1
chatSession.go Bestand weergeven

@@ -151,7 +151,7 @@ func (ss *openIDSessionData) getVal(key string) string {

//main entry point for processing each incoming message
//this stage has session date available
func (ss *openIDSessionData) incomingMsg(v InWechatMsg) {
func (ss *openIDSessionData) incomingMsg(v *InWechatMsg) {
openID := v.header.FromUserName
//kfSendTxtAs(openID, "信息收到"+v.header.MsgType, "孙鹏")
//are we in an existing procedure

+ 3
- 3
eventSubscribe.go Bestand weergeven

@@ -9,7 +9,7 @@ import (
"time"
)

func onSubscribe(in InWechatMsg) {
func onSubscribe(in* InWechatMsg) {
openID := in.header.FromUserName
//check whether we have his own record in the CRM system
info, found, err := crmFindLeadByOpenID(openID)
@@ -88,7 +88,7 @@ func (in *InWechatMsg) askUserFillupBasicInfo() {
templateSendJoinCommunity(in.header.FromUserName, url, first, remark, communityName, joinDate)
}

func sendGreeting4ExistingUser(in InWechatMsg, info crmdLead) {
func sendGreeting4ExistingUser(in* InWechatMsg, info crmdLead) {

first := "欢迎" + info.Name + "返回澳洲校友会"
//url := "http://wechat.hitxy.org.au/profile_newly_register"
@@ -136,7 +136,7 @@ func (m *WechatUserInfo) uploadAvatar() (avatarID string, err error) {
}

//when user left
func onUnSubscribe(in InWechatMsg) {
func onUnSubscribe(in* InWechatMsg) {
info, found, err := crmFindLeadByOpenID(in.header.FromUserName)

if err != nil {

+ 1
- 1
location.go Bestand weergeven

@@ -15,7 +15,7 @@ type crmdLocation struct {
}

//record location
func onLocation(in InWechatMsg) {
func onLocation(in* InWechatMsg) {
openID := in.header.FromUserName

lead, found, err := crmFindLeadByOpenID(openID)

+ 1
- 1
main.go Bestand weergeven

@@ -69,7 +69,7 @@ func setupHTTPHandler() {

func startSessionManager(concurrent int) {
m := SessionManager{}
AllInMessage = make(chan InWechatMsg, concurrent)
AllInMessage = make(chan *InWechatMsg, concurrent)
go m.startSessionManager(AllInMessage)
}


+ 1
- 1
menuClick.go Bestand weergeven

@@ -1,6 +1,6 @@
package main

func onClick(in InWechatMsg) {
func onClick(in* InWechatMsg) {
e := in.body.(EventMsg)
openid := in.header.FromUserName
switch e.EventKey {

+ 3
- 3
procDummy.go Bestand weergeven

@@ -15,17 +15,17 @@ func (m *procDummyDef) clean(ss *openIDSessionData) {
ss.Procedure = ""
}

func (m *procDummyDef) start(ss *openIDSessionData, in InWechatMsg) {
func (m *procDummyDef) start(ss *openIDSessionData, in* InWechatMsg) {

}

func (m *procDummyDef) serve(ss *openIDSessionData, in InWechatMsg) {
func (m *procDummyDef) serve(ss *openIDSessionData, in* InWechatMsg) {

}
func (m *procDummyDef) summary(ss *openIDSessionData) {

}

func (m *procDummyDef) intro(ss *openIDSessionData, in InWechatMsg) {
func (m *procDummyDef) intro(ss *openIDSessionData, in* InWechatMsg) {

}

+ 5
- 5
procEcho.go Bestand weergeven

@@ -23,11 +23,11 @@ func (m *procEchoDef) clean(ss *openIDSessionData) {
//log.Println(*ss)
}

func (m *procEchoDef) start(ss *openIDSessionData, in InWechatMsg) {
func (m *procEchoDef) start(ss *openIDSessionData, in* InWechatMsg) {
ss.setKvPair("started at", time.Now().Format("2006/03:04:05"))
}

func (m *procEchoDef) serve(ss *openIDSessionData, in InWechatMsg) {
func (m *procEchoDef) serve(ss *openIDSessionData, in* InWechatMsg) {
stopEcho := false
if in.header.MsgType == "text" {
if in.body.(TextMsg).Content == "结束Echo" || in.body.(TextMsg).Content == "结束echo" {
@@ -62,12 +62,12 @@ func (m *procEchoDef) summary(ss *openIDSessionData) {
kfSendTxt(ss.OpenID, msg)
}

func (m *procEchoDef) intro(ss *openIDSessionData, in InWechatMsg) {
func (m *procEchoDef) intro(ss *openIDSessionData, in* InWechatMsg) {
in.replyText("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您")
kfSendTxt(ss.OpenID, "10分钟静默之后 Echo将自动结束,\n输入 [结束Echo],或者语音 ‘退出退出', 清除Echo模式")
}

func (m *procEchoDef) doCommand(ss *openIDSessionData, in InWechatMsg) {
func (m *procEchoDef) doCommand(ss *openIDSessionData, in* InWechatMsg) {
openID := in.header.FromUserName
str, err := BuildTextMsg(openID, "default")
log.Println("echoCommand :" + in.header.MsgType)
@@ -124,7 +124,7 @@ func (m *procEchoDef) doCommand(ss *openIDSessionData, in InWechatMsg) {
return
}

func (m *procEchoDef) doEvents(ss *openIDSessionData, in InWechatMsg) {
func (m *procEchoDef) doEvents(ss *openIDSessionData, in* InWechatMsg) {
openID := in.header.FromUserName
str := ""
e := in.body.(EventMsg)

+ 6
- 6
procGetBasicUserInfo.go Bestand weergeven

@@ -14,12 +14,12 @@ func (m *getBasicUserInfoDef) init(ss *openIDSessionData) {
func (m *getBasicUserInfoDef) clean(ss *openIDSessionData) {
ss.Procedure = ""
}
func (m *getBasicUserInfoDef) start(ss *openIDSessionData, in InWechatMsg) {
procGetBasicUserInfo.summary(ss)
in.replyText("get user info")
func (m *getBasicUserInfoDef) start(ss *openIDSessionData, in *InWechatMsg) {
//procGetBasicUserInfo.summary(ss)
in.replyText("panic")
}

func (m *getBasicUserInfoDef) serve(ss *openIDSessionData, in InWechatMsg) {
func (m *getBasicUserInfoDef) serve(ss *openIDSessionData, in *InWechatMsg) {
in.replyText("servve get user info")
procGetBasicUserInfo.clean(ss)

@@ -28,6 +28,6 @@ func (m *getBasicUserInfoDef) summary(ss *openIDSessionData) {

}

func (m *getBasicUserInfoDef) intro(ss *openIDSessionData, in InWechatMsg) {
func (m *getBasicUserInfoDef) intro(ss *openIDSessionData, in *InWechatMsg) {
in.replyText("111")
}

+ 4
- 4
procedure.go Bestand weergeven

@@ -24,10 +24,10 @@ type chatProcedure interface {
init(*openIDSessionData) //house keeping
clean(*openIDSessionData) //house keeping

start(*openIDSessionData, InWechatMsg) //for first message
serve(*openIDSessionData, InWechatMsg) //for all subsequent message
summary(*openIDSessionData) //after all message has been done
intro(*openIDSessionData, InWechatMsg) //initial text/video/voice introduction
start(*openIDSessionData, *InWechatMsg) //for first message
serve(*openIDSessionData, *InWechatMsg) //for all subsequent message
summary(*openIDSessionData) //after all message has been done
intro(*openIDSessionData, *InWechatMsg) //initial text/video/voice introduction
}

//AllProc all procedure that we implemented

+ 6
- 6
serveCommand.go Bestand weergeven

@@ -7,7 +7,7 @@ import (
"time"
)

type commandFunction func(*openIDSessionData, InWechatMsg) bool
type commandFunction func(*openIDSessionData, *InWechatMsg) bool

var commandMap = map[string]commandFunction{
"版本信息": cmdVersion,
@@ -16,7 +16,7 @@ var commandMap = map[string]commandFunction{
"回音": cmdEcho,
}

func (ss *openIDSessionData) serveCommand(in InWechatMsg) (processed bool) {
func (ss *openIDSessionData) serveCommand(in *InWechatMsg) (processed bool) {
switch in.header.MsgType {
case "text":
processed = ss.serveTextCommand(in)
@@ -38,7 +38,7 @@ func (ss *openIDSessionData) serveCommand(in InWechatMsg) (processed bool) {

}

func (ss *openIDSessionData) serveTextCommand(in InWechatMsg) (processed bool) {
func (ss *openIDSessionData) serveTextCommand(in *InWechatMsg) (processed bool) {
cmd := ""
if in.header.MsgType == "text" {
cmd = in.body.(TextMsg).Content
@@ -66,7 +66,7 @@ func (ss *openIDSessionData) serveTextCommand(in InWechatMsg) (processed bool) {
return false //不认识的命令,我们选择这个信息不处理
}

func allCommand(ss *openIDSessionData, in InWechatMsg) (processed bool) {
func allCommand(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
processed = true
msg := "命令如下:\n"
count := 0
@@ -84,13 +84,13 @@ func allCommand(ss *openIDSessionData, in InWechatMsg) (processed bool) {
return
}

func cmdVersion(ss *openIDSessionData, in InWechatMsg) (processed bool) {
func cmdVersion(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
processed = true
in.replyText("这是测试版本" + time.Now().Format("2006/01/02 03:04:05"))
return
}

func cmdEcho(ss *openIDSessionData, in InWechatMsg) (processed bool) {
func cmdEcho(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
processed = true
procEcho.init(ss)
procEcho.intro(ss, in)

+ 1
- 1
serveEvents.go Bestand weergeven

@@ -1,6 +1,6 @@
package main

func (ss *openIDSessionData) serveEvents(in InWechatMsg) (processed bool) {
func (ss *openIDSessionData) serveEvents(in* InWechatMsg) (processed bool) {
processed = true
e := in.body.(EventMsg)
switch e.Event {

+ 1
- 1
server.go Bestand weergeven

@@ -67,7 +67,7 @@ func answerWechatPost(w http.ResponseWriter, r *http.Request) {
log.Println("Error: Invalid Input ")
} else {
//put into user session based pipeline
AllInMessage <- in
AllInMessage <- &in
//read instant response
reply = <-in.instantResponse
}

+ 7
- 7
sessionManager.go Bestand weergeven

@@ -9,7 +9,7 @@ import (
type openIDSession struct {
openID string //who is this?
count int // number of message in the Queue, channel
jobs chan InWechatMsg
jobs chan *InWechatMsg
data openIDSessionData //session data, that needs to be saved to disk
}

@@ -29,13 +29,13 @@ type SessionManager struct {
// we collect them and redistribute it according to
// openID, all messages for a single openID will be
// sequentially processed
var AllInMessage chan InWechatMsg
var AllInMessage chan *InWechatMsg

//a stand alone routine that manage all
// live sessions
// take all message and fanout to different openid handlers
// for each openid, its message are manipulated in serial manner.
func (m *SessionManager) startSessionManager(in <-chan InWechatMsg) {
func (m *SessionManager) startSessionManager(in <-chan *InWechatMsg) {
log.Println("session manager start..")
m.sessions = map[string]openIDSession{}
m.done = make(chan workDone, 2000) //2000 different openID simultaneously.
@@ -51,14 +51,14 @@ func (m *SessionManager) startSessionManager(in <-chan InWechatMsg) {
}
}

func (m *SessionManager) processIncomingMsg(v InWechatMsg) {
func (m *SessionManager) processIncomingMsg(v *InWechatMsg) {
cnt := m.addJob(v)
if cnt == 1 { //there is no worker thread working
go m.startJob(v.header.FromUserName)
}
}

func (m *SessionManager) addJob(v InWechatMsg) (jobCount int) {
func (m *SessionManager) addJob(v *InWechatMsg) (jobCount int) {
openID := v.header.FromUserName
s, found := m.sessions[openID]
if !found { //create one
@@ -80,7 +80,7 @@ func (m *SessionManager) createSession(openID string) openIDSession {
s := openIDSession{}
s.openID = openID
s.count = 0
s.jobs = make(chan InWechatMsg, 50)
s.jobs = make(chan *InWechatMsg, 50)
s.data = openIDSessionData{}
s.data.Load(openID) //either load or create new
m.sessions[openID] = s //register it to memory
@@ -146,7 +146,7 @@ func (m *SessionManager) startJob(openID string) {
return
}

func (m *SessionManager) checkOpenID(openID string, v InWechatMsg) {
func (m *SessionManager) checkOpenID(openID string, v *InWechatMsg) {
if v.header.FromUserName != openID {
log.Println("Error:[SessionMgr/checkOpenID] Weird Message below ...")
log.Println(v)

Laden…
Annuleren
Opslaan