feat: add Telegram bot for manual trade commands
- Added telegram_command_bot.py with slash commands (/buySOL, /sellBTC, etc) - Docker compose setup with DNS configuration - Sends trades as plain text to n8n webhook (same format as TradingView) - Improved Telegram success message formatting - Only responds to authorized chat ID (579304651) - Commands: /buySOL, /sellSOL, /buyBTC, /sellBTC, /buyETH, /sellETH
This commit is contained in:
96
telegram_command_bot.py
Normal file
96
telegram_command_bot.py
Normal file
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Telegram Trade Bot - SECURE Command-based
|
||||
Only responds to YOUR commands in YOUR chat
|
||||
"""
|
||||
import os
|
||||
import requests
|
||||
from telegram import Update
|
||||
from telegram.ext import Application, CommandHandler, ContextTypes
|
||||
|
||||
# Configuration
|
||||
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN')
|
||||
N8N_WEBHOOK_URL = os.getenv('N8N_WEBHOOK_URL')
|
||||
ALLOWED_CHAT_ID = int(os.getenv('TELEGRAM_CHAT_ID', '579304651'))
|
||||
|
||||
async def trade_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
"""Handle trade commands like /buySOL, /sellBTC, etc."""
|
||||
|
||||
# Only process from YOUR chat
|
||||
if update.message.chat_id != ALLOWED_CHAT_ID:
|
||||
await update.message.reply_text("❌ Unauthorized")
|
||||
return
|
||||
|
||||
# Extract command (remove the /)
|
||||
command = update.message.text[1:].lower() # e.g., "buysol"
|
||||
|
||||
# Parse action and symbol
|
||||
if command.startswith('buy'):
|
||||
action = 'buy'
|
||||
symbol = command[3:] # e.g., "sol"
|
||||
elif command.startswith('sell'):
|
||||
action = 'sell'
|
||||
symbol = command[4:] # e.g., "btc"
|
||||
else:
|
||||
await update.message.reply_text("❓ Unknown command")
|
||||
return
|
||||
|
||||
message = f"{action} {symbol}"
|
||||
print(f"📨 Command: {message}", flush=True)
|
||||
|
||||
# Forward to n8n webhook - send as plain text body like TradingView does
|
||||
try:
|
||||
print(f"📤 Sending: {message}", flush=True)
|
||||
|
||||
response = requests.post(
|
||||
N8N_WEBHOOK_URL,
|
||||
data=message, # Plain text, not JSON
|
||||
headers={'Content-Type': 'text/plain'},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
print(f"📥 Response status: {response.status_code}", flush=True)
|
||||
print(f"📥 Response body: {response.text[:200]}", flush=True)
|
||||
|
||||
if response.ok:
|
||||
print(f"✅ Sent: {message}", flush=True)
|
||||
await update.message.reply_text(
|
||||
f"🤖 {action.upper()} {symbol.upper()}\n"
|
||||
f"✅ Trade command sent!"
|
||||
)
|
||||
else:
|
||||
print(f"❌ Error: {response.status_code}", flush=True)
|
||||
await update.message.reply_text(f"❌ Error: {response.status_code}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Error: {e}", flush=True)
|
||||
await update.message.reply_text(f"❌ Error: {str(e)}")
|
||||
|
||||
def main():
|
||||
"""Start the bot"""
|
||||
|
||||
print(f"🚀 Telegram Trade Bot Starting...", flush=True)
|
||||
print(f"📱 Allowed Chat ID: {ALLOWED_CHAT_ID}", flush=True)
|
||||
print(f"🔗 Webhook: {N8N_WEBHOOK_URL}", flush=True)
|
||||
print(f"\n✅ Commands:", flush=True)
|
||||
print(f" /buySOL, /sellSOL", flush=True)
|
||||
print(f" /buyBTC, /sellBTC", flush=True)
|
||||
print(f" /buyETH, /sellETH", flush=True)
|
||||
|
||||
# Create application
|
||||
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
||||
|
||||
# Add command handlers
|
||||
application.add_handler(CommandHandler("buySOL", trade_command))
|
||||
application.add_handler(CommandHandler("sellSOL", trade_command))
|
||||
application.add_handler(CommandHandler("buyBTC", trade_command))
|
||||
application.add_handler(CommandHandler("sellBTC", trade_command))
|
||||
application.add_handler(CommandHandler("buyETH", trade_command))
|
||||
application.add_handler(CommandHandler("sellETH", trade_command))
|
||||
|
||||
# Start polling
|
||||
print("\n🤖 Bot ready! Send commands to your Telegram.\n", flush=True)
|
||||
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user