Advertisement
Guest User

Untitled

a guest
Mar 11th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. import logging
  2. from telegram import Update
  3. from telegram.ext import Application, PicklePersistence, CommandHandler, ConversationHandler, ContextTypes, MessageHandler, filters
  4.  
  5. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  6. level=logging.INFO)
  7. logger = logging.getLogger(__name__)
  8.  
  9. CHOOSING, TYPING_REPLY = range(2)
  10.  
  11. async def timeout(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
  12. logger.info('#### in timeout')
  13. await update.message.reply_text('Sorry, you took too long to respond. Moving on...')
  14. return ConversationHandler.END
  15.  
  16. async def ended(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
  17. logger.info('#### in ended')
  18. await update.message.reply_text('End of conversation...')
  19. return ConversationHandler.END
  20.  
  21. async def waiting(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
  22. logger.info('#### in waiting')
  23. await update.message.reply_text('Still waiting...')
  24. return ConversationHandler.END
  25.  
  26. async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
  27. await update.message.reply_text('Hi! Please type something, or you will time out in 30 seconds.')
  28. return CHOOSING
  29.  
  30. async def received_information(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int:
  31. user_data = context.user_data
  32. text = update.message.text
  33. user_data['choice'] = text
  34. await update.message.reply_text("Got it! You said: " + text)
  35. return ConversationHandler.END
  36.  
  37. def main():
  38. persistence = PicklePersistence(filepath='bot.data.pickle')
  39. application = Application.builder().token("6185855471:AAF8vwkGXVVQZs3YddT75QVuaHggfyLQHJ8").persistence(persistence).build()
  40. conv_handler = ConversationHandler(
  41. entry_points=[CommandHandler('start', start)],
  42. states={
  43. CHOOSING: [MessageHandler(filters.TEXT & ~filters.COMMAND, received_information)],
  44. ConversationHandler.TIMEOUT: [MessageHandler(filters.TEXT & ~filters.COMMAND, timeout)],
  45. ConversationHandler.WAITING: [MessageHandler(filters.TEXT & ~filters.COMMAND, waiting)],
  46. ConversationHandler.END: [MessageHandler(filters.TEXT & ~filters.COMMAND, ended)]
  47. },
  48. fallbacks=[],
  49. conversation_timeout=10.0,
  50. )
  51. application.add_handler(conv_handler)
  52. application.run_polling()
  53.  
  54. if __name__ == '__main__':
  55. main()
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement