Как заставить бота телеграмм периодически отправлять сообщения?
Всем привет, сделал рабочий парсер телеграмм. Хочу сделать чтобы он автоматически (спустя определенный промежуток времени) отправлял прайс. На данный момент отправляет только по нажатию команды /price.
код:
import requests
import telebot
from config import token
from datetime import datetime
def get_data():
req = requests.get("https://yobit.net/api/3/ticker/btc_usd")
response = req.json()
print(response)
sell_price = response["btc_usd"]["sell"]
print(f"{datetime.now().strftime('%Y-%m-%d %H:%M')}\nSell BTC price: {sell_price}")
volume = response["btc_usd"]["vol"]
print(f"\namount BTC: {volume}")
def telegram_bot(token):
bot = telebot.TeleBot(token)
@bot.message_handler(commands=["price"])
def send_text(message):
if message.text.lower() == "/price":
try:
req = requests.get("https://yobit.net/api/3/ticker/btc_usdt")
response = req.json()
sell_price = response["btc_usdt"]["sell"]
volume = response["btc_usdt"]["vol"]
bot.send_message(
message.chat.id,
f"{datetime.now().strftime('%Y-%m-%d %H:%M')}\nSell BTC price: {sell_price}"
f"\nVolume: {volume}")
except Exception as ex:
print(ex)
bot.send_message(
message.chat.id,
"Damn...Something was wrong...")
bot.polling()
if __name__ == '__main__':
#get_data()
#get_data2()
telegram_bot(token)
Ответы (1 шт):
Автор решения: dfdbn
→ Ссылка
Я сделал, немного криво но сделал. В начале кода приписал тайм слип просто и всё. Может пригодится кому.
