Created
July 9, 2016 18:06
-
-
Save egtann/8dc174ab9fdc776e4fabc0356d15d772 to your computer and use it in GitHub Desktop.
Abot v0.3 API WIP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func init() { | |
// plugin.AddTrigger replaces plugin.SetKeywords. Rather than specify | |
// all Commands and Objects, Abot will automatically handle that for | |
// you based on a few example sentences. | |
plugin.AddTrigger(p, kwRecommendRestaurants, []string{ | |
// This also matches similar sentences, like "where's a place | |
// to eat?", "is there a place to gnosh closeby?", "find me a | |
// good eatery", "locate a steakhouse for me", and "I want to | |
// go out to eat tonight" automatically. | |
"Find me a restaurant nearby", | |
}) | |
plugin.AddTrigger(p, kwGetPhone, []string{"What's their phone number?"}) | |
plugin.AddTrigger(p, kwGetPhone, []string{"How do I get there?"}) | |
plugin.AddTrigger(p, kwGetRating, []string{"What's it rated?"}) | |
plugin.AddTrigger(p, kwGetPictures, []string{"Can I see pics?"}) | |
plugin.AddTrigger(p, kwGetMenu, []string{"Show me the menu."}) | |
plugin.AddTrigger(p, kwIterate, []string{ | |
"I want something different.", | |
"What else is there?", | |
}) | |
// plugin.AddState replaces plugin.SetStates with its unwieldly | |
// [][]dt.State{} structure. Now you simply AddStates and Tasks in | |
// order. No more deep nesting of { { {. | |
plugin.AddState(p, dt.State{ | |
OnEntry: func(in *dt.Msg) string { | |
return "Where are you now?" | |
}, | |
OnInput: func(in *dt.Msg) { | |
extractAndSaveCities(in) | |
}, | |
Complete: func(in *dt.Msg) (bool, string) { | |
return p.HasMemory(in, prefs.Location), "" | |
}, | |
SkipIfComplete: true, | |
}) | |
plugin.AddState(p, dt.State{ | |
OnEntry: func(in *dt.Msg) string { | |
// Conversational "flow" words like "Ok." are added | |
// automatically. | |
return "What kind of restaurant are you looking for?" | |
}, | |
OnInput: func(in *dt.Msg) { | |
_ = kwRecommendRestaurants(in) | |
}, | |
Complete: func(in *dt.Msg) (bool, string) { | |
var complete bool | |
if p.HasMemory(in, "restaurantType") { | |
complete = true | |
} | |
return complete, "" | |
}, | |
SkipIfComplete: true, | |
}) | |
// To add a task, we use plugin.AddTask. | |
plugin.AddTask(p, task.Iterate(p, "iterate", task.OptsIterate{ | |
IterableMemKey: "restaurantSearchResultsStrings", | |
ResultMemKeyIdx: "selectedRestaurantIdx", | |
})) | |
plugin.AddState(p, dt.State{ | |
OnEntry: func(in *dt.Msg) string { | |
return "Let me know if you'd like to get the address or phone number, or see reviews, pictures, or the menu." | |
}, | |
OnInput: func(in *dt.Msg) {}, | |
Complete: func(in *dt.Msg) (bool, string) { return false, "" }, | |
}) | |
// plugin.SetOnReset replaces p.SM.SetOnReset for consistency. | |
plugin.SetOnReset(p, func(in *dt.Msg) { | |
task.ResetIterate(p, in) | |
p.DeleteMemory(in, "restaurantSearchResults") | |
p.DeleteMemory(in, "restaurantSearchResultsStrings") | |
p.DeleteMemory(in, "selectedRestaurantIdx") | |
p.DeleteMemory(in, "restaurantType") | |
}) | |
if err = plugin.Register(p); err != nil { | |
p.Log.Fatal("failed to register restaurants plugin.", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment