API requests

Basic information about requests

Request is an address from one service to another service to receive or send information.

Requests can be made to any third-party service that supports them. If you can't find the API documentation in the public domain, contact support or the system developers.

Botmother can send requests and accept responses only in JSON format. If the system you need does not support JSON, you need to look for a service that changes the request format, or write a processing script in code on the server. You can also use services that replace integration with requests — for example, Zapier

There are many types of requests, we use only the most common ones: GET, POST, PUT, DELETE.

  • GET is most often used as an information request. In some cases, it can be used equivalently with other types of requests. For example, in Telegram, you can use both GET and POST for the same requests.
  • POST is used to send information and create objects. This method is most often used to create applications, make orders, etc.
  • PUT updates the information about the object.
  • DELETE deletes the created object.

GET request body can be passed in the URL string, for other requests it is possible only in a special component field.

Basic information about responses

Information about an error or about a successful request with or without additional data can be the response to the request.

You can find out more about the answers to request in the article.

Success and error responses are most often received.

If the request was successful, the code will have a value starting with 2:

  • 200 OK — successful request.
  • 201 Created — a new resource was created as a result of successful execution of the request.
  • 202 Accepted — the request was accepted for processing, but it is not completed.

If the request failed due to an error in the request parameters, Botmother considers it successful. This errors starting with 4:

  • 400 Bad Request — the server detected a syntax error in the client request. This code is used for any errors of this type, if the service developer doesn't nest other values in it.
  • 401 Unauthorized — authorization is required to access the requested resource.
  • 403 Forbidden — the server understood the request, but it refuses to execute it due to restrictions in access for the client to the specified resource.
  • 404 Not Found — the requested resource was not found or does not exist.

If the request failed due to a server error (the request fell due to timeout, the server is unavailable or the body could not be parsed), the bot executes the error screen. In all other cases, the success screen will work. If the response came valid with the status which is not 200, you need to install Fork component after the request and look at the status in the body. Based on this, you can direct it to the desired screen. Server errors start with 5:

  • 500 Internal Server Error — it may be any internal server error. This code is used if the developer has not added code to describe this error.
  • 501 Not Implemented — the server cannot process the request. For example, you are using a method that doesn't exist.
  • 502 Bad Gateway — appears when the server to which you are making a request is actually a buffer between you and another server, it receives an incorrect response from another server.
  • 503 Service Unavailable — the server is temporarily unable to process requests. Such errors occur when the server is turned off, does not have access to the Network or it is restricted in access.
  • 504 Gateway Timeout — when the server to which you are making a request is actually a buffer between you and another server, it does not receive a response from the other server.

Sending by the GET method

Creating URL for GET request

We will use requests to the Telegram bot API for the example.

1. Add the bot token to the URL request.

All requests in Telegram are created according to the template:

https://api.telegram.org/bot<token>/METHOD_NAME

When each bot is created it is assigned a unique token of the following type 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

Authorization of requests to Telegram passes through the bot token. This means that the request will be triggered in the bot whose token you specified. For example, when sending a request with a text, you can specify the token of another bot, and then the message will come to it.

So far, the URL will be like this:

 https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/METHOD_NAME

Where instead of 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 is your bot token.

2. Add the API method.

API method is the action that the system is supposed to perform in the specified bot.

One of the simplest methods is SendMessage. It sends a text.

After inserting the API method, our template looks like this:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage

3. Add the request parameters..

Request parameters are mandatory and additional data that specify what action will occur. For example, the parameters of the recipient or the text that will be sent to him.

The required parameters are chat_id and text:

  • chat_id is a unique user ID in Telegram. We get it from the variable {{this_user. platform_id}}. To send a message to a specific user, you need to insert the value of his variable.
  • text is the text of the message that we are sending.

To transfer spaces in the request URL, you need to replace them with + sign. If you send a text with spaces, it will end at the first space.

For GET request, API method and parameters are passed to the request URL. The parameters are separated from API method by a question mark. The parameter value is sent after the equal sign. The parameters are separated from each other by & sign.

4. Form a GET request template.

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage?parameter1=value1parameter2=value2

Instead of parameter1, add chat_id, and output the value1 from the variables {{this_user.platform_id}}.

If necessary, a particular value can be specified in value1.

Instead of parameter2, add text, in value2 write the text that should come to the bot.

The GET request URL is ready:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage?chat_id={{this_user.platform_id}}&text=Hello+from+bot!

Component setup

1. Add Request component to the screen.

2. Select the Request methodGET.

3. Specify the Request URL.

4. Specify the Screen that will be executed when the request is successfully executed.

5. Specify the Screen that will be executed when the request fails.

6. Set the Variable name for the response body if you need to write the response from the server not to last_request, where the response comes by default.

7. Set the Variable name for the response code if you need to write the response from the server not in last_request_status_code, where the response comes by default.

8. If the API requires, add additional request headers in the component drop-down menu. Don't delete the default headers.

9. The Request body for GET requests should be left empty.

10. To use the data for authorization via http basic authentication, check Use authorization box.

11. Check Encode request body box if you need to pass parameters according to the Unicode standard.

Sending by the POST method

Creating URL for POST request

