Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement extension.NewExecuteCommandFuncResponse and Option methods #28

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 7 additions & 12 deletions examples/00_test/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ package main
import (
"log"

"github.com/jaimeteb/chatto/extension"
"github.com/jaimeteb/chatto/query"
ext "github.com/jaimeteb/chatto/extension"
)

func greetFunc(req *extension.ExecuteCommandFuncRequest) (res *extension.ExecuteCommandFuncResponse) {
return &extension.ExecuteCommandFuncResponse{
FSM: req.FSM,
Answers: []query.Answer{{
Text: "Hello Universe",
Image: "https://i.imgur.com/pPdjh6x.jpg",
}},
}
func greetFunc(req *ext.ExecuteCommandFuncRequest) (res *ext.ExecuteCommandFuncResponse) {
return req.NewExecuteCommandFuncResponse(
ext.WithTextAnswer("Hello Universe"),
)
}

var registeredCommandFuncs = extension.RegisteredCommandFuncs{
var registeredCommandFuncs = ext.RegisteredCommandFuncs{
"any": greetFunc,
}

func main() {
if err := extension.ServeREST(registeredCommandFuncs); err != nil {
if err := ext.ServeREST(registeredCommandFuncs); err != nil {
log.Fatalln(err)
}
}
61 changes: 24 additions & 37 deletions examples/04_trivia/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,47 @@ import (
"log"

"github.com/jaimeteb/chatto/extension"
"github.com/jaimeteb/chatto/fsm"
"github.com/jaimeteb/chatto/query"
)

func validateAnswer1(req *extension.ExecuteCommandFuncRequest) (res *extension.ExecuteCommandFuncResponse) {
ans := req.FSM.Slots["answer_1"]
fsmDomain := req.Domain

if !(ans == "1" || ans == "2" || ans == "3") {
return &extension.ExecuteCommandFuncResponse{
FSM: &fsm.FSM{
State: fsmDomain.StateTable["question_1"],
Slots: req.FSM.Slots,
},
Answers: []query.Answer{{Text: "Select one of the options"}},
}
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer("Select one of the options"),
extension.WithState(fsmDomain.StateTable["question_1"]),
)
}

return &extension.ExecuteCommandFuncResponse{
FSM: req.FSM,
Answers: []query.Answer{{Text: "Question 2:\n" +
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer("Question 2:\n" +
"What is the capital of the state of Utah?\n" +
"1. Salt Lake City\n" +
"2. Jefferson City\n" +
"3. Cheyenne"}},
}
"3. Cheyenne"),
)

}

func validateAnswer2(req *extension.ExecuteCommandFuncRequest) (res *extension.ExecuteCommandFuncResponse) {
ans := req.FSM.Slots["answer_2"]
fsmDomain := req.Domain

if !(ans == "1" || ans == "2" || ans == "3") {
return &extension.ExecuteCommandFuncResponse{
FSM: &fsm.FSM{
State: fsmDomain.StateTable["question_2"],
Slots: req.FSM.Slots,
},
Answers: []query.Answer{{Text: "Select one of the options"}},
}
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer("Select one of the options"),
extension.WithState(fsmDomain.StateTable["question_2"]),
)
}

return &extension.ExecuteCommandFuncResponse{
FSM: req.FSM,
Answers: []query.Answer{{Text: "Question 3:\n" +
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer("Question 3:\n" +
"Who painted Starry Night?\n" +
"1. Pablo Picasso\n" +
"2. Claude Monet\n" +
"3. Vincent Van Gogh"}},
}
"3. Vincent Van Gogh"),
)
}

func calculateScore(req *extension.ExecuteCommandFuncRequest) (res *extension.ExecuteCommandFuncResponse) {
Expand All @@ -62,13 +53,10 @@ func calculateScore(req *extension.ExecuteCommandFuncRequest) (res *extension.Ex
slt := req.FSM.Slots

if !(ans == "1" || ans == "2" || ans == "3") {
return &extension.ExecuteCommandFuncResponse{
FSM: &fsm.FSM{
State: fsmDomain.StateTable["question_3"],
Slots: req.FSM.Slots,
},
Answers: []query.Answer{{Text: "Select one of the options"}},
}
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer("Select one of the options"),
extension.WithState(fsmDomain.StateTable["question_3"]),
)
}

answer1 := slt["answer_1"]
Expand Down Expand Up @@ -98,10 +86,9 @@ func calculateScore(req *extension.ExecuteCommandFuncRequest) (res *extension.Ex
message = "You got 3/3 answers right.\nYou are good! Congrats!"
}

return &extension.ExecuteCommandFuncResponse{
FSM: req.FSM,
Answers: []query.Answer{{Text: message}},
}
return req.NewExecuteCommandFuncResponse(
extension.WithTextAnswer(message),
)
}

var registeredCommandFuncs = extension.RegisteredCommandFuncs{
Expand Down
44 changes: 44 additions & 0 deletions extension/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,47 @@ func (l *ListenerREST) GetBuildVersion(w http.ResponseWriter, r *http.Request) {
return
}
}

// ExecuteCommandFuncResponseOption functions represent an option to build a new ExecuteCommandFuncResponse
type ExecuteCommandFuncResponseOption func(*ExecuteCommandFuncResponse)

// NewExecuteCommandFuncResponse creates a new ExecuteCommandFuncResponse based on the data from
// the ExecuteCommandFuncRequest that was sent to the extension command function
func (r *ExecuteCommandFuncRequest) NewExecuteCommandFuncResponse(opts ...ExecuteCommandFuncResponseOption) *ExecuteCommandFuncResponse {
response := ExecuteCommandFuncResponse{
FSM: r.FSM,
Answers: make([]query.Answer, 0),
}
for _, o := range opts {
o(&response)
}
return &response
}

// WithAnswer appends a text and image answer to the response
func WithAnswer(text, image string) ExecuteCommandFuncResponseOption {
return func(r *ExecuteCommandFuncResponse) {
r.Answers = append(r.Answers, query.Answer{Text: text, Image: image})
}
}

// WithTextAnswer appends a text answer to the response
func WithTextAnswer(text string) ExecuteCommandFuncResponseOption {
return func(r *ExecuteCommandFuncResponse) {
r.Answers = append(r.Answers, query.Answer{Text: text})
}
}

// WithState sets a different state to the response's FSM
func WithState(state int) ExecuteCommandFuncResponseOption {
return func(r *ExecuteCommandFuncResponse) {
r.FSM.State = state
}
}

// WithSlot sets a slot value to the response's FSM
func WithSlot(key, value string) ExecuteCommandFuncResponseOption {
return func(r *ExecuteCommandFuncResponse) {
r.FSM.Slots[key] = value
}
}