Skip to content

Messenger

Caleb Hiebert edited this page Nov 19, 2018 · 6 revisions

The messenger integration is split up into 2 parts, the first part is setting up the integration, the second part is sending responses.

Here is a code snippet showing how to set up the integration

func main() {

	// Create a new bot
	gobblr := gbl.New()

	// First, create a messenger API
	// The messenger api takes one argument, your page access token
	mapi := fb.CreateMessengerAPI("PAGE_ACCESS_TOKEN")

	// Using the API, create an integration
	mi := fb.MessengerIntegration{
		API:         mapi,
		VerifyToken: "frogs",
	}

	// Apply the UserExtractionMiddleware, this is a built in middleware that will extract the user object
	// from any facebook webhooks
	gobblr.Use(gbl.UserExtractionMiddleware())

	// Apply the RequestExtractionMiddleware, this is a built in middleware that will extract a bit of text
	// from facebook webhook requests
	gobblr.Use(gbl.RequestExtractionMiddleware())

	// This is a facebook specific middleware, it will mark the user's message as seen in the chat
	// it does this in a goroutine to avoid slowing down the response process
	gobblr.Use(fb.MarkSeenMiddleware())

	// Create a very simple echo middleware
	gobblr.Use(func(c *gbl.Context) {

		// CreateResponse is a helper that will create a new response object and add it to the context for you
		// the facebook integration uses this object to respond to webhook requests
		r := fb.CreateResponse(c)

                // Using the response helpers, this is how you add a text response
		r.Text(fmt.Sprintf("Echo: %s", c.Request.Text))
	})

	// The facebook integration comes with it's own webhook server
	// This uses go's built in http library, to avoid adding dependencies, and to keep everything speedy
	// Avoid setting the usual Handler parameter, because the integration overwrites it
	mi.Listen(&http.Server{
		Addr: ":8080",
	}, gobblr)
}

Clone this wiki locally