taught us how to create simple push-button bots, this article delves into integrating a bot with a weather service. Similarly, the bot can be integrated with CRM systems.
Creating External Requests
The first step is to create an
@External Request
— this is a functionality enabling the
@Agent
to access external systems via API. Using
@External Request
s, we will send data to the weather service and receive the information required for the
@Limit on the size of the received body in response to an External Request
. If the body exceeds the limits, then the
@raw_response
variable, which contains the external system’s response to the
@External Request
, records not the response, but the error text. To get the response text, you need to configure its parsing by writing it to the
@User Context Variables
on the Response tab. To do this, go to the Response tab, designated for parsing the response to a request, and input “result” in the Variable field and {{ body }} in the Value field. This way, the entire response body will be stored in the result variable and will be available for use in the
@Agent
@Script
. For additional information regarding the Response tab:
by clicking the Create new button. Name it “weather 2”, keep the request method as GET, and input {{ url2 }} in the Endpoint field. Later we will create this
@Context Variable
in our
@Agent
@Script
.
Go to the Response tab and and input “result2” in the Variable field and {{ body }} in the Value field to save the entire response body in the result2 variable and use it in the
@Agent
@Script
.
Save the request by clicking the CREATE button.
Creating an Agent Script
The
@External Request
s are ready, now you need to create the
@Agent
and its
@Script
.
Go to
@Projects Tab
by clicking Dashboard button on the upper panel.
s will be compiled. The URL format for the first request is: http://api.openweathermap.org/geo/1.0/direct?q={city name}&appid={API key}. We need to compose a URL by substituting the name of the city entered by the
@Bot User
and your personal API key.
Name the
@Slot
“compose url for request”.
Enter the url value in the Key field, and the value 'http://api.openweathermap.org/geo/1.0/direct?q={{ client_message }}&appid=API key' in the Value field, where API key is the key from the
‘s message. In the Memory Slot, The Context Variable intended for substitution must be specified in the particular format: it needs to be enclosed within double curly quotes. The entire field value must be enclosed in quotes in order for it to be written to the variable as a string. In this way, the URL for the first request will be compiled and stored in the url
@Context Variable
, which we previously specified in the
@External Request
as the Endpoint.
Save the
@Slot
.
After the
@Memory
@Slot
, add an
@External Request
@Slot
— it will be used to integrate with the weather service. Enter the name “receiving coordinates” in the Name field. In the Request field, select
@External Request
called “weather”.
If the
@Bot User
specifies a non-existent city name, we will receive an empty response from the weather service, i.e. The
@Context Variable
@raw_response
will have the value []. We will use this in the
@Script
to prompt the
@Bot User
to enter a different name. To do this, add a
@Transition Rule
@Slot
after the
@External Request
@Slot
— it enables
@Agent
@Script
branching depending on the specified conditions. Read more:
s are automatically generated. If a condition in a
@Subslot
is true, the Agent will proceed to the Script Branch following this
@condition
@Subslot
.
We need to check whether the value of the
@Context Variable
@raw_response
is [].Open the "main"
@Subslot
and enter {{ raw_response == '[]' }} in the Condition field. Then, clear the Name field so that the expression from the Condition field will be displayed in the
@Script Tree
on the
@condition
@Subslot
.
After this
@Subslot
, add a
@Text
@Slot
with the text “I don’t know such a city. Please enter a different name.” and the
@Jump
@Slot
with a transition to the
@Wait For Reaction
@Slot
, where the
@Agent
waits for the
@Bot User
to enter the name of the city.
If the city is found in the weather service, a response in the following format will be received:
[
{
"name":"city name",
"local_names":{
city name in different languages
},
"lat": latitude,
"lon": longitude,
"country":"code of the country",
"state":"name of the country"
}
]
After the FALLBACK_CONDITION
@Subslot
, add a
@Memory
@Slot
to extract the coordinates of the city from the response received from the service.
Name the
@Slot
“saving coordinates”.
Enter the value lat in the Key field, and the value {{ result[0].lat }} in the Value field.
Add another Key - Value pair by clicking Add new field.
Enter the value lon in the Key field, and the value {{ result[0].lon }} in the Value field.
This is how the first element of the result array and the variables lat and lon contained in it are accessed — the latitude and longitude values. More information about the syntax
. The URL format for the second request is: https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={API key}. We need to compose a URL by substituting the coordinates of the city entered by the
@Bot User
and our personal API key.
Add another Key - Value pair. Enter the value url2 in the Key field, and the value 'https://api.openweathermap.org/data/2.5/weather?lat={{ lat }}&lon={{ lon }}&appid=API key' in the Value field , where API key is the key value from
, and {{ lat }} and {{ lon }} are coordinate variables. This will compose the URL for the second request and store it in the url2
@Context Variable
, which we previously specified in the
@External Request
as the Endpoint.
Save the
@Slot
.
Next, create an
@External Request
@Slot
named “receive weather” and select the “weather 2”
@External Request
in it.
In response to the request, we expect to receive a response in the following format (the response has a Dictionary data format):
{
"coord": {
city coordinates
},
"weather": [
{
"id": 501,
"main": "weather",
"description": "weather description",
"icon": "10d"
}
],
"base": "stations",
"main": {
"temp": temperature in Kelvin degrees,
"feels_like": what the temperature feels like in Kelvin degrees,
"temp_min": minimal temperature in Kelvin degrees,
"temp_max": maximal temperature in Kelvin degrees,
"pressure": atmospheric pressure,
"humidity": air humidity,
"sea_level": atmospheric pressure on sea level,
"grnd_level": atmospheric pressure on ground level
},
"visibility": visibility in meters,
"wind": {
data about wind
},
"rain": {
data about rain
},
"clouds": {
data about clouds
},
"dt": 1661870592,
"sys": {
system data
},
"timezone": timezone,
"id": 3163858,
"name": "name of the city",
"cod": system parameter
}
The
@Agent
will display temperature data to the
@Bot User
. To do this, add a
@Memory
@Slot
after the
@External Request
@Slot
.
Give the
@Slot
the name “saving weather data”.
First, let's save the value of the "main" element, which contains temperature data, into a variable. The service response is contained in the result2 variable, which has the format of a
. A dictionary element is accessed through square brackets indicating the name of the element. Enter the value “var1” in the Key field, and the value {{ result2['main'] }} in the Value field.
Next, save the values of the actual and felt air temperature. To do this, add two more Key - Value pairs. In the Key fields, enter the values var2 and var3, and in the Value fields — {{ var1['temp'] }} and {{ var1['feels_like'] }}.
Since the service returns temperature data in degrees Kelvin, let’s convert them to degrees Celsius. Temperature in degrees Celsius is calculated using the formula {temperature in degrees Kelvin}-273.15. Add two more Key - Value pairs: in the Key fields, write “temperature” and “feels_like”, and in the Value fields, input {{ var2 - 273.15 }} and {{ var3 - 273.15 }}, respectively. More information about mathematical operations in the syntax:
with the text “The temperature in {{ client_message }} is {{ temperature }} ºC at the moment. Feels like {{ feels_like }}ºC. Would you like to try with another city?” and
@Jump
@Slot
with а transition to the button menu.
This concludes the creation of the Agent Script. You should now have the following Script:
Agent training and testing
After creating the
@Agent
@Script
, you need to train the
@Agent
and test its
@Script
.
To train the
@Agent
, click the TRAIN button located in the upper right corner of