diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..c8a0c21 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,75 @@ +pipeline { + agent any + + environment { + BOT_DIR = '/srv/fymious-tg-bot' + VENV_DIR = '/srv/fymious-tg-bot/.venv' + } + + stages { + stage('Checkout') { + steps { + echo 'Pulling latest code...' + checkout scm + } + } + + stage('Setup') { + steps { + echo 'Setting up virtual environment and dependencies...' + sh ''' + # Create bot directory if it doesn't exist + mkdir -p ${BOT_DIR} + + # Copy files to bot directory + cp -r * ${BOT_DIR}/ + + cd ${BOT_DIR} + + # Create virtual environment if it doesn't exist + if [ ! -d "${VENV_DIR}" ]; then + python3 -m venv ${VENV_DIR} + fi + + # Install/update dependencies + ${VENV_DIR}/bin/pip install -r requirements.txt + ''' + } + } + + stage('Deploy') { + steps { + echo 'Restarting bot...' + sh ''' + # Kill existing bot process + pkill -f "python.*bot.py" || true + + # Wait a moment + sleep 2 + + # Start bot in background + cd ${BOT_DIR} + nohup ${VENV_DIR}/bin/python bot.py > ${BOT_DIR}/bot.log 2>&1 & + + # Verify bot started + sleep 3 + if pgrep -f "python.*bot.py" > /dev/null; then + echo "Bot started successfully!" + else + echo "Failed to start bot. Check logs at ${BOT_DIR}/bot.log" + exit 1 + fi + ''' + } + } + } + + post { + success { + echo 'Deployment successful!' + } + failure { + echo 'Deployment failed!' + } + } +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..10a77e1 --- /dev/null +++ b/main.py @@ -0,0 +1,54 @@ +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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..234da17 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +python-telegram-bot==21.0