Question:
Repl link:
import random
from telegram import Update, KeyboardButton, ReplyKeyboardMarkup
from telegram.ext import (
Updater,
CommandHandler,
CallbackContext,
MessageHandler,
Filters,
)
# Replace 'YOUR_TELEGRAM_BOT_TOKEN' with your actual bot token obtained from BotFather
TOKEN = 'Your_Token'
# Handler for the '/start' command
def start(update: Update, context: CallbackContext) -> None:
chat_id = update.message.chat_id
otp_code = generate_otp()
send_otp(chat_id, otp_code)
# Handler for text messages
def handle_text(update: Update, context: CallbackContext) -> None:
chat_id = update.message.chat_id
message_text = update.message.text
if message_text == '/generate':
otp_code = generate_otp()
send_otp(chat_id, otp_code)
# Generate a random 6-digit OTP code
def generate_otp() -> str:
return str(random.randint(100000, 999999))
# Send the generated OTP code to the user
def send_otp(chat_id: int, otp_code: str) -> None:
update.message.reply_text(f"Your OTP code is: {otp_code}")
def main():
updater = Updater(TOKEN, use_context=True)
dispatcher = updater.dispatcher
# Add handlers
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_text))
# Start the bot
updater.start_polling()
# Run the bot until you press Ctrl-C
updater.idle()
if __name__ == '__main__':
main()