Balram-1 / bot

0 stars 0 forks source link

bot.py #2

Open Balram-1 opened 2 months ago

Balram-1 commented 2 months ago

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

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()

if name == 'main': main()