首页 > 解决方案 > python上的价格令牌电报机器人

问题描述

我对使用 coinmarketcap API 的电报机器人中的加密价格有疑问

机器人工作正常,但电报上的价格有太多零:coin=[7.68321846e-06]$,实际价格为 0.00000768$

我该如何修改这个?提前致谢!

# global variables
api_key = '-----'
bot_token = '----'
chat_id = '-----'
market_symbol = 'coin'
currency_iso = 'USD'
time_interval = 5 * 60 # in seconds
def get_coin_price():
    url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest'
    query = '?symbol=' + market_symbol + '&convert=' + currency_iso
    headers = {
        'Accepts': 'application/json',
        'X-CMC_PRO_API_KEY': api_key
    }


    # make a request to the coinmarketcap api
    response = requests.get(url + query, headers=headers)
    response_json = response.json()

# extract the coin price from the json data
    coin_price = response_json['data'][market_symbol]

    return coin_price['quote'][currency_iso]['price']
# fn to send_message through telegram
def send_message(chat_id, msg):
    url = f"https://api.telegram.org/bot{bot_token}/sendMessage?chat_id={chat_id}&text=coin={msg}$"

标签: pythonbotstelegram

解决方案


推荐阅读