Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance Crypto Price Bot Functionality and User Experience #29

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ def get_top_cryptos(limit=100):
'page': 1,
'sparkline': False
})
return response.json()
if response.status_code == 200:
return response.json()
return []

def get_trending_cryptos():
response = requests.get(f"{COINGECKO_API_URL}/search/trending")
return response.json().get('coins', [])
if response.status_code == 200:
return response.json().get('coins', [])
return []

def get_crypto_details(crypto_id: str, currency: str = 'usd'):
params = {'ids': crypto_id, 'vs_currencies': currency, 'include_24hr_change': 'true', 'include_market_cap': 'true'}
response = requests.get(f"{COINGECKO_API_URL}/simple/price", params=params)
data = response.json()
if crypto_id in data:
return data[crypto_id]
if response.status_code == 200:
data = response.json()
return data.get(crypto_id)
return None

# Command Handlers
Expand Down Expand Up @@ -73,9 +77,9 @@ async def show_crypto_list(update: Update, context: ContextTypes.DEFAULT_TYPE, c
for i in range(0, len(cryptos), 2):
row = []
for crypto in cryptos[i:i+2]:
name = crypto.get('name', crypto['item']['name'] if 'item' in crypto else 'Unknown')
symbol = crypto.get('symbol', crypto['item']['symbol'] if 'item' in crypto else 'Unknown')
crypto_id = crypto.get('id', crypto['item']['id'] if 'item' in crypto else 'unknown')
name = crypto.get('name', 'Unknown')
symbol = crypto.get('symbol', 'Unknown')
crypto_id = crypto.get('id', 'unknown')
row.append(InlineKeyboardButton(f"{name} ({symbol.upper()})", callback_data=f"crypto:{crypto_id}"))
keyboard.append(row)

Expand Down Expand Up @@ -105,10 +109,12 @@ async def button_click(update: Update, context: ContextTypes.DEFAULT_TYPE) -> in
await show_main_menu(update, context)
return MAIN_MENU
elif query.data == 'top100':
await query.edit_message_text("Fetching top cryptocurrencies, please wait...")
cryptos = get_top_cryptos()
await show_crypto_list(update, context, cryptos, "Top 100 Cryptocurrencies:")
return CHOOSING_CRYPTO
elif query.data == 'trending':
await query.edit_message_text("Fetching trending cryptocurrencies, please wait...")
cryptos = get_trending_cryptos()
await show_crypto_list(update, context, cryptos, "Trending Cryptocurrencies:")
return CHOOSING_CRYPTO
Expand Down