How to create a bot

icon picker
Creating a Bot with Weather Service Integration

The
@Platform
offers integration capabilities with various external systems, such as CRM or other services with open APIs. While the article 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
@Script
.
Register with the weather service at . This service allows you to make up to 1000 API calls per day for free.
The requests we need can be found at this request enables the retrieval of city coordinates, and this request facilitates weather retrieval based on location coordinates.
Register on the
@Platform
or log into an existing account using the link . You will be directed to the Company
@Project
s page. More information about registration and authorization: . ​
image.png
To create your first
@External Request
, go to the
@Resourses Tab
by clicking Resources on the upper panel. ​
image.png
Go to the section with
@External Request
s. More information about the tab: . ​
image.png
Create a new
@External Request
by clicking the Create new button. ​
image.png
Name the request “weather, keep the request method as GET, and input {{ url }} in the Endpoint field. This format is used to specify
@User Context Variables
in this field. To learn more about
@Context Variable
s please refer to: . For further details on
@User Context Variables
see: . Later we will create this
@Context Variable
in our
@Agent
@Script
. ​
image.png
In the
@Platform
, there is a
@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:
image.png
Save the request by clicking the CREATE button.
Create another
@External Request
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
. ​
image.png
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
. ​
image.png
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. ​
image.png
Create a new
@Agent
, as described in the article . ​
image.png
Go to
@BotBuilder
and create a
@Text
@Slot
with the text “Hello! I'm a meteorologist bot, I can tell you the weather in any city. Do you want to try?". ​
image.png
Next, add the “Yes” and “No” buttons. ​
image.png
After the “No” button, add a
@Text
@Slot
with the text “You can come back at any time.” and a
@Wait For Reaction
@Slot
. ​
image.png
After the
@fallback
@Subslot
, add a
@Text
@Slot
with the text “Please select one of the buttons.” and
@Jump
@Slot
with transition back to the button menu. ​
image.png
After the “Yes” button, add a
@Text
@Slot
with the text “Then write the name of the city.” and a
@Wait For Reaction
@Slot
. ​
image.png
Next, add a Memory Slot. This Slot enables you to write data to
@User Context Variables
. Learn more about the
@Memory
@Slot
:
image.png
Find your API key by going to and copy the value from the Key field. ​
image.png
In this
@Memory
@Slot
the Endpoint for the first of our
@External Request
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 , and {{ client_message }} is the
@client_message
@Context Variable
, which contains the
@Bot User
‘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
. ​
image.png
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”. ​
image.png
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: . Name the
@Slot
city check”. ​
image.png
When creating a
@Transition Rule
, two
@condition
@Subslot
s are automatically generated. If a condition in a
@Subslot
is true, the Agent will proceed to the Script Branch following this
@condition
@Subslot
.
image.png
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
.
image.png
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.
image.png
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”. ​
image.png
Enter the value lat in the Key field, and the value {{ result[0].lat }} in the Value field. ​
image.png
Add another Key - Value pair by clicking Add new field. ​
image.png
Enter the value lon in the Key field, and the value {{ result[0].lon }} in the Value field. ​
image.png
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
@Platform
: .
In the same
@Slot
we can collect the Endpoint for the second
@External Request
. 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. ​
image.png
Save the
@Slot
.
Next, create an
@External Request
@Slot
named “receive weather” and select the “weather 2
@External Request
in it.
image.png
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”. ​
image.png
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. ​
image.png
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'] }}. ​
image.png
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: . ​
image.png
After the
@Memory
@Slot
, add a
@Text
@Slot
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. ​
image.png
This concludes the creation of the Agent Script. You should now have the following Script:
image.png

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
@BotBuilder
. It will initiate the
@Script Validation
process. If the
@Script
meets the , the
@Agent
will be trained. Otherwise, the
@Script Validation
will fail and an error text will appear in the lower right corner. Read more:
image.png
If errors are detected in the
@Script
, identify problem areas using the guidelines outlined in the article and fix the errors. After this, train the
@Agent
again.
After successful
@Agent
Training, open the
@Debug Widget
by clicking the Debug button. ​
image.png
In the
@Debug Widget
that opens, send a message to the
@Agent
to start the
@Script
. ​
image.png
Communicate with the
@Agent
, going through all the steps of the
@Script
, while checking that there are no errors in the texts and the logic of the
@Script
is correct.
If you find errors, fix them, remembering to retrain the
@Agent
after each change so that it takes effect. If everything is correct, congratulations, your
@Agent
is ready!
Support-Weather-2024-04-15-15-33.cfg
5 kB
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.