I assume that you’ve got Go arrange. There are many different tutorials on this. Additionally, that you already know your manner round utilizing the Fb Messenger API. Expertise constructing Bots utilizing different languages is a plus.
First, we’re utilizing dep. Its okay when you don’t. The benefit is simply that it “locks down” your dependencies in order that your code doesn’t break if the dependency (variations) change. So there are two exterior dependencies we’re utilizing: “yaml.v2” and “mux”.
Trending Chatbot Tutorials
Mux is a bundle that enables us outline routes for our API endpoints/URLs and connect the “VERB” (which means HTTP methods) and that’s all we have to get our routes/API endpoints cranking. Yaml.v2 is the bundle we’re utilizing to parse the YAML file we’re utilizing for our configuration. In fact we may have used common HTTP bundle in Go. Maybe that’ll be eliminated throughout main refactoring.
So when you’re utilizing dep, set up mux and yaml.v2
Create a brand new foremost.go file. Contained in the file, now we have the next code:
The getPort() perform permits us dynamically get the PORT. Cloud internet hosting platforms like Heroku assign us a port for any of our initiatives that we are able to’t management or know precisely. This perform permits us get the PORT ENV. It nevertheless defaults to port 3500 if the port is just not outlined.
So now we have our primary server up and working. In your terminal/command line, kind:
go run foremost.go
Going to localhost:3500 will give us the next
Yay!! We now have our Server up and working. Subsequent, we wish to setup the route and the suitable “HandlerFunctions” that permit us work with the webhooks we’ll be utilizing with Fb messenger. Subsequent, create webhooks.go file. On this file, we’ll have:
First, now we have a
struct known as
config. Right here, this struct will act because the blueprint of the construction of information we expect from our yaml config. Then now we have the
webhookGetHandler perform. From the title, this perform handles the
GET requests made to our webhook. With a purpose to confirm that our Fb webhook URL, Messenger ship a pattern
GET request to our endpoint. The request accommodates the question string
confirm token. It has to match the token now we have in our config file.
var tk Config
var v = getToken()
var _ = json.Unmarshal(byte(v), &tk)
Right here, there are lots of issues that don’t appear proper: we don’t have the
Config struct, we don’t have the
getToken() perform additionally (within the file). Don’t fear, we’re going to have all these in a unique file. This file will probably be accountable for parsing our yaml file.
right here, we’re getting the “hub.verify_token” question string from the incoming webhook request from Messenger. We’re doing the identical for “hub.problem”. We now get the token from the yaml file after which we examine with the incoming confirm token. In the event that they match, we’re sending the again the response to the server. This implies our webhook endpoint is all good and Messenger can ship all the required webhook occasions. On this response, we’re setting the Header as
Content material-Kind: utility/json which means that the content material is encoded as an
utility/json format. If the tokens don’t match, we’re telling the server that the tokens don’t match.