1

我有个问题。我从 CoinGeckoAPI 获取加密信息。信息未排序。于是我整理了一下。当我用 print 运行这个文件时。有用。但是当我想在 Telegram 中回复用户的消息时,我没有得到相同的结果。问题与电报的文档有关。我是一名新程序员,这就是我需要帮助的原因。

from pycoingecko import CoinGeckoAPI
from collections import OrderedDict
import telebot
bot = telebot.TeleBot('YOUR TOKEN')
cg = CoinGeckoAPI()


price = cg.get_price(ids=['bitcoin', 'ftx-token', 'cardano', 'the-graph', 'near', 'polkadot', 'avalanche-2', 'dogecoin', 'ethereum',
         'terra-luna', 'litecoin', 'matic-network', 'solana', 'binancecoin', 'uniswap'], vs_currencies='usd')
temp = price
temp = OrderedDict(sorted(temp.items(), key=lambda i: i[1]['usd'], reverse=True))
print(temp)


bot.polling()

结果排序

OrderedDict([('bitcoin', {'usd': 37859}), ('ethereum', {'usd': 2572.28}), ('binancecoin', {'usd': 392.59}), ('litecoin', {'usd': 110.22}), ('solana', {'usd': 96.54}), ('avalanche-2', {'usd': 70.16}), ('terra-luna', {'usd': 52.13}), ('ftx-token', {'usd': 40.97}), ('polkadot', {'usd': 18.69}), ('near', {'usd': 11.25}), ('uniswap', {'usd': 11.07}), ('matic-network', {'usd': 1.72}), ('cardano', {'usd': 1.07}), ('the-graph', {'usd': 0.419375}), ('dogecoin', {'usd': 0.142963})])

@bot.message_handler(commands=['main'])
def main(message):
    price = cg.get_price(ids=['bitcoin', 'ftx-token', 'cardano', 'the-graph', 'near', 'polkadot', 'avalanche-2', 'dogecoin', 'ethereum', 'terra-luna', 'litecoin', 'matic-network', 'solana', 'binancecoin', 'uniswap'], vs_currencies='usd')
    temp = price
    temp = OrderedDict(sorted(temp.items(), key=lambda i: i[1]['usd'], reverse=True))
    bot.send_message(message.chat.id, f'Crypto:\n'
                                      f'Bitcoin ~  {price["bitcoin"]["usd"]} $\n'
                                      f'Ethereum ~ {price["ethereum"]["usd"]} $\n'
                                      f'Near Protocol ~ {price["near"]["usd"]} $\n'
                                      f'FTX Token ~ {price["ftx-token"]["usd"]} $\n'
                                      f'Litecoin ~ {price["litecoin"]["usd"]} $\n'
                                      f'Terra ~ {price["terra-luna"]["usd"]} $\n'
                                      f'Solana ~ {price["solana"]["usd"]} $\n'
                                      f'Polkadot ~ {price["polkadot"]["usd"]} $\n'
                                      f'Binance Coin ~ {price["binancecoin"]["usd"]} $\n'
                                      f'Dogecoin ~ {price["dogecoin"]["usd"]} $\n'
                                      f'Cardano ~ {price["cardano"]["usd"]} $\n'
                                      f'The Graph ~ {price["the-graph"]["usd"]} $\n'
                                      f'Polygon ~ {price["matic-network"]["usd"]} $\n'
                                      f'Avalanche ~ {price["avalanche-2"]["usd"]} $\n'
                                      f'Uniswap ~ {price["uniswap"]["usd"]} $')

    bot.send_message(message.chat.id, temp{temp})

未排序的结果

Sabyr's crypto bot, [29.01.2022 19:24]
Crypto:
Bitcoin ~  37840 $
Ethereum ~ 2571.11 $
Near Protocol ~ 11.23 $
FTX Token ~ 40.98 $
Litecoin ~ 110.24 $
Terra ~ 52.21 $
Solana ~ 96.53 $
Polkadot ~ 18.71 $
Binance Coin ~ 392.64 $
Dogecoin ~ 0.143045 $
Cardano ~ 1.07 $
The Graph ~ 0.419587 $
Polygon ~ 1.72 $
Avalanche ~ 70.17 $
Uniswap ~ 11.08 $

结果未排序

Sabyr's crypto bot, [29.01.2022 19:24]
bitcoin


4

0 回答 0