i can’t tag other chat users(except administrators)
and i just couldn’t do it.
i decided to leave a function for tag admins.
for (let i = 0; i < userCount; i++) {
const member = await ctx.telegram.getChatMember(chatId, i + 1);
const user = member.user;
i’ve tried doing exact same method for a tag users
but im have error
Error in receiving users: TelegramError: 400: Bad Request: PARTICIPANT_ID_INVALID
and next
response: {
ok: false,
error_code: 400,
description: 'Bad Request: PARTICIPANT_ID_INVALID'
},
on: {
method: 'getChatMember',
payload: { chat_id: -1234567890, user_id: 1 }
}
}
and my full code
import dotenv from 'dotenv';
dotenv.config();
const botToken = process.env.TELEGRAM_BOT_TOKEN;
import { Telegraf } from 'telegraf';
const bot = new Telegraf(botToken);
bot.command('all', async (ctx) => {
try {
const chatId = ctx.chat.id;
const userCount = await ctx.telegram.getChatMembersCount(chatId);
console.log(`users count: ${userCount}`);
let message = '';
const tagLimit = 5;
let chunk = [];
const admins = await ctx.telegram.getChatAdministrators(chatId);
for (let i = 0; i < admins.length; i++) {
const user = admins[i].user;
if (user.username) {
chunk.push(`@${user.username}`);
} else {
chunk.push(`@${user.id}`);
}
if (chunk.length === tagLimit) {
message += chunk.join('n') + 'n';
chunk = [];
}
}
if (chunk.length > 0) {
message += chunk.join('n');
}
console.log('tags users:n' + message.trim());
await ctx.reply(message.trim() || 'users not have @usernames');
} catch (error) {
console.error('an error when receiving users:', error);
ctx.reply('an error when receiving users_list');
}
});
bot.launch();
console.log('bot is started');