![](/img/trans.png)
[英]How do I wait for user reply? (Telegram bot with python-telegram-bot)
[英]How do I get the username of a Telegram user in the main function of a python Telegram bot?
我正在使用库python-telegram-bot
bot 在 python 中编写一个 Telegram 机器人程序,但我遇到了一个问题:我需要获取用户的用户名(抱歉是双关语),但我不知道该怎么做在主要的 function 中。我已经在 inte.net 上搜索过,每个人都使用update.message.from_user.username
在 function 中获取用户名。 但我需要在主 function 中执行此操作,因为我想将此用户名和一些文本发送给另一个 Telegram 用户。 你能帮助我吗?
我当前的代码是这样的:
import telegram
import logging
from telegram.ext import CommandHandler, CallbackQueryHandler, Updater
from functions import start, button
bot = telegram.Bot(token='')
updater = Updater(token='')
dispatcher = updater.dispatcher
logging.basicConfig(format='%(asctime)s - %(name)s'
' - %(levelname)s - %(message)s', level=logging.INFO)
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
updater.dispatcher.add_handler(CallbackQueryHandler(button))
updater.start_polling()
updater.idle()
updater.stop()
看看这是不是你要找的:
def start (update, context):
#this will retrieve the user's username, as you already know
user = update.message.from_user
#this will send the information to some Telegram user
context.bot.send_message(chat_id = some_user_chat_id,
text = f'User {user} just hit start command!')
一旦用户点击/start
命令,他/她的用户名将被发送到您选择的chat_id
。
您可以设置处理来自用户的新更新的中间件并获取用户名 throw ctx.from.username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.