Since in the POST request, the request parameters and their values make up the request body, in the URL string remains the following:

https://api.telegram.org/bot<token>/METHOD_NAME

Where <token> is the bot token where the request will be triggered </token>

Token example: 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11.

Leave the name of the method, as in the previous example — SendMessage.

We get a URL of this type:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendMessage

Do not add parameters in the URL, as in the GET request.

For POST method, only API method is passed to the URL, the parameters are sent in the request body in JSON format.

JSON is one of the data formats. It consists of key-value data pairs. The key is the name of the parameter, the value is the value of the parameter (it can be defined or variable).

To make a body with our parameters, we specify them in JSON format:

{
"chat_id": "{{this_user.platform_id}}",
"text": "Hello from bot!"
}
It is important to mind the syntax. If you forget to put at least one character, the request will not work. This is the most common error when sending requests.
You can wrap strings using \n, but such characters break JSON. To prevent JSON from breaking, you need to escape in this way: /\n. But this will not work if we send a text from the user in the var variable. In this case, you need to use escapeJsonEntities helper in this way: {{escapeJsonEntities var}}.

Component setup

1. Add the Request component to the screen.

2. Select the Request methodPOST.

3. Specify the Request URL.

4. Specify the Screen that will be executed when the request is successfully completed.

5. Specify the Screen that will be executed when the request fails.

6. Set the Variable name for the response body if you need to write the response from the server not to last_request, where the response comes by default.

7. Set the Variable name for the response code if you need to write the response from the server not in last_request_status_code, where the response comes by default.

8. If the API requires, add additional request headers in the component drop-down menu. Don't delete the default headers.

9. The Request body for the POST method is specified if the API requires it. It is supported only in JSON format.

10. To use the data for authorization via http basic authentication, check Use authorization box.

11. Check the Encode request body box if you need to pass parameters according to the Unicode standard.

Deleting a message using a POST request

To test the POST method, you can try to delete the previous message using the deleteMessage method.

Required parameters:

  • chat_id is the unique identifier of the user in the bot. We get it as the main variable {{this_user.platform_id}}.
  • message_id is a unique identifier of the message, located in the variable {{last_telegram_message_id}}.

Create deleteMessage POST request in the builder:

1. Add Request component to the screen.

2. Select the Request methodPOST.

3. Specify the Request URL.

The request path (URL) will look like this:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/deleteMessage

Where 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 is your bot token.

4. Specify the Screen that will be executed when the request is successfully completed.

5. Specify the Screen that will be executed when the request fails.

6. Set the Variable name for the response body if you need to write the response from the server not to last_request, where the response comes by default.

7. Set the Variable name for the response code if you need to write the response from the server not in last_request_status_code, where the response comes by default.

8. If the API requires, add additional request headers in the component drop-down menu. Don't delete the default headers.

9. Add the Request body. The body is supported only in JSON format.

{
"chat_id": "{{this_user.platform_id}}",
"message_id": "{{last_telegram_message_id}}"
}

10. To use data for authorization via http basic authentication, check the Use authorization box.

11. Check the Encode request body box if you need to pass parameters according to the Unicode standard.

Now the last message will be deleted from the dialogue with the bot in the messenger, if you set such a request in the builder immediately after the message.

Send a sticker using a POST request

Let’s send a sticker to the chat using the sendSticker method.

Required parameters:

  • chat_id is the unique identifier of the user in the bot. We get it as the main variable {{this_user.platform_id}}.
  • sticker is the unique identifier of the sticker. You can find out the sticker ID by sending a sticker to bot Sticker ID.

Let’s create a sendSticker POST request in the builder:

1. Add Request component to the screen.

2. Select the Request methodPOST.

3. Specify the Request URL.

The request path (URL) will look like this:

https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/sendSticker

Where 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11 — is your bot token.

4. Specify the Screen that will be executed when the request is successfully completed.

5. Specify the Screen that will be executed when the request fails.

6. Set the Variable name for the response body if you need to write the response from the server not to last_request, where the response comes by default.

7. Set the Variable name for the response code if you need to write the response from the server not in last_request_status_code, where the response comes by default.

8. If the API requires, add additional request headers in the component drop-down menu. Don't delete the default headers.

9. Add the Request body. The body is supported only in JSON format.

{
"chat_id": "{{this_user.platform_id}}",
"sticker": "12345"
}

Where 12345 is the ID of the sticker being sent.

10. To use the data for authorization via http basic authentication, check the Use authorization box.

11. Check Encode request body box if you need to pass parameters according to the Unicode standard.

If everything is done correctly, when executing the Request, a sticker will come to the bot.

Response Processing — Object

For example, we will use the resource https://www.cbr-xml-daily.ru.

We select a request in JSON format in it:

https://www.cbr-xml-daily.ru/daily_json.js

We open it as a regular link in the browser and get a response in JSON format.

To make the answer easy to read, install the extension on the browser. For Google Chrome, you can use JSON Formatter.

For example, we want to know the cost of 1 Belarusian ruble. We find the part where BYN value is output and compose a variable

The response to the request is located in {{last_request}} object.

To output a variable inside the text, input it inside curly brackets.