The Make API call step lets a conversation bot use a REST API request to fetch data from an external system, such as Shopify or Salesforce.
In this recipe, you’ll create a bot answer that retrieves weather data for a location provided by an end user. The answer gets the data from the OpenWeather API. You can use the answer flow created in this recipe as a starting point for building your own API-powered answers.
- Task 1: Checking your setup
- Task 2: Creating an API connection
- Task 3: Creating a custom ticket field
- Task 4: Building a bot answer
- Task 5: Testing your changes
Task 1: Checking your setup
To complete this recipe, you’ll need the following:
- A Zendesk account with a published conversation bot. If you want, you can use a sandbox testing environment to test the
conversation bot before using it in production.
To set up a conversation bot on a web and mobile messaging channel, see Creating a conversation bot for your web and mobile channels. For information about using messaging in a sandbox environment, see Using messaging in your sandbox.
- An API key for an
OpenWeatheraccount. The API used in this recipe is
available on OpenWeather’s free plan.
To sign up for a free OpenWeather account, see the Create New Account page on openweathermap.org. After signing in, you can get an API key on your OpenWeather account page.
To activate OpenWeather API keys, you must verify your account email address. After verification, API keys can take up to two hours to activate. For more information, see the FAQ on openweathermap.org.
Task 2: Creating an API connection
To start, create an API connection to store your OpenWeather API key. Your conversation bot can use this connection to authenticate calls to the OpenWeather API.
- In Admin Center, click Apps and integrations in the sidebar, then select Connections > Connections.
- Click Create connection.
- Select the API key authentication type.
- Enter openweather_api_key as the Connection name.
- Enter x-api-key as the Header name.
- Enter your OpenWeather API key as the Value.
- Enter api.openweathermap.org as the Allowed domain.
- Click Save to create the connection.
Task 3: Creating a custom ticket field
Next, create a custom ticket field to capture the location provided by the end user.
- In Admin Center, click Objects and rules in the sidebar, then select Tickets > Fields.
- Click Add field.
- Select the Text field type.
- Enter Location as the Display name.
- Under Permissions, select Customers can edit.
- Under Customers, enter Location as the Title shown to customers.
- Click Save.
If your account uses a single ticket form, the new field automatically appears in your ticket form. To remove the field, see Editing ticket forms.
Task 4: Building a bot answer
This section walks you through creating the answer, setting its intent, and adding steps for the answer in the bot builder. This involves the following steps:
- Step 1: Creating the answer
- Step 2: Setting the answer’s intent
- Step 3: Adding an initial message step
- Step 4: Asking the end user for a location
- Step 5: Retrieving weather data for the location
- Step 6: Sending a weather message to the end user
- Step 7: Adding a failure message
- Step 8: Publishing your updated bot
Step 1: Creating the answer
Use bot builder to create an answer for your conversation bot.
- In Admin Center, click Channels in the sidebar, then select AI agents and automation > AI agents.
- Click Manage conversation bots.
- Click the conversation bot you want to work with.
- On the Answers tab, click Create answer.
Step 2: Setting the answer’s intent
Set the answer’s intent and training phrases. These include phrases and words used to trigger the answer in a conversation.
- On the Answers tab of the bot’s edit page, click Create answer.
- Click Build your own answer, then click Next.
- Enter Get weather as the answer’s Intent.
- Under Training phrases, enter Get weather forecast and Get current temperature.
- Click
Next.
The answer opens in the bot builder where you can build out the response.
Step 3: Adding an initial message step
Add a Send message step to ask the user for a location. This is the first step in the answer’s flow.
- In the bot builder, click Add step.
- Under Choose step, select Send message.
- In
Bot message, enter the following
text:
What location would you like to get the weather for? Include the country code. For example: "Melbourne, AU" or "San Francisco, US"
Step 4: Asking the end user for a location
After the initial message, add an Ask for details step to present a basic form to the end user. The form includes a text box for the Location ticket field’s value.
- In the bot builder, click the Add step icon ( ).
- Under Choose step, select Ask for details.
- Enter Get location as the Name.
- In Fields, type and select the Location ticket field.
Step 5: Retrieving weather data for the location
Add a Make API call step to get weather data for the location from the OpenWeather API’s Current weather data endpoint.
- In the bot builder, click Add step.
- Under Choose step, select Make API Call.
- Enter Get weather as the Name.
- Under
API details,
enter
https://api.openweathermap.org/data/2.5/weather?units=metric&q=
as the Endpoint URL. - Use the
Add a variable
icon (
) to append the
Location
field variable to the
Endpoint URL value.
The finished Endpoint URL value should look like this:
- In Authentication, select the openweather_api connection.
- To test the API request, click Make API call.
- Under Test Data, enter Melbourne, AU as the Location.
- Click Make API call.
- Save the following variables using their default names:
- main > temp
- weather > item 1 > description
Step 6: Sending a weather message to the end user
After retrieving the weather data, use a Send message step to respond with a message containing the weather data.
- In the bot builder, click Add step under the Get weather step’s API call successful branch.
- Under Choose step, select Send message.
- In
Bot message, enter the following
text:
It's {{temp}}° C with {{description}} in
- Use the
Add a variable
icon (
) to append the
Location
field variable to the
Bot message
value. Then
add a period (.).
The finished Bot message value should look like this:
Step 7: Adding a failure message
The request made during the Make API call step may fail. For example, the request may include a location that doesn’t exist. Add a Send message step to the API call failed branch. This step returns a message if the OpenWeather API call fails.
- In the bot builder, click Add step under the Get weather step’s API call failed branch.
- Under Choose step, select Send message.
- In Bot message, enter the following
text:
I'm sorry. I wasn't able to get weather data for
- Use the
Add a variable
icon (
) to append the
Location
field variable to the
Bot message
value. Then
add a period (.).
The finished Bot message value should look like this:
Step 8: Publishing your updated bot
To push the answer live, publish the updated bot.
- Click Done in the upper right corner of the bot builder.
- On the bot page, click Publish bot.
- Click Publish.
Task 5: Testing your changes
After you publish your changes, you can test the new answer by asking “How’s the weather?” in a conversation with the updated bot. For more information about testing conversation bots, see Testing the end user's messaging experience.