Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- from telegram import Update
- from telegram.ext import Application, PicklePersistence, CommandHandler, ConversationHandler, ContextTypes, MessageHandler, filters
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
- level=logging.INFO)
- logger = logging.getLogger(__name__)
- CHOOSING, TYPING_REPLY = range(2)
- async def timeout(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
- logger.info('#### in timeout')
- await update.message.reply_text('Sorry, you took too long to respond. Moving on...')
- return ConversationHandler.END
- async def ended(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
- logger.info('#### in ended')
- await update.message.reply_text('End of conversation...')
- return ConversationHandler.END
- async def waiting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
- logger.info('#### in waiting')
- await update.message.reply_text('Still waiting...')
- return ConversationHandler.END
- async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
- await update.message.reply_text('Hi! Please type something, or you will time out in 30 seconds.')
- return CHOOSING
- async def received_information(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
- user_data = context.user_data
- text = update.message.text
- user_data['choice'] = text
- await update.message.reply_text("Got it! You said: " + text)
- return ConversationHandler.END
- def main():
- persistence = PicklePersistence(filepath='bot.data.pickle')
- application = Application.builder().token("6185855471:AAF8vwkGXVVQZs3YddT75QVuaHggfyLQHJ8").persistence(persistence).build()
- conv_handler = ConversationHandler(
- entry_points=[CommandHandler('start', start)],
- states={
- CHOOSING: [MessageHandler(filters.TEXT & ~filters.COMMAND, received_information)],
- ConversationHandler.TIMEOUT: [MessageHandler(filters.TEXT & ~filters.COMMAND, timeout)],
- ConversationHandler.WAITING: [MessageHandler(filters.TEXT & ~filters.COMMAND, waiting)],
- ConversationHandler.END: [MessageHandler(filters.TEXT & ~filters.COMMAND, ended)]
- },
- fallbacks=[],
- conversation_timeout=10.0,
- )
- application.add_handler(conv_handler)
- application.run_polling()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement