I’m working on a Telegram bot project and I need some help. The bot talks to users, and I want to send a summary of their conversations to the admin’s Telegram account. I’m using the node telegram bot api for the backend.
Is there a way to send messages to a specific person using their @username through the Telegram API? I’ve looked through the docs but couldn’t find a clear answer.
Hey there! I’ve worked with Telegram bots before, and I can share some insights on sending messages to specific users.
You’re on the right track, but there’s a slight catch - Telegram doesn’t allow sending messages directly to usernames. Instead, you need to use chat IDs.
Here’s what I’d suggest:
When users interact with your bot, store their chat IDs. You can use a simple object or a database for this. Then, when you want to send a message to the admin, use their stored chat ID.
Here’s a quick example of how you could modify your code:
const adminChatId = 123456789; // Store this when admin first interacts
bot.on('message', (msg) => {
const chatId = msg.chat.id;
const userMessage = msg.text;
// Send summary to admin
bot.sendMessage(adminChatId, `User ${chatId} said: ${userMessage}`);
});
Remember to handle cases where the admin chat ID might not be available. Also, consider using a database for storing chat IDs in a production environment.
Hope this helps with your project! Let me know if you need any clarification.
hey mate, you can’t send msgs directly to usernames. gotta use chat IDs instead. Store the admin’s chat ID when they first interact with ur bot, then use that ID to send msgs. Here’s a quick example:
const adminChatId = 123456789; // Store this when admin first uses bot
bot.sendMessage(adminChatId, `User ${chatId} said: ${userMessage}`);
While it’s not possible to send messages directly to usernames, there’s a workaround. You need to store the chat IDs of users when they first interact with your bot. Here’s how you can modify your code:
This approach stores chat IDs in an object, allowing you to send messages to specific users later. Remember to implement proper error handling and consider using a database for persistent storage in a production environment.