Open Balram-1 opened 2 months ago
import os from telegram import Update from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
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():
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()
import os from flask import Flask, request from telegram import Update, Bot from telegram.ext import Dispatcher, CommandHandler, MessageHandler, Filters, CallbackContext
Get the bot token from the environment variable
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
Initialize the bot and dispatcher
bot = Bot(TOKEN) dispatcher = Dispatcher(bot, None, workers=0)
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)
Register handlers with dispatcher
dispatcher.add_handler(CommandHandler("start", start)) dispatcher.add_handler(CommandHandler("help", help_command)) dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
Create Flask app
app = Flask(name)
@app.route('/webhook', methods=['POST']) def webhook(): update = Update.de_json(request.get_json(force=True), bot) dispatcher.process_update(update) return 'ok'
if name == 'main':
Set webhook with your Render domain