Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import logging
  2.  
  3. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  4.  
  5. # Enable logging
  6. logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
  7. level=logging.INFO)
  8.  
  9. logger = logging.getLogger(__name__)
  10.  
  11.  
  12. # Define a few command handlers. These usually take the two arguments bot and
  13. # update. Error handlers also receive the raised TelegramError object in error.
  14. def start(update, context):
  15. """Send a message when the command /start is issued."""
  16. update.message.reply_text('Hi!')
  17.  
  18.  
  19. def help(update, context):
  20. """Send a message when the command /help is issued."""
  21. update.message.reply_text('Help!')
  22.  
  23.  
  24. def echo(update, context):
  25. """Echo the user message."""
  26. update.message.reply_text(update.message.text)
  27.  
  28.  
  29. def error(update, context):
  30. """Log Errors caused by Updates."""
  31. logger.warning('Update "%s" caused error "%s"', update, context.error)
  32.  
  33.  
  34. def main():
  35. """Start the bot."""
  36. # Create the Updater and pass it your bot's token.
  37. # Make sure to set use_context=True to use the new context based callbacks
  38. # Post version 12 this will no longer be necessary
  39. updater = Updater("TOKEN", use_context=True)
  40.  
  41. # Get the dispatcher to register handlers
  42. dp = updater.dispatcher
  43.  
  44. # on different commands - answer in Telegram
  45. dp.add_handler(CommandHandler("start", start))
  46. dp.add_handler(CommandHandler("help", help))
  47.  
  48. # on noncommand i.e message - echo the message on Telegram
  49. dp.add_handler(MessageHandler(Filters.text, echo))
  50.  
  51. # log all errors
  52. dp.add_error_handler(error)
  53.  
  54. # Start the Bot
  55. updater.start_polling()
  56.  
  57. # Run the bot until you press Ctrl-C or the process receives SIGINT,
  58. # SIGTERM or SIGABRT. This should be used most of the time, since
  59. # start_polling() is non-blocking and will stop the bot gracefully.
  60. updater.idle()
  61.  
  62.  
  63. if __name__ == '__main__':
  64. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement