Skip to content

Instantly share code, notes, and snippets.

@depado
Created March 27, 2018 13:34
Show Gist options
  • Save depado/9b43f2b78d09c67a2426a70ee95ad9fc to your computer and use it in GitHub Desktop.
Save depado/9b43f2b78d09c67a2426a70ee95ad9fc to your computer and use it in GitHub Desktop.
Gists Linked to the DialogFlow article https://blog.depado.eu/post/dialogflow-golang-webhook
func webhook(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{})
}
func main() {
r := gin.Default()
r.POST("/webhook", webhook)
if err := r.Run("127.0.0.1:8001"); err != nil {
panic(err)
}
}
out := fmt.Sprintf("I found that cocktail : %s", d.StrDrink)
dff := &df.Fulfillment{
FulfillmentMessages: df.Messages{
{RichMessage: df.Text{Text: []string{out}}},
df.ForGoogle(cardFromDrink(d)),
},
}
c.JSON(http.StatusOK, dff)
type searchParams struct {
Alcohol string `json:"alcohol"`
DrinkType string `json:"drink-type"`
Name string `json:"name"`
}
func search(c *gin.Context, dfr *df.Request) {
var err error
var p searchParams
if err = dfr.GetParams(&p); err != nil {
logrus.WithError(err).Error("Couldn't get parameters")
c.AbortWithStatus(http.StatusBadRequest)
return
}
spew.Dump(p)
c.JSON(http.StatusOK, gin.H{})
}
func specify(c *gin.Context, dfr *df.Request) {
var err error
var p searchParams
if err = dfr.GetContext("Search-followup", &p); err != nil {
logrus.WithError(err).Error("Couldn't get parameters")
c.AbortWithStatus(http.StatusBadRequest)
return
}
spew.Dump(p)
c.JSON(http.StatusOK, gin.H{})
}
func webhook(c *gin.Context) {
var err error
var dfr *df.Request
if err = c.BindJSON(&dfr); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
spew.Dump(dfr)
c.JSON(http.StatusOK, gin.H{})
}
func webhook(c *gin.Context) {
var err error
var dfr *df.Request
if err = c.BindJSON(&dfr); err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
switch dfr.QueryResult.Action {
case "search":
log.Println("Search action detected")
c.JSON(http.StatusOK, gin.H{})
case "random":
log.Println("Search action detected")
c.JSON(http.StatusOK, gin.H{})
default:
log.Println("Unknown action")
c.AbortWithStatus(http.StatusNotFound)
}
}
func search(c *gin.Context, dfr *df.Request) {
}
func random(c *gin.Context, dfr *df.Request) {
}
func random(c *gin.Context, dfr *df.Request) {
var err error
var d *cocktail.FullDrink
if d, err = cocktail.C.GetRandomDrink(); err != nil {
logrus.WithError(err).Error("Coudln't get random drink")
c.AbortWithStatus(http.StatusInternalServerError)
return
}
fmt.Println(d)
c.JSON(http.StatusOK, gin.H{})
}
out := fmt.Sprintf("I found that cocktail : %s", d.StrDrink)
dff := &df.Fulfillment{
FulfillmentMessages: df.Messages{
{RichMessage: df.Text{Text: []string{out}}},
df.ForGoogle(df.SingleSimpleResponse(out, out)),
},
}
c.JSON(http.StatusOK, dff)
// Message is a struct holding a platform and a RichMessage.
// Used in the FulfillmentMessages of the response sent back to dialogflow
type Message struct {
Platform
RichMessage RichMessage
}
// Platform is a simple type intended to be used with responses
type Platform string
// Platform constants, used in the webhook responses
const (
Unspecified Platform = "PLATFORM_UNSPECIFIED"
Facebook Platform = "FACEBOOK"
Slack Platform = "SLACK"
Telegram Platform = "TELEGRAM"
Kik Platform = "KIK"
Skype Platform = "SKYPE"
Line Platform = "LINE"
Viber Platform = "VIBER"
ActionsOnGoogle Platform = "ACTIONS_ON_GOOGLE"
)
func cardFromDrink(d *cocktail.FullDrink) df.BasicCard {
card := df.BasicCard{
Title: d.StrDrink,
FormattedText: d.StrInstructions,
Image: &df.Image{
ImageURI: d.StrDrinkThumb,
},
}
return card
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment