-
-
Save hiddentribe/d1e24825a7d1d22a5cd705fb25c8fea0 to your computer and use it in GitHub Desktop.
Introduction to bot programming
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """Source: https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot2.py""" | |
| from telegram.ext import Updater, CommandHandler, MessageHandler, Filters | |
| import logging | |
| # Enable logging | |
| logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | |
| level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| # Define a few command handlers. These usually take the two arguments bot and | |
| # update. Error handlers also receive the raised TelegramError object in error. | |
| def start(bot, update): | |
| """Send a message when the command /start is issued.""" | |
| update.message.reply_text('Hi!') | |
| def echo(bot, update): | |
| """Echo the user message.""" | |
| update.message.reply_text(update.message.text) | |
| """Start the bot.""" | |
| # Create the EventHandler and pass it your bot's token. | |
| updater = Updater("TOKEN") | |
| # Get the dispatcher to register handlers | |
| dp = updater.dispatcher | |
| # on different commands - answer in Telegram | |
| dp.add_handler(CommandHandler("start", start)) | |
| # on noncommand i.e message - echo the message on Telegram | |
| dp.add_handler(MessageHandler(Filters.text, echo)) | |
| # Start the Bot | |
| updater.start_polling() | |
| updater.idle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment