Stopping Polls in Telegram Bot
If you have a Telegram bot and are using polls in it, you may want to stop a poll at some points. For this purpose, Telegram provides a user API called stopPoll
. By using this API, you can stop active polls and, if necessary, display their results.
The stopPoll
function allows you to end polls whenever you want. For example, suppose you have a poll about selecting the best movie and want to stop it after a specific period. In this case, using stopPoll
is an appropriate solution.
Don't forget that in order to use this API, you need to know the chat identifier (chat_id
) and the poll identifier (message_id
). This information allows you to specify which poll you want to stop and view its results.
Now, let’s take a look at a sample code. We will use stopPoll
with the related information like this:
const chatId = 'CHAT_ID';
const messageId = 'MESSAGE_ID';
const token = 'YOUR_BOT_TOKEN';
fetch(`https://api.telegram.org/bot${token}/stopPoll?chat_id=${chatId}&message_id=${messageId}`)
.then(response => response.json())
.then(data => console.log(data));
Code Explanation
const chatId = 'CHAT_ID';
This line specifies the chat identifier (chat_id). This identifier must match the chat that the poll was created in.
const messageId = 'MESSAGE_ID';
Here, we specify the message identifier (message_id) of the poll that we want to stop.
const token = 'YOUR_BOT_TOKEN';
In this line, you will enter your Telegram bot token. This token allows your bot to communicate with the Telegram API.
fetch(`https://api.telegram.org/bot${token}/stopPoll?chat_id=${chatId}&message_id=${messageId}`)
In this line, we use the fetch
function to send a request to the Telegram API. We create the URL with chat_id
and message_id
.
.then(response => response.json())
After sending the request, using then
, we convert the received response to JSON format.
.then(data => console.log(data));
Finally, we display the result in the console. This result will include various information such as the status of the poll and its results.