Introduction to Types of Messages in the Telegram Bot API
Telegram bots are one of the most convenient and attractive tools for interacting with users in this messaging platform. When we talk about bots, one of the interesting points that we should pay attention to is the types of messages that can be sent from the user to the bot. One of these types is MessageOriginHiddenUser
, which we will discuss more about today.
Messages of MessageOriginHiddenUser
refer to messages that are sent as if from users "hidden". In other words, this type of message is usually sent from users in a way that their identity is not specified. This issue could lead to different responses, for instance, when a user sends a message from a channel or group and cannot be accessed by others.
This type of message has wide applications in Telegram. For example, in a large group, it may happen that some users have not completed their profile, and for this reason, this type of message may be used. Bots can respond to these types of messages and display different reactions based on them.
Therefore, this can help us to understand where and how we can utilize these types of messages. In addition to identifying the type of message, bots can also provide more information about users who are hidden in messages. This information can include sent messages, the time they were sent, and other related parameters.
Example Code
const TelegramBot = require('node-telegram-bot-api');
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
const bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
const chatId = msg.chat.id;
if (msg.message_origin == 'hidden_user') {
bot.sendMessage(chatId, 'Your message was successfully received!');
}
});
Code Explanations
Note 1:
const TelegramBot = require('node-telegram-bot-api');
With this line, we can import the necessary library for working with Telegram bots.
Note 2:
const token = 'YOUR_TELEGRAM_BOT_TOKEN';
Here, you should input your bot's token to maintain the connection.
Note 3:
const bot = new TelegramBot(token, {polling: true});
With this line, we initialize the bot and activate polling to receive new messages.
Note 4:
bot.on('message', (msg) => {
With this line, we specify what action should occur whenever a message is sent to the bot.
Note 5:
if (msg.message_origin == 'hidden_user') {
Here, we check whether the received message is of the type
MessageOriginHiddenUser
or not.Note 6:
bot.sendMessage(chatId, 'Your message was successfully received!');
If the previous condition is true, the bot sends a friendly message back to the user. Of course, you can customize the reply message as you like.