updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help_command))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT, SIGTERM or SIGABRT
updater.idle()
import os from telegram import Update from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
Get the bot token from the environment variable
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
Define command handlers
def start(update: Update, context: CallbackContext) -> None: update.message.reply_text('Hi! I am your bot. How can I help you?')
def help_command(update: Update, context: CallbackContext) -> None: update.message.reply_text('Help!')
def echo(update: Update, context: CallbackContext) -> None: update.message.reply_text(update.message.text)
def main():
Create the Updater and pass it your bot's token
if name == 'main': main()