How can we get the API of Telegram Bot?
Telegram bots are incredibly useful tools that can help us easily carry out various tasks using their API. One of the most interesting uses is that we can easily send online gifts to users! Here, we want to give you a simple explanation with plenty of details on how you can perform this task.
The first step to create a Telegram bot is to visit BotFather in Telegram and create a new bot. BotFather itself is a bot that manages the bots and creates them. You will receive a token that is necessary for identifying your bot.
Now, using this token, you can start sending messages from the pre-defined ones to users. One of the APIs that you can work with is the Telegram Bot API which provides numerous capabilities for interacting with users.
In addition, if you want to send gifts to your users, you can create a small bot that, when the user clicks on it, the bot sends a message as a gift. This way, the user receives a small gift when they click on it. Let's code this operation step by step!
const TelegramBot = require('node-telegram-bot-api');
// Enter your bot token here
const token = 'YOUR_BOT_TOKEN';
const bot = new TelegramBot(token, {polling: true});
bot.onText(/gift/, (msg) => {
const chatId = msg.chat.id;
// Send a gift to the user
bot.sendMessage(chatId, '🎁 This is a gift for you! Have fun!', {
reply_markup: {
inline_keyboard: [[
{ text: 'More gifts!', callback_data: 'moreGifts' }
]]
}
});
});
bot.on('callback_query', (callbackQuery) => {
const chatId = callbackQuery.message.chat.id;
// Send a new gift to the user upon request
bot.sendMessage(chatId, '🎁 This is another gift for you!');
});
Code Explanation Line by Line
const TelegramBot = require('node-telegram-bot-api');
This line imports a library needed to work with the Telegram API.
const token = 'YOUR_BOT_TOKEN';
Here, you need to enter your bot token that you received from BotFather.
const bot = new TelegramBot(token, {polling: true});
This initializes your bot with the token and allows it to receive messages via polling.
bot.onText(/gift/, (msg) => {
This line listens for messages that contain the text /gift, which we will define.
bot.sendMessage(chatId, '🎁 This is a gift for you!', { ... });
Here, a message is sent to the user, which includes a gift icon along with a message and options for receiving more gifts.
bot.on('callback_query', (callbackQuery) => {
This sets up a listener for inline callback queries, allowing the bot to process user requests.
bot.sendMessage(chatId, '🎁 This is another gift for you!');
In this case, a new gift is sent to the user upon clicking.