Skip to content

Commit

Permalink
Merge pull request #5 from milobella/upgrade
Browse files Browse the repository at this point in the history
Upgrade go 1.19 and SDK v0.9.0
  • Loading branch information
celian-garcia authored Sep 27, 2022
2 parents f84ec2d + b7e33f9 commit 93904dd
Show file tree
Hide file tree
Showing 4 changed files with 506 additions and 164 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

########################################################################
### builder stage : Build the golang application in src folder
FROM golang:1.16-alpine as builder
FROM golang:1.19-alpine as builder

COPY . /src
WORKDIR /src
Expand Down
80 changes: 40 additions & 40 deletions cmd/ability/main.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
package main

import (
"github.com/milobella/ability-sdk-go/pkg/ability"
"github.com/milobella/ability-sdk-go/pkg/config"
"github.com/milobella/ability-sdk-go/pkg/model"
"github.com/milobella/ability-sdk-go/pkg/server"
"github.com/milobella/ability-sdk-go/pkg/server/conditions"
"github.com/milobella/ability-shoppinglist/pkg/shoppinglist"
)

var shoppingListClient *shoppinglist.Client

const (
deleteAction="DELETE"
addAction="ADD"
itemsSlot="ITEMS"
itemEntity="SHOPITEM"
deleteAction = "DELETE"
addAction = "ADD"
itemsSlot = "ITEMS"
itemEntity = "SHOPITEM"
)

// fun main()
func main() {
// Read configuration
conf := ability.ReadConfiguration()
conf := config.Read()

// Initialize client for shopping list tool
shoppingListClient = shoppinglist.NewClient(conf.Tools["shoppinglist"].Host, conf.Tools["shoppinglist"].Port)

// Initialize server
server := ability.NewServer("Shopping List Ability", conf.Server.Port)
server.RegisterIntentRule("TRIGGER_SHOPPING_LIST", triggerShoppingListHandler)
server.RegisterIntentRule("REMOVE_FROM_LIST", removeFromListHandler)
server.RegisterIntentRule("REMOVE_FROM_SHOPPING_LIST", removeFromListHandler)
server.RegisterIntentRule("ADD_TO_LIST", addToListHandler)
server.RegisterIntentRule("ADD_TO_SHOPPING_LIST", addToListHandler)
server.RegisterIntentRule("EMPTY_LIST_ITEMS", emptyShoppingListHandler)
server.RegisterIntentRule("COUNT_LIST_ITEMS", countShoppingListHandler)
server.RegisterIntentRule("LIST_LIST_ITEMS", listShoppingListHandler)
server.RegisterRule(isRemoveContext, removeFromListHandler)
server.RegisterRule(isAddContext, addToListHandler)
server.Serve()
srv := server.New("Shopping List", conf.Server.Port)

// Register first the conditions on actions because they have priority on intents.
// The condition returns true if an action is pending.
srv.Register(conditions.IfInSlotFilling(deleteAction), removeFromListHandler)
srv.Register(conditions.IfInSlotFilling(addAction), addToListHandler)

// Then we register intents routing rules.
// It means that if no pending action has been found in the context, we'll use intent to decide the handler.
srv.Register(conditions.IfIntents("TRIGGER_SHOPPING_LIST"), triggerShoppingListHandler)
srv.Register(conditions.IfIntents("REMOVE_FROM_LIST", "REMOVE_FROM_SHOPPING_LIST"), removeFromListHandler)
srv.Register(conditions.IfIntents("ADD_TO_LIST", "ADD_TO_SHOPPING_LIST"), addToListHandler)
srv.Register(conditions.IfIntents("EMPTY_LIST_ITEMS"), emptyShoppingListHandler)
srv.Register(conditions.IfIntents("COUNT_LIST_ITEMS"), countShoppingListHandler)
srv.Register(conditions.IfIntents("LIST_LIST_ITEMS"), listShoppingListHandler)

srv.Serve()
}

func isRemoveContext(req *ability.Request) bool {
return req.IsInSlotFillingAction(deleteAction)
}

func isAddContext(req *ability.Request) bool {
return req.IsInSlotFillingAction(addAction)
}

