1

I have a problem. I'm getting Crypto information from CoinGeckoAPI. Information is not sorted. So I sorted. When I'm running this file with print. It works. But When I would like to reply user's message in Telegram, I have not got the same result. Problem relates to telegram's doc. I'm a new programmer, That's why I need Help.

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()

Result SORTED

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})

Unsorted Result

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 $

Result DOESN'T MAKE SORTED

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


Sabyr
  • 11
  • 2
  • Hi Sabyr, welcome to stackoverflow. I'm not clearly understanding the issue. It says "Result DOESN"T MAKE SORTED", what do you mean by that? What is result and what do you want to sort? Are you sorting by the $ amount or the name of the coin? – lane Jan 29 '22 at 13:59
  • I wanted that my telegram bot send me this result. But instead of it. He sends me only bitcoin. 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})]) – Sabyr Jan 30 '22 at 05:23
  • @lane I made a sorting method and writing result into txt file. So I have question. I can't send the including of this file. – Sabyr Jan 30 '22 at 07:52
  • def collect_data(): s = requests.Session() response = s.get(url="https://api.coingecko.com/api/v3/simple/price?ids=bitcoin%2Cftx-token%2Ccardano%2Cthe-graph%2Cnear%2Cpolkadot%2Cavalanche-2%2Cdogecoin%2Cethereum%2Cterra-luna%2Clitecoin%2Cmatic-network%2Csolana%2Cbinancecoin%2Cuniswap&vs_currencies=usd") data = response.json() result_data = sorted(data.items(), key=lambda i: i[1]['usd'], reverse=True) f = open('file2.txt', 'w') for i in result_data: line = ' '.join(str(x) for x in i) f.write(line + '\n') f.close() – Sabyr Jan 30 '22 at 07:52
  • import requests import json import telebot from pycoingecko import CoinGeckoAPI cg = CoinGeckoAPI() bot = telebot.TeleBot('YOUR TOKEN') @bot.message_handler(commands=['pop']) def bitcoin(message): bot.send_message(message.chat.id, "Please waiting...") collect_data() – Sabyr Jan 30 '22 at 07:53
  • @bot.message_handler(commands=['main']) def pop(message): collect_data() myfile = open('file2.txt', 'r') for x in myfile: a_string = x alpha = "" numeric = "" for character in a_string: if character.isalpha(): alpha += character if character.isdigit(): numeric += character if character == ".": numeric += character new_list = alpha.upper()[0]+ alpha[1:-3]+" ~ "+" "+numeric + " $" bot.send_message(message.chat.id, new_list) – Sabyr Jan 30 '22 at 10:33
  • Glad you were able to sort it out! Sorry I was unable to respond in time. For the benefit of other users can you edit your question and add clarity? Also you can post the solution to your question in the answer section and select your own answer. – lane Jan 30 '22 at 14:04

0 Answers0