Skip to content

Context Routing

Caleb Hiebert edited this page Nov 17, 2018 · 2 revisions

GOBBL allows you to route to different handlers depending on the intent/context present. Below is a code snippet that is extended from the code snippet in the Context wiki page.

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

	// The session middleware relies on the c.User.ID property being set
	gobblr.Use(gbl.UserExtractionMiddleware())
	gobblr.Use(gbl.RequestExtractionMiddleware())

	// The context middleware relies on the
	// session middleware to store context information
	gobblr.Use(sess.Middleware(sess.MemoryStore()))

	// Add the context middleware
	gobblr.Use(bctx.Middleware())

	// Create a basic handler to test context functionality
	gobblr.Use(func(c *gbl.Context) {

		switch c.Request.Text {

		// Adding context to a conversation is quite easy,
		// simply pass in the name and the lifetime.
		// Setting the lifetime to -1 means it will live forever
		case "set":
			bctx.Add(c, "Context1", -1)
			bctx.Add(c, "Context2", 4)

		// Clearing a single context is quite easy as well
		// This will clear the context even if it's lifetime is set to -1
		case "clear":
			bctx.Clear(c, "Context1")

		// Adding a third context for testing below
		case "three":
			c.Flag("intent", "Three")
			bctx.Add(c, "Context3", -1)

		// This will clear all contexts, including ones with lifetimes set to -1
		case "clear all":
			bctx.ClearAll(c)
		}

		// The context middleware stores information in the _bctxDecoded flag
		// !!! THIS FLAG SHOULD NEVER BE MODIFIED DIRECTLY !!!
		fmt.Println(c.GetFlag("_bctxDecoded"))

		if !c.HasFlag("intent") {
			// Set a dummy intent for testing our contexts
			c.Flag("intent", "Dummy Intent")
		}

		c.Next()
	})

	// This is a ContextIntentRouter
	// It allows you to add handlers that will be executed when filtering
	// For different context/intent combos
	router := bctx.ContextIntentRouter()

	// This will match when the intent is "Three"
	// and ANY of the contexts are set
	router.Any(bctx.I{"Three"}, bctx.C{"Context1", "Context2", "Context3"}, func(c *gbl.Context) {
		c.R.(*gbl.BasicResponse).Text("Intent is three and any context is present")
	})

	// This will match when the intent is "Dummy Intent"
	// and BOTH "Context1" AND "Context2" are present
	router.All(bctx.I{"Dummy Intent"}, bctx.C{"Context1", "Context2"}, func(c *gbl.Context) {
		c.R.(*gbl.BasicResponse).Text("Context 1 and 2 present")
	})

	// This will match when the intent is "Dummy Intent"
	// and "Context1" is present
	router.Any(bctx.I{"Dummy Intent"}, bctx.C{"Context1"}, func(c *gbl.Context) {
		c.R.(*gbl.BasicResponse).Text("Context1 present")
	})

	// This will match when the intent is "Dummy Intent"
	// and "Context2" is present
	router.Any(bctx.I{"Dummy Intent"}, bctx.C{"Context2"}, func(c *gbl.Context) {
		c.R.(*gbl.BasicResponse).Text("Context2 present")
	})

	// Make sure the bot is using the router's middleware
	gobblr.Use(router.Middleware())

	// A handler to catch when none of the above handlers are triggered
	gobblr.Use(func(c *gbl.Context) {
		c.R.(*gbl.BasicResponse).Text("No Contexts Present")
	})

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

Clone this wiki locally