func removeFromListHandler(req *ability.Request, resp *ability.Response) {
func removeFromListHandler(req *model.Request, resp *model.Response) {
// Retrieve only shopping items from NLU entities
items := collectItemsFromRequest(req)

Expand All @@ -66,14 +66,14 @@ func removeFromListHandler(req *ability.Request, resp *ability.Response) {

// Build the NLG answer
resp.Nlg.Sentence = "I removed {{items}} from your shopping list."
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "items",
Value: items,
Type: "enumerated_list",
}}
}

func addToListHandler(req *ability.Request, resp *ability.Response) {
func addToListHandler(req *model.Request, resp *model.Response) {
// Retrieve only shopping items from NLU entities
items := collectItemsFromRequest(req)

Expand All @@ -94,30 +94,30 @@ func addToListHandler(req *ability.Request, resp *ability.Response) {

// Build the NLG answer
resp.Nlg.Sentence = "I added {{items}} to your shopping list"
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "items",
Value: items,
Type: "enumerated_list",
}}
}

func triggerShoppingListHandler(_ *ability.Request, resp *ability.Response) {
func triggerShoppingListHandler(_ *model.Request, resp *model.Response) {
items, err := shoppingListClient.GetItems()
if err != nil {
resp.Nlg.Sentence = "Error receiving items from your shopping list."
return
}
// Build the NLG answer
resp.Nlg.Sentence = "You have {{count}} items in your main shopping list, what do you want to do ?"
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "count",
Value: len(items),
Type: "string",
}}
resp.AutoReprompt = true
}

func emptyShoppingListHandler(_ *ability.Request, resp *ability.Response) {
func emptyShoppingListHandler(_ *model.Request, resp *model.Response) {
// Here we count the items
var count = -1
items, err := shoppingListClient.GetItems()
Expand All @@ -144,14 +144,14 @@ func emptyShoppingListHandler(_ *ability.Request, resp *ability.Response) {
}

resp.Nlg.Sentence = "{{count}} items has been removed from your shopping list."
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "count",
Value: count,
Type: "string",
}}
}

func countShoppingListHandler(_ *ability.Request, resp *ability.Response) {
func countShoppingListHandler(_ *model.Request, resp *model.Response) {
items, err := shoppingListClient.GetItems()
if err != nil {
resp.Nlg.Sentence = "Error counting items from your shopping list."
Expand All @@ -164,14 +164,14 @@ func countShoppingListHandler(_ *ability.Request, resp *ability.Response) {
}

resp.Nlg.Sentence = "You have {{count}} items in your shopping list."
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "count",
Value: count,
Type: "string",
}}
}

func listShoppingListHandler(_ *ability.Request, resp *ability.Response) {
func listShoppingListHandler(_ *model.Request, resp *model.Response) {
items, err := shoppingListClient.GetItems()
if err != nil {
resp.Nlg.Sentence = "Error receiving items from your shopping list."
Expand All @@ -185,7 +185,7 @@ func listShoppingListHandler(_ *ability.Request, resp *ability.Response) {

if count == 1 {
resp.Nlg.Sentence = "You only have one element in your shopping list. There is {{item}."
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "item",
Value: items[0],
Type: "string",
Expand All @@ -194,7 +194,7 @@ func listShoppingListHandler(_ *ability.Request, resp *ability.Response) {
}

resp.Nlg.Sentence = "You have {{count}} items in your shopping list. There are {{items}}."
resp.Nlg.Params = []ability.NLGParam{{
resp.Nlg.Params = []model.NLGParam{{
Name: "count",
Value: count,
Type: "string",
Expand All @@ -205,6 +205,6 @@ func listShoppingListHandler(_ *ability.Request, resp *ability.Response) {
}}
}

func collectItemsFromRequest(req *ability.Request) []string {
func collectItemsFromRequest(req *model.Request) []string {
return req.GetEntitiesByLabel(itemEntity)
}
45 changes: 42 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
module github.com/milobella/ability-shoppinglist

go 1.16
go 1.19

require (
github.com/milobella/ability-sdk-go v0.4.2
github.com/sirupsen/logrus v1.5.0
github.com/milobella/ability-sdk-go v0.9.0
github.com/sirupsen/logrus v1.9.0
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/iamolegga/enviper v1.4.0 // indirect
github.com/labstack/echo/v4 v4.9.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/milobella/oratio v1.4.3 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.13.0 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 93904dd

Please sign in to comment.