- 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
80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Simple Telegram bot that forwards trade commands to n8n webhook
|
|
Install: pip3 install python-telegram-bot requests
|
|
Run: python3 telegram_trade_bot.py
|
|
"""
|
|
import os
|
|
import requests
|
|
from telegram import Update
|
|
from telegram.ext import Application, MessageHandler, filters, ContextTypes
|
|
|
|
# Configuration - SET THESE!
|
|
TELEGRAM_BOT_TOKEN = os.getenv('TELEGRAM_BOT_TOKEN', 'YOUR_BOT_TOKEN_HERE')
|
|
N8N_WEBHOOK_URL = os.getenv('N8N_WEBHOOK_URL', 'https://your-n8n.com/webhook/manual-trade')
|
|
ALLOWED_CHAT_ID = int(os.getenv('TELEGRAM_CHAT_ID', '579304651'))
|
|
|
|
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
"""Forward trade commands to n8n"""
|
|
|
|
# Only your chat
|
|
if update.message.chat_id != ALLOWED_CHAT_ID:
|
|
return
|
|
|
|
text = update.message.text.lower().strip()
|
|
|
|
# Only process trade commands: buy/sell followed by symbol
|
|
if not any(text.startswith(cmd) for cmd in ['buy', 'sell', 'long', 'short']):
|
|
return
|
|
|
|
print(f"📨 {text}")
|
|
|
|
# Forward to n8n
|
|
try:
|
|
response = requests.post(
|
|
N8N_WEBHOOK_URL,
|
|
json={'text': text},
|
|
timeout=10
|
|
)
|
|
|
|
if response.ok:
|
|
print(f"✅ Sent to n8n")
|
|
else:
|
|
print(f"❌ Error {response.status_code}")
|
|
await update.message.reply_text(f"❌ Error: {response.status_code}")
|
|
|
|
except Exception as e:
|
|
print(f"❌ {e}")
|
|
await update.message.reply_text(f"❌ Error: {str(e)}")
|
|
|
|
def main():
|
|
"""Start bot"""
|
|
|
|
if TELEGRAM_BOT_TOKEN == 'YOUR_BOT_TOKEN_HERE':
|
|
print("❌ Set TELEGRAM_BOT_TOKEN environment variable")
|
|
print("Example: export TELEGRAM_BOT_TOKEN='your_token'")
|
|
return
|
|
|
|
if N8N_WEBHOOK_URL == 'https://your-n8n.com/webhook/manual-trade':
|
|
print("❌ Set N8N_WEBHOOK_URL environment variable")
|
|
print("Example: export N8N_WEBHOOK_URL='https://n8n.yourdomain.com/webhook/manual-trade'")
|
|
return
|
|
|
|
print(f"🚀 Telegram Trade Bot Starting...")
|
|
print(f"📱 Chat ID: {ALLOWED_CHAT_ID}")
|
|
print(f"🔗 Webhook: {N8N_WEBHOOK_URL}")
|
|
print(f"\n✅ Send messages like:")
|
|
print(f" buy sol")
|
|
print(f" sell btc")
|
|
print(f" buy eth")
|
|
print(f" sell sol")
|
|
|
|
app = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
|
app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
|
|
|
print(f"\n🤖 Bot ready! Listening for trade commands...\n")
|
|
app.run_polling(allowed_updates=Update.ALL_TYPES)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|