Telegram Bot - 如何获取用户 ID

问题描述 投票:0回答:1

我正在寻求开发一个类似于@rawdatabot的机器人,它在与其共享时提供用户ID。

Example 1 Example 2

但是,我不确定这样的机器人如何检索与您互动过的用户的信息,特别是如果该人在设置中禁用了转发消息、电话号码、用户名的功能。我在API文档中找不到任何相关细节。

我尝试通过共享联系人获取用户ID,但没有成功。由于用户的隐私设置,似乎不允许这样做。 我希望机器人能够向我提供用户 ID,即使该人已在其设置中启用了所有隐私功能。我知道这是可能的,因为@rawdatabot。

P.S 开发人员使用带有 TDL 库的打字稿

typescript bots telegram python-telegram-bot tdl
1个回答
0
投票

user_id = update.effective_user.id
:提取用户ID,发送/start时在此处


from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes

TELEGRAM_BOT_TOKEN = 'Your_Token'

async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:

    #Get User ID
    user_id = update.effective_user.id

    #Send UserID to the User when he send /start
    await context.bot.send_message(chat_id=update.effective_chat.id, text=f"Your User ID: {user_id}")

def main() -> None:
    application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
    application.add_handler(CommandHandler("start", start))
    application.run_polling()

if __name__ == '__main__':
    main()
© www.soinside.com 2019 - 2024. All rights reserved.