How can I find out the user id by his username? I use the aiogram library and I have not found information on the Internet how to do this. Is there any method?
import logging
from configparser import ConfigParser
from aiogram import Bot, Dispatcher, executor, types
logging.basicConfig(level=logging.INFO)
config = ConfigParser()
config.read('config.ini')
TOKEN = config.get('BOT', 'Token')
bot = Bot(token=TOKEN)
dp = Dispatcher(bot)
@dp.message_handler(commands=['start'])
async def start(message: types.Message):
text = config.get('BOT_TEXT', 'Start')
await bot.send_message(message.chat.id, text)
@dp.message_handler()
async def forward(message: types.Message):
await bot.send_message(message.forward_from.id, 'Kuku')
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
2
Answers
You will receive the from_id
Don’t forget to install fsm
You could create a ReplyKeyboardMarkup with a KeyboardButtonRequestChat
After that just send the contact over to your bot using the ReplyKeyboardMarkup you’ve created, handle the shared chat & fetch the user_id.
As far as I remember it should be something like this:
⚠️ Caution, I didn’t test the code, but as far as I remember, it should work like this in aiogram 2.*