南锋

南奔万里空,脱死锋镝余

Retrieve User Information from Telegram

Following the previous article, after creating a Telegram bot, we began developing a mini game or mini app, which inevitably involves a login feature. This means we need to access user information.

Reference Guide Reference Guide, Telegram provides many APIs, and to retrieve user information, you simply need to call the appropriate API.

Retrieve User Information

Here, the main information retrieved is the user’s avatar, ID, username, name, and status. We use two APIs, getChatMember and getUserProfilePhotos.
Usage examples:
Retrieve User Avatar:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Initialize avatar URL as an empty string
let photoUrl = '';

try {
// Get user avatar information
const profilePhotos = await bot.api.getUserProfilePhotos(userId, { limit: 1 });

if (profilePhotos.total_count > 0) {
const fileId = profilePhotos.photos[0][0].file_id;
const file = await bot.api.getFile(fileId);
photoUrl = `https://api.telegram.org/file/bot${TOKEN}/${file.file_path}`;
}
} catch (error) {
console.error("Failed to retrieve avatar: ", error);
}

Retrieve User Login Information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  let userInfo = '';
let id = "";
let name = "";
try {
const chatMember = await bot.api.getChatMember(ctx.chat.id, userId);
id = chatMember.user.id;
name = chatMember.user.first_name;
userInfo = `User Information:\nID: ${chatMember.user.id}\nName: ${chatMember.user.first_name}\nUsername: ${chatMember.user.username}\nStatus: ${chatMember.status}`;
} catch (error) {
console.error("Failed to retrieve user information: ", error);
}

if (photoUrl) {
await ctx.reply(`Avatar URL: ${photoUrl}`);
} else {
await ctx.reply("Failed to retrieve your avatar.");
}

await ctx.reply(userInfo || "Failed to retrieve your user information.");

Complete Code

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Bot, InlineKeyboard } from "https://deno.land/x/grammy@v1.25.0/mod.ts";

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

// Handle the /start command
bot.command("start", async (ctx) => {
const firstName = ctx.update.message.from.first_name;
const userId = ctx.from.id;

// Initialize avatar URL as an empty string
let photoUrl = '';

try {
// Get user avatar information
const profilePhotos = await bot.api.getUserProfilePhotos(userId, { limit: 1 });

if (profilePhotos.total_count > 0) {
const fileId = profilePhotos.photos[0][0].file_id;
const file = await bot.api.getFile(fileId);
photoUrl = `https://api.telegram.org/file/bot${TOKEN}/${file.file_path}`;
}
} catch (error) {
console.error("Failed to retrieve avatar: ", error);
}

// Retrieve user login information
let userInfo = '';
let id = "";
let name = "";
try {
const chatMember = await bot.api.getChatMember(ctx.chat.id, userId);
id = chatMember.user.id;
name = chatMember.user.first_name;
userInfo = `User Information:\nID: ${chatMember.user.id}\nName: ${chatMember.user.first_name}\nUsername: ${chatMember.user.username}\nStatus: ${chatMember.status}`;
} catch (error) {
console.error("Failed to retrieve user information: ", error);
}

if (photoUrl) {
await ctx.reply(`Avatar URL: ${photoUrl}`);
} else {
await ctx.reply("Failed to retrieve your avatar.");
}

await ctx.reply(userInfo || "Failed to retrieve your user information.");
});

// Start the bot
bot.start();

Run the Code and Observe the Output

Execute the following command to run the code:

1
deno run --allow-net your-script.ts

Go back to the conversation with your bot and send /start. You will see the bot returning the user login information and avatar, as shown below:
对话框;

+