To get the time for a place we need to geocode the location we were asked for. Let's use an API by Google to accomplish that.
- [Instructor] Our goal is to get the local time for city name, and to find that out we need the time zone a city is in. Luckily, Google provides an API for getting the time zone for our location; and if we look how this time zone API works actually, so there is aN sample request down here. We see that the location has to be paste in as coordinates longitude and latitude. To get this information, we need to geocode the city name; and Google provides a geocoding API as well.
So geocoding means that a location name will be converted into longitude and latitude coordinates. To use this API, you will need a Google account to create an API key. You most probably have one; otherwise please create one. Then on top of the page, let's click on get a key. You might be asked to select a project and then click on enable API here. And here is your API key. We copy this out and open visual studio and let's put it in here as comment and to know which key this is, this is the geocoding key.
We copy it in here, just for later reference. Now let's go back to the API documentation. Let's close this here, and let's copy out this whole sample request; and, again, let's open visual studio code and copy it in here. And let's look at this sample request. Here we see that there is an address parameter and also on the end there is a placeholder for our API key.
So what we can do now is we will copy out this API key, add it here, replacing the placeholder, and maybe as address we now will simply name a city name. So make sure you remove everything between address and this key parameter here, and let's use Vienna as address.
Now copy out this whole request, open your browser again and copy this into your address bar and let's see what happens. We see that we get adjacent object back that has a results ray here and the geometry property here. We see that we get adjacent object back that has a results ray and a geometry property within and there, again, there is a location property that finally has the latitude and longitude coordinates.
It's right here. So let's implement this request in node now. So for that we open visual studio code again and to do this request, we need the superagent module again. We used it before to communicate with width. So I type in npm install --save superagent. And after that we also want to (mumbles) here like we did it before, const request equals require superagent.
Now we will rewrite the route we created before. It is here and here. On line 11 we now add request.get and then for now just paste in the sample request we had before copied from here and pasted in there, and instead of Vienna, we now want to simply use this request parameter we got before.
You see it here req.params.location, and we paste it in here. While we're here we also now can remove that, and here is callback. We now add again an anonymous function that takes the parameters error and responds and, again, has this function body in curly brackets. And in this function body, we will now simply add that first error (mumbles) we always want to create when we have a callback.
So in this case we want to console.log the error and return a response to the calling service that tells us something went wrong and this is, in this case, an error 500 HTTP code. If everything went right, we simply want to reply with longitude and latitude now. Note that we will simply return the first result of the result object, and this is a little bit of a shortcut because if a location name is (mumbles), we might get the wrong city and in real scenarios, you would maybe want to ask the user to repeat the question with a more precise location.
But for now we just reply with res.json and here we simply use our response.body.results and this in the ray, so the first element geometry.location. Now we will start our time service again by running node bin run.js and now we will open our browser again and, as we did it before, we call local host with the port 3010 and service and add some city name.
In this case, I chose Berlin and hit enter, and we get the latitude and the longitude of Berlin back. Fine.
After explaining some basics about Node.js and microservices, Daniel shows you how to sketch out the planned architecture for your application and get the boilerplate code, modules, and credentials in place. Next, he shows how to create a bot user in Slack, connect to Slack, and post messages. He also shows you how to get your bot to process variations in text by creating logic that delegates the processing of intent to dedicated modules. Lastly, he shows how to register additional services and he covers how to use monitoring to identify architectural or performance issues.
- Using Slack APIs
- Sketching out a Slack bot architecture
- Setting up a project and choosing modules
- Creating and naming your bot
- Connecting to Slack
- Setting up and using natural language processing
- Routing by intents
- Implementing geocoding and time calculation
- Adding and monitoring services