未验证 提交 cbf29c7b 编写于 作者: LinuxSuRen's avatar LinuxSuRen 提交者: GitHub

Merge pull request #3 from LinuxSuRen/unknown-reply

Support for unknown keywords
TAG=dev-$(shell cat .version)-$(shell git config --get user.email | sed -e "s/@/-/")
build:
CGO_ENABLED=0 GOOS=linux go build -ldflags "-w -s" -a -installsuffix cgo -o bin/wechat-backend
upx bin/wechat-backend
......@@ -7,13 +9,13 @@ build-local:
upx bin/wechat-backend
image: build
docker build -t surenpi/jenkins-wechat .
docker build -t surenpi/jenkins-wechat:${TAG} .
image-alauda: build
docker build -t index.alauda.cn/alaudak8s/jenkins-wechat .
push-image: image
docker push surenpi/jenkins-wechat
docker push surenpi/jenkins-wechat:${TAG}
push-image-alauda: image-alauda
docker push index.alauda.cn/alaudak8s/jenkins-wechat
......@@ -27,7 +29,7 @@ init-mock-dep:
go install github.com/golang/mock/mockgen
update:
kubectl set image deploy/wechat wechat=surenpi/jenkins-wechat
kubectl set image deploy/wechat wechat=surenpi/jenkins-wechat:${TAG}
make restart
update-alauda:
......
......@@ -76,6 +76,8 @@ func (we *WeChat) wechatRequest(writer http.ResponseWriter, r *http.Request) {
if textRequestBody != nil {
autoReplyInitChains := reply.AutoReplyChains()
fmt.Printf("found [%d] autoReply", len(autoReplyInitChains))
var potentialReplys []reply.AutoReply
for _, autoReplyInit := range autoReplyInitChains {
if autoReplyInit == nil {
fmt.Printf("found a nil autoReply.")
......@@ -86,6 +88,13 @@ func (we *WeChat) wechatRequest(writer http.ResponseWriter, r *http.Request) {
continue
}
potentialReplys = append(potentialReplys, autoReply)
}
sort.Sort(reply.ByWeight(potentialReplys))
if len(potentialReplys) > 0 {
autoReply := potentialReplys[0]
fmt.Printf("going to handle by %s\n", autoReply.Name())
if data, err := autoReply.Handle(); err != nil {
......@@ -95,8 +104,9 @@ func (we *WeChat) wechatRequest(writer http.ResponseWriter, r *http.Request) {
} else {
fmt.Printf("response:%s\n", data)
fmt.Fprintf(writer, data)
break
}
} else {
fmt.Println("should have at least one reply")
}
}
}
......
......@@ -7,10 +7,12 @@ import (
core "github.com/linuxsuren/wechat-backend/pkg"
)
// AutoReply represent auto reply interface
type AutoReply interface {
Accept(request *core.TextRequestBody) bool
Handle() (string, error)
Name() string
Weight() int
}
type Init func() AutoReply
......
......@@ -26,6 +26,10 @@ func (m *MatchAutoReply) Name() string {
return "SimpleMatchReply"
}
func (m *MatchAutoReply) Weight() int {
return 0
}
// Accept consider if it will accept the request
func (m *MatchAutoReply) Accept(request *core.TextRequestBody) (ok bool) {
m.Request = request
......
......@@ -12,6 +12,20 @@ func AutoReplyChains() []Init {
return autoReplyInitChains
}
type ByWeight []AutoReply
func (b ByWeight) Len() int {
return len(b)
}
func (b ByWeight) Less(i, j int) bool {
return b[i].Weight() < b[j].Weight()
}
func (b ByWeight) Swap(i, j int) {
b[i], b[j] = b[j], b[i]
}
// func init() {
// autoReplyInitChains = make([]Init, 3)
// }
......@@ -22,6 +22,10 @@ func (m *SearchAutoReply) Name() string {
return "SearchAutoReply"
}
func (m *SearchAutoReply) Weight() int {
return 0
}
// Accept consider if it will accept the request
func (m *SearchAutoReply) Accept(request *core.TextRequestBody) (ok bool) {
m.Request = request
......
package reply
import (
"math"
core "github.com/linuxsuren/wechat-backend/pkg"
)
// UnknownAutoReply unknown auto reply
type UnknownAutoReply struct {
Request *core.TextRequestBody
}
// Name represent name for current auto reply
func (u *UnknownAutoReply) Name() string {
return "UnknownReply"
}
// Accept all keywords
func (u *UnknownAutoReply) Accept(request *core.TextRequestBody) bool {
u.Request = request
return true
}
// Handle take care of unknown things
func (u *UnknownAutoReply) Handle() (string, error) {
from := u.Request.ToUserName
to := u.Request.FromUserName
data, err := makeTextResponseBody(from, to, `
我貌似没有明白你的问题,请回复“聊天室”问问其他人吧。
`)
return string(data), err
}
// Weight should be the last one
func (u *UnknownAutoReply) Weight() int {
return math.MaxInt64
}
var _ AutoReply = &UnknownAutoReply{}
func init() {
Register(func() AutoReply {
return &UnknownAutoReply{}
})
}
package reply
import (
"testing"
"github.com/golang/mock/gomock"
core "github.com/linuxsuren/wechat-backend/pkg"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestUnknown(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Unknown keywords")
}
var _ = Describe("Unknon keywords", func() {
var (
reply AutoReply
ctrl *gomock.Controller
)
BeforeEach(func() {
ctrl = gomock.NewController(GinkgoT())
reply = &UnknownAutoReply{}
})
It("should not error", func() {
Expect(reply.Accept(&core.TextRequestBody{})).To(Equal(true))
})
AfterEach(func() {
ctrl.Finish()
})
})
......@@ -29,6 +29,10 @@ func (m *WelcomeReply) Accept(request *core.TextRequestBody) (ok bool) {
return
}
func (m *WelcomeReply) Weight() int {
return 0
}
func init() {
fmt.Println("register for welcome")
Register(func() AutoReply {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册