How to use the Telegram API to unban a user from a group?
Sometimes, it might be necessary to unban several users from a Telegram group. However, at times, you may want to re-add them to the group. For this action, you can use the unbanChatMember method, which allows you to re-enable users who have previously been banned.
This method is simply a request sent to the Telegram API. You just need to specify chat_id of the group and user_id of the user you wish to free, and you can also specify a reason for unbanning the user; however, this parameter is optional.
To start working with the Telegram API, your first step should be to create a bot and use its token. After receiving this token, you can use the unbanChatMember method with the HTTP request. Here is an example:
By reviewing this information, you'll find a better understanding of how to use this method. In the continuation, we'll examine sample code that can be used to unban a user from the group.
const axios = require('axios');
const token = 'YOUR_BOT_TOKEN';
const chatId = 'CHAT_ID'; // Chat group identifier
const userId = 'USER_ID'; // Identify the user to free
async function unbanUser() {
try {
const response = await axios.post(`https://api.telegram.org/bot${token}/unbanChatMember`, {
chat_id: chatId,
user_id: userId,
});
console.log(response.data);
} catch (error) {
console.error('Error unbanning user:', error);
}
}
unbanUser();
Code Explanation
In this code, we first import the axios
module, which is used for sending HTTP requests.
Then, we specify the bot's token, group identifier, and the user identifier that we wish to unban.
In the unbanUser
function, a POST
request is sent to the Telegram API address, specifying chat_id and user_id.
If the request is successful, the result is printed in the console, and in case of an error, the corresponding error message appears.
In the end, the unbanUser
function is called to perform the unban action.