55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import os
|
|
import logging
|
|
from telegram import Update
|
|
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
|
|
|
# Enable logging
|
|
logging.basicConfig(
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
level=logging.INFO
|
|
)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Bot token from environment variable
|
|
TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
"""Send a message when the command /start is issued."""
|
|
await update.message.reply_text(
|
|
'Hi! I am Fymious Bot. Send me any message and I will echo it back!'
|
|
)
|
|
|
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
"""Send a message when the command /help is issued."""
|
|
await update.message.reply_text(
|
|
'Available commands:\n'
|
|
'/start - Start the bot\n'
|
|
'/help - Show this help message\n'
|
|
'\nJust send me any text and I will echo it back!'
|
|
)
|
|
|
|
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
"""Echo the user message."""
|
|
await update.message.reply_text(f"You said: {update.message.text}")
|
|
|
|
def main():
|
|
"""Start the bot."""
|
|
if not TOKEN:
|
|
logger.error("TELEGRAM_BOT_TOKEN environment variable not set!")
|
|
return
|
|
|
|
# Create the Application
|
|
application = Application.builder().token(TOKEN).build()
|
|
|
|
# Register handlers
|
|
application.add_handler(CommandHandler("start", start))
|
|
application.add_handler(CommandHandler("help", help_command))
|
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
|
|
|
# Start the Bot
|
|
logger.info("Bot is starting...")
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|