Language: English | Русский | Español | Português | Türkçe | Deutsch | Français | Bahasa Indonesia | العربية | فارسی | Oʻzbek | हिन्दी | 中文 | اردو | Tagalog | አማርኛ

← Home

grammY (Node.js): get chat_id and user ID

In grammY (Node.js Telegram Bot framework), you get chat_id and user_id from the context. Here are examples.

From a message

bot.on('message', (ctx) => {
  const chatId = ctx.chat.id;
  const userId = ctx.from?.id;
  return ctx.reply(`Chat ID: ${chatId}, User ID: ${userId}`);
});

From a callback query

bot.on('callback_query:data', (ctx) => {
  const chatId = ctx.chat?.id;
  const userId = ctx.from?.id;
  return ctx.answerCallbackQuery().then(() =>
    ctx.reply(`Chat ID: ${chatId}, User ID: ${userId}`)
  );
});

From channel_post

bot.on('channel_post', (ctx) => {
  const chatId = ctx.chat.id;  // channel ID
  return ctx.reply(`Channel ID: ${chatId}`);
});

Debugging with @print_id_bot

Use @print_id_bot to inspect the raw update:

Forward a message to the bot and send /dump to see the exact format grammY receives.

FAQ

Where is chat_id in ctx?

ctx.chat.id. grammY sets ctx.chat from the update (message.chat, channel_post.chat, etc.).

How do I get chat_id for a new group?

Add your bot to the group. When someone sends a message, your handler receives it with ctx.chat.id. Or add @print_id_bot and send /id.

Is ctx.update the raw Telegram update?

Yes. ctx.update is the full Update object from the Telegram API. Use /dump in @print_id_bot to compare.

What if ctx.from is undefined?

For channel_post, there is no from. Use optional chaining: ctx.from?.id.

Does grammY support TypeScript?

Yes. grammY has first-class TypeScript support. The examples work in both JS and TS.


Use @print_id_bot /dump to inspect update structure for grammY.

Open @print_id_bot


Related pages