【发布时间】:2021-05-24 11:45:27
【问题描述】:
我正在尝试为我的电报组制作一个非常简单的价格机器人。
令牌的数据取自 API。数据被正确提取,没有任何问题。但是,当我打开机器人并调用价格时,它只显示第一个价格,因为它会请求一次,之后不再循环。
有没有办法在机器人的函数中构建 API 调用,以便当我调用 /price 命令时,它会在那个时候从 API 中提取它?
我试过了:将 API 调用放在 bot 消息函数中,但它不能正常工作。
有没有办法让它工作?
在我的代码下面:
import os
import telebot
from requests_html import HTMLSession
import json
import urllib.request
from urllib.request import urlopen
import ssl
#Creating the bot with the attached API
API_KEY = ''
bot = telebot.TeleBot(API_KEY)
#Token price scraping
ssl._create_default_https_context = ssl._create_unverified_context
url = ''
data = urllib.request.urlopen(url).read()
jsonn = json.loads(data)
price_token = jsonn[-1][1]
print(price_token)
#Token price bot commands
@bot.message_handler(commands=['price'])
def pricetoken(message):
bot.send_message(message.chat.id,price_token)
bot.polling(none_stop=True, timeout=200)
【问题讨论】:
-
嗨。请删除
python-telegram-bot标记,它用于不同的库;-) -
嗨,对不起,我现在删除了它!
标签: python json function py-telegram-bot-api