How to send messages to specific users with Telegram Bot API?

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.

Here’s a basic example of what I’m trying to do:

const TelegramBot = require('node-telegram-bot-api');
const bot = new TelegramBot('YOUR_BOT_TOKEN', {polling: true});

bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  const adminUsername = '@admin123';

  // Process user message
  const userMessage = msg.text;
  
  // Send summary to admin
  bot.sendMessage(adminUsername, `User ${chatId} said: ${userMessage}`);
});

This code doesn’t work, of course. How can I modify it to send messages to the admin by username? Any help would be great!

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}`);

hope this helps!

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:

const userChatIds = {};

bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  const username = msg.from.username;

  userChatIds[username] = chatId;

  const userMessage = msg.text;
  const adminChatId = userChatIds['admin123'];

  if (adminChatId) {
    bot.sendMessage(adminChatId, `User ${chatId} said: ${userMessage}`);
  } else {
    console.log('Admin chat ID not found.');
  }
});

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.