I’m developing a Python Telegram Bot that monitors Bitcoin prices by fetching data from the CoinMarketCap API. The current script periodically retrieves BTC pricing and sends notifications when the price falls below a preset limit. However, I haven’t implemented a /start command yet, which prevents the bot from being activated properly within a Telegram group chat. I’m looking for assistance on how to integrate a /start command into the bot so that users can initialize it. Below is my revised code snippet for reference:
import requests
from telegram.ext import Updater, CommandHandler
COIN_API_TOKEN = 'your_coin_api_token'
TELEGRAM_BOT_KEY = 'your_telegram_bot_key'
PRICE_THRESHOLD = 30000
def retrieve_bitcoin_value():
api_url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
hdrs = {
'Accept': 'application/json',
'X-CMC_PRO_API_KEY': COIN_API_TOKEN
}
resp = requests.get(api_url, headers=hdrs)
json_data = resp.json()
btc_value = json_data['data'][0]['quote']['USD']['price']
return btc_value
def activate_bot(update, context):
update.message.reply_text('Bot activated! Now tracking BTC price...')
current_value = retrieve_bitcoin_value()
if current_value < PRICE_THRESHOLD:
update.message.reply_text(f'Warning: BTC has dropped to {current_value} USD.')
def run_bot():
updater = Updater(TELEGRAM_BOT_KEY, use_context=True)
dispatcher = updater.dispatcher
dispatcher.add_handler(CommandHandler('start', activate_bot))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
run_bot()