-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtelegram_bot.py
More file actions
86 lines (46 loc) · 2.16 KB
/
telegram_bot.py
File metadata and controls
86 lines (46 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import requests
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
import yfinance as yf
import asyncio
# Your Bot Token
TOKEN = "7851301405:AAFFQxBuNstIy_-LeVqE3UvKjQJEYnk7fPM"
# Start Command
async def start(update: Update, context: CallbackContext):
await update.message.reply_text("Welcome to the AI Trading Bot!\nUse /price <symbol> to get live prices.")
# Help Command
async def help_command(update: Update, context: CallbackContext):
await update.message.reply_text("Available commands:\n/start - Start the bot\n/help - Show help message\n/price <symbol> - Get live stock/crypto price")
# Fetch the latest symbol-to-ID mapping from CoinGecko
def get_coin_mapping():
url = "https://api.coingecko.com/api/v3/coins/list"
response = requests.get(url).json()
return {coin["symbol"].upper(): coin["id"] for coin in response}
# Load mapping once at startup
coin_mapping = get_coin_mapping()
async def get_price(update, context):
symbol = context.args[0].upper() # Convert user input to uppercase
coin_id = coin_mapping.get(symbol)
if not coin_id:
await update.message.reply_text("Invalid symbol or data not found.")
return
# Fetch price for the corresponding CoinGecko ID
url = f"https://api.coingecko.com/api/v3/simple/price?ids={coin_id}&vs_currencies=usd"
response = requests.get(url).json()
if coin_id in response:
price = response[coin_id]["usd"]
await update.message.reply_text(f"{symbol} Price: ${price}")
else:
await update.message.reply_text("Price data not available.")
# Main Function to Run the Bot
def main():
app = Application.builder().token(TOKEN).build()
# Add Handlers
app.add_handler(CommandHandler("start", start))
app.add_handler(CommandHandler("help", help_command))
app.add_handler(CommandHandler("price", get_price)) #command for price checking
# Start Bot
print("ULA LALA LEO Now Bot is running...")
app.run_polling()
if __name__ == "__main__":
main()