diff --git a/serveCommand.go b/serveCommand.go index eece5c8..7309664 100644 --- a/serveCommand.go +++ b/serveCommand.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" "time" ) @@ -16,22 +17,43 @@ var commandMap = map[string]commandFunction{ } func (ss *openIDSessionData) serveCommand(in InWechatMsg) (processed bool) { - if in.header.MsgType == "text" { - return ss.serveTextCommand(in) + switch in.header.MsgType { + case "text": + processed = ss.serveTextCommand(in) + case "image": + in.replyText("图片收到") + processed = true + case "voice": + processed = ss.serveTextCommand(in) + case "video": + case "shortvideo": + case "location": + case "link": + case "event": + processed = ss.serveEvents(in) + default: + processed = false } - processed = false return + } func (ss *openIDSessionData) serveTextCommand(in InWechatMsg) (processed bool) { - cmd := in.body.(TextMsg).Content - if f, hasFunction := commandMap[cmd]; hasFunction { - return f(ss, in) + cmd := "" + if in.header.MsgType == "text" { + cmd = in.body.(TextMsg).Content + } else if in.header.MsgType == "voice" { + cmd = voice2Cmd(in.body.(VoiceMsg)) } - if cmd == "所有命令" || cmd == "all command" { - return allCommand(ss, in) + if cmd != "" { + if f, hasFunction := commandMap[cmd]; hasFunction { + return f(ss, in) + } + if cmd == "所有命令" || cmd == "all command" { + return allCommand(ss, in) + } } - return false + return false //不认识的命令,我们选择这个信息不处理 } func allCommand(ss *openIDSessionData, in InWechatMsg) (processed bool) { @@ -44,6 +66,7 @@ func allCommand(ss *openIDSessionData, in InWechatMsg) (processed bool) { } in.replyText(msg) + in.replyText("Error") return } @@ -61,3 +84,9 @@ func cmdEcho(ss *openIDSessionData, in InWechatMsg) (processed bool) { procEcho.start(ss, in) return } + +func voice2Cmd(v VoiceMsg) (cmd string) { + cmd = v.Recognition + cmd = strings.TrimSuffix(cmd, "。") //去掉末尾的标点符号,句号 + return +}