Перезагрузить данные запроса для телеграмба питона - PullRequest
0 голосов
/ 01 июля 2018

Я создаю телеграмба по цене, и я сделал это успешно, но проблема в том, что цена не обновлялась, каждый раз, когда я нажимал на кнопку «Пуск», я получал одну и ту же цену, пока она обновлялась на веб-странице API

import requests
import json
import telebot
import locale
import time
from datetime import datetime
locale.setlocale( locale.LC_ALL, '' )




while True : 
    try:
                # base URLs
        tickerURL = "https://api.coinmarketcap.com/v1/ticker/"

    # 

    choice = "bitcoin"
    tickerURL += '/'+choice+'/'
    request = requests.get(tickerURL)
    data = request.json()
    ticker = data[0]['symbol']
    price = data[0]['price_usd']
    price_btc = data[0]['price_btc']
    rank = data[0]['rank']
    market_cap = data[0]['market_cap_usd']
    change_24 = data[0]['24h_volume_usd']
    price_a = ticker + ":\t\t$ " + price
    price_b = ticker + ":\t\tB " + price_btc

    bot_token = '<telegram-token>'

    bot = telebot.TeleBot(bot_token)

    @bot.message_handler(commands=['price', 'help'])
    def send_welcome(message):
        cont = price_a
        cont += '\n'+ price_b
        cont += '\nRank: '+ rank
        cont += '\nChange 24hr: '+ "{:,}".format(float(change_24))
        cont += '\nMarket_cap: ' + "{:,}".format(float(market_cap))
        bot.reply_to(message,cont)
    print('hey..')      
    bot.polling()
except Exception:
    time.sleep(10)

Поэтому я хочу добавить код для частого обновления цены

1 Ответ

0 голосов
/ 01 июля 2018

попробуйте этот фрагмент:

import time
import requests
import telebot


from telebot import TeleBot

app = TeleBot(__name__)

@app.route('price')
def send_welcome(message):
    print('hey..')
    chat_dest = message['chat']['id']

    tickerURL = "https://api.coinmarketcap.com/v1/ticker/"


    choice = "bitcoin"
    tickerURL += '/'+choice+'/'
    request = requests.get(tickerURL)
    data = request.json()
    ticker = data[0]['symbol']
    price = data[0]['price_usd']
    price_btc = data[0]['price_btc']
    rank = data[0]['rank']
    market_cap = data[0]['market_cap_usd']
    change_24 = data[0]['24h_volume_usd']
    price_a = ticker + ":\t\t$ " + price
    price_b = ticker + ":\t\tB " + price_btc

    cont = str(price_a)
    cont += '\n'+ str(price_b)
    cont += '\nRank: '+ str(rank)
    cont += '\nChange 24hr: '+ "{:,}".format(float(change_24))
    cont += '\nMarket_cap: ' + "{:,}".format(float(market_cap)) 


    app.send_message(chat_dest, cont)

bot_token = 'TOKEN'#replace with your token

if __name__ == '__main__':
    app.config['api_key'] = bot_token
    app.poll(debug=True)
...