Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
8
Indexable
Never
from telegram import ReplyKeyboardMarkup, KeyboardButton, Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler
ASK_ID, WAITING_FOR_ID, REM_TRAFFIC = range(3)
# Define the command handler
async def start(update, context):
    # Define the keyboard buttons
    keyboard = [[KeyboardButton('Remaining Traffic')], 
                [KeyboardButton('Remaining Time')],
                [KeyboardButton('testButton')]] #testButton is for testing purposes

    # Create the reply markup
    reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True)

    # Send the message with the keyboard
    await context.bot.send_message(chat_id=update.message.chat_id,text='Welcome!',reply_markup=reply_markup)

# Define the message handler
async def conv(update: Update, context: ContextTypes.DEFAULT_TYPE):
    if(update.message.text == 'Remaining Traffic'):
        return ASK_ID
    elif(update.message.text == 'Remaining Time'):
        await context.bot.send_message(chat_id=update.message.chat_id,text='Remaining Time 0 Days')
    elif(update.message.text == 'testButton'):
        await context.bot.send_message(chat_id=update.message.chat_id,text='test button is working')
    else:
        await context.bot.send_message(chat_id=update.message.chat_id,text="I don't understand you.")

async def askID(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    print('ConversationHandler State ASK_ID working')
    await context.bot.send_message(chat_id=update.message.chat_id,text='enter your id')
    return REM_TRAFFIC

async def remainingTraffic(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    id = update.message.text
    await context.bot.send_message(chat_id=update.message.chat_id,text="here's your id : " + id)
    await context.bot.send_message(chat_id=update.message.chat_id,text='Remaining Traffic 0 GB')
    return ConversationHandler.END
    

async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
    await context.bot.send_message(chat_id=update.message.chat_id,text='Bye! I hope we can talk again some day.')
    return ConversationHandler.END

# Set up the bot
application = ApplicationBuilder().token('token').build()
# Define the handlers and add them to the application
application.add_handler(CommandHandler('start', start))
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), conv))
conversation_handler = ConversationHandler(
    # entry point at starting conv with message handler
    entry_points=[MessageHandler(filters.TEXT & (~filters.COMMAND), conv)],
    states={
        # askID whenever the user sends 'Remaining Traffic'
        ASK_ID: [MessageHandler(filters.TEXT & (~filters.COMMAND), askID)],
        # remainingTraffic whenever the user sends his id
        REM_TRAFFIC: [MessageHandler(filters.TEXT & (~filters.COMMAND), remainingTraffic)],
    },
    fallbacks=[CommandHandler('cancel', cancel)])
application.add_handler(conversation_handler)
# Start the bot
application.run_polling(drop_pending_updates=True)