I am writing a bot that gets channel usernames by provided channel ids. It does not get username by id #1966
Answered
by
MKRhere
Kenya-West
asked this question in
Q&A
-
Consider the following code to get channel username by provided id: public startFlow(bot: Telegraf<any>): Promise<void> {
bot.start((ctx) => ctx.reply('Welcome. I am a bot that retrieves usernames by Telegram ID. Send me Telegram ID and I will return the username'));
bot.help((ctx) => ctx.reply('Send me a Telegram ID'));
bot.on(message('text'), (ctx) => {
try {
const id = ctx.message.text;
bot.telegram.getChat(id).then((chat) => {
ctx.reply(`Username: ${(chat.type === 'private' || chat.type === 'channel') ? chat.username : chat.title}`);
}).catch((err) => {
ctx.reply('User not found');
});
} catch (err) {
ctx.reply('Invalid data, ID, or format');
}
});
return Promise.resolve();
} It always fails to get username as it receives Try it yourself with Why does it happen? |
Beta Was this translation helpful? Give feedback.
Answered by
MKRhere
Mar 21, 2024
Replies: 1 comment 2 replies
-
Bots can only call |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Kenya-West
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bots can only call
getChat
on chats they are a member of. Is your bot a member of the channel you are attempting to fetch?