Get Telegram User Profiles with grammY: ID, Status and Photo URL

Telegram exposes user and chat APIs through grammY. This example uses getUserProfilePhotos, getFile, and getChatMember.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { Bot } from "https://deno.land/x/grammy@v1.25.0/mod.ts";

const TOKEN = '';
const bot = new Bot(TOKEN);

bot.command('start', async ctx => {
  const userId = ctx.from.id;
  let photoUrl = '';

  try {
    const photos = await bot.api.getUserProfilePhotos(userId, { limit: 1 });
    if (photos.total_count > 0) {
      const file = await bot.api.getFile(photos.photos[0][0].file_id);
      photoUrl = `https://api.telegram.org/file/bot${TOKEN}/${file.file_path}`;
    }
  } catch (error) {
    console.error('Could not load profile photo:', error);
  }

  try {
    const member = await bot.api.getChatMember(ctx.chat.id, userId);
    const info = [
      `ID: ${member.user.id}`,
      `Name: ${member.user.first_name}`,
      `Username: ${member.user.username || 'not set'}`,
      `Status: ${member.status}`,
    ].join('\n');
    if (photoUrl) await ctx.reply(`Profile photo: ${photoUrl}`);
    await ctx.reply(info);
  } catch (error) {
    console.error('Could not load user information:', error);
  }
});

bot.start();

User information response

Avoid exposing the bot token in client-side code or logs; a profile-file URL constructed with the token is sensitive.