Types of ChatMemberRestricted in the Telegram Bot API

telegram bot api chatmemberrestricted
08 January 2025

Introduction to the ChatMemberRestricted Type in the Telegram Bot API

When talking about Telegram bots, the API of these bots serves as a powerful tool for communication and interaction with users and groups. One of the important concepts in this API is the ChatMember type, which indicates what type of member a user is in a group or chat. One of its variants is ChatMemberRestricted. This type provides us with complete information about the access status of a user in a group.

In fact, ChatMemberRestricted occurs when a user is temporarily restricted from performing certain activities in a group. In other words, this type can indicate that a user has limited access to the chat and cannot, as long as their restrictions are not lifted, access certain group capabilities.

From the perspective of group management in Telegram bots, having precise information about group members and their statuses is essential for implementing restrictions or better group management. By using the Telegram bot API, you can receive information such as history and the reasons for a user's restrictions.

In continuation, we provide a code example to see how we can use the ChatMemberRestricted type in the Telegram API. This code makes a request to retrieve the status of a specific user in a chat and checks whether this user is of type ChatMemberRestricted or not.

Code Example

const TelegramBot = require('node-telegram-bot-api');

// Place your Telegram bot token here
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const bot = new TelegramBot(token);

const chatId = 'CHAT_ID'; // Identifier for the relevant chat
const userId = 'USER_ID'; // Identifier for the relevant user

bot.getChatMember(chatId, userId).then((member) => {
if (member.status === 'restricted') {
console.log('This user is restricted.');
} else {
console.log('This user is not restricted.');
}
}).catch((error) => {
console.error('Error retrieving user status:', error);
});

Code Explanation

const TelegramBot = require('node-telegram-bot-api');
This line imports the node-telegram-bot-api library, which allows us to work with the Telegram bot API.

const token = 'YOUR_TELEGRAM_BOT_TOKEN';
In this line, you must place your bot token to access the API.

const chatId = 'CHAT_ID'; // Identifier for the relevant chat
Here, you need to specify the identifier for the chat whose user status you want to check.

const userId = 'USER_ID'; // Identifier for the relevant user
In this line, you need to specify the identifier for the user whose status you want to check.

bot.getChatMember(chatId, userId).then((member) => {…
This line calls a method that fetches the user information from the relevant chat and stores the results in the variable member.

if (member.status === 'restricted') {…
This section checks whether the user has been classified as ChatMemberRestricted or not, and displays the appropriate message.

.catch((error) => {…
In conclusion, any errors in fetching the user's information will be handled here, preventing issues from breaking the workflow.

FAQ

?

Where can I find more accurate information about ChatMemberRestricted?

?

Can I change user permissions?

?

How can I determine if a user has been restricted in a group?