Now you will make your first API Call.
Create an account at WeatherAPI and login:
Choose Sign up
Fill in the needed information
Select Sign up.
An email will be sent to you with a link. Select the link to activate your account.
Login to your account.
The images below will walk you through it.
Your API Key will be located right on your home page. Copy it and then go to the WeatherAPI Interactive API Explorer, which is linked on the left-hand navigation.
Paste the API Key into the API field, put in a zip code or city, and then select Show Response.
Now you know the API Key works and you are given the API Call URL so copy that.
Go back to your bot and go to Build...Configurations...Environment Variables...Add Variable.
Name the Variable Weather API Call URL and paste the Call URL. Save when you are finished.
FYI: You DO NOT have to create an Environment Variable. You could paste the URL directly into the Service node we will create later. Environment Variables are useful when you plan to reuse a URL over and over.
Login, open your bot and go to Build...Dialog Tasks...Create Dialog.
Title it Weather API Call for any City. Under Intent Training, add the user utterance of “Get the weather” and then press Enter. Now the bot will know when the user is asking for this task. Select Proceed.
Add an Entity node by clicking the plus sign, choosing Entity and New Entity. Name it CityorZip with a user prompt of, “Please enter a city or zipcode.” Click the x icon in the top right of the panel when you are done.
Next, add a new Message node that lets the user know the bot is working on getting the weather for their location. Name this node CityorZipEcho. The message will say, “Give me just a moment to get the weather report for {{context.entities.CityorZip}}." This special formatting of using curly brackets and the name of our Entity node allows you to personalize the message to your user.
Add a new Bot Action node and name it GetWeather. Inside the Bot Action node, add the action of Service. Name it AnyCityWeatherAPICall. Use the default settings of Custom Service, Webservice and REST. Under Request Definition, select Edit Request.
This will be a GET request so leave that as the default. You need the Call URL, which you have saved as a variable so now, instead of tracking down that long URL, you can just reference it in curly brackets. Env stands for Environment Variable and WeatherAPICall was the name of the variable.
Now, you can reference the city or zip code that the user will supply. Next, select Test Request. You can see the URL pulls up, but there isn’t value for the city so you need to supply one in order to test this out.
Select Test. You're looking for a status code of 200 which means everything worked.
Save your sample response and then save the service request. Use the arrow in the top left to exit.
Click the X to close the bot action area.
In the Message node that follows, you will be reporting the weather details back to the user so you need to know how to read the JSON response. Below is a sample response. The reference will always be in curly brackets and will begin with context followed by the name of the Service node and then each level of the JSON response.
Add a new Message node named ReportAnyCityWeathertoUser. Under Bot Responses, choose Manage. Open the Prompt (click on it) to edit the message.
The message should say:
The current condition for {{context.AnyCityWeatherAPICall.response.body.location.name}} is {{context.AnyCityWeatherAPICall.response.body.current.condition.text}}. The temperature is {{context.AnyCityWeatherAPICall.response.body.current.temp_f}} degrees F.
Save the prompt and return to the Build Canvas.
Add a Confirmation node called NewCityorZip that will ask, “Do you want to check another location?” You will need to return to this node in a moment to configure what happens when the user says ‘yes, no or something else.’
Click one of the plus signs under the Confirmation node and add another Bot Action called ClearEntity and add a Script node named ClearCityorZip. We will use some basic JavaScript to clear the previous city the user inquired about so the bot can accept a new city. When using JavaScript, you don’t have to use the curly brackets. We will simply state that context.entities.CityorZip = undefined. That will delete the city that is currently being held in the bot’s memory. Save that.
Open the Connections panel of the Bot Action. After this node, you want the bot to return to the CityorZip node to collect another location. Use the Drop down menu to choose the CityorZip node. Close the Bot Action.
Click another plus sign and add a Dialog Task. Choose the Feedback Survey you created as a Dialog Task called Weather Rating. Open the Connections panel and choose End of Dialog. That means this task will conclude right after this node is processed.
Adjust the connections between these last few nodes. Open the NewCityorZip node and click on the Connections panel. Delete the first rule. For Yes, use the drop-down menu to go to the ClearEntity Bot Action. For No or anything Else, have the bot to go to the Feedback Survey named Weather Rating.
This is what your completed Dialog Task should look like.
Test it out with Talk to Bot. The icon is in the lower right of the screen.
Launch the Debug log by selecting the bug icon. This will allow you to see where problems occur. Say, "Check the Weather".
You can watch as the nodes of the Dialog Task are executed. If the bot encounters any problems, they will be marked with a red error label. That tells you where to look. Usually, the problem has to do with an error in your code. For example, maybe you left out a period or you didn’t capitalize a letter. When writing code, every single digit must be exact!
Next, you will deploy and publish your bot.