Skip to content

Intent Router

Caleb Hiebert edited this page Nov 16, 2018 · 3 revisions

The intent router is a middleware that allows routing to different handlers based on the intent flag

Here is a code snippet showing how to use the intent router

func main() {
	gobblr := gbl.New()
	gobblr.Use(gbl.RequestExtractionMiddleware())

	// Create a dummy middleware to simulate an intent
	gobblr.Use(func(c *gbl.Context) {
		c.Flag("intent", "hello")
		c.Next()
	})

	// Create a new intent router
	ir := gbl.IntentRouter()

	// Add a handler for the "hello" intent
	// This handler will be called whenever the "intent" flag is set to "hello"
	ir.Intent("hello", func(c *gbl.Context) {
		// When using the console integration, the context R (response) object
		// we need to cast it so we can use it's functions
		basicResponse := c.R.(*gbl.BasicResponse)

		// Add a text message to the output
		basicResponse.Text(fmt.Sprintf("Hello: %s", c.Request.Text))
	})

	// Make sure the bot is using the middleware generated by the router
	gobblr.Use(ir.Middleware())

	ci := gbl.ConsoleIntegration{}
	ci.Listen(gobblr)
}

Clone this wiki locally