Created
March 27, 2018 13:34
-
-
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
This file contains 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 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) | |
} | |
} |
This file contains 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
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) |
This file contains 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
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{}) | |
} |
This file contains 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 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{}) | |
} |
This file contains 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 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{}) | |
} |
This file contains 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 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) | |
} | |
} |
This file contains 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 search(c *gin.Context, dfr *df.Request) { | |
} | |
func random(c *gin.Context, dfr *df.Request) { | |
} |
This file contains 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 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{}) | |
} |
This file contains 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
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) |
This file contains 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
// 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 | |
} |
This file contains 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
// 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" | |
) |
This file contains 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 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