Answer the question
In order to leave comments, you need to log in
Greetings. There is a json file that contains spattered news, how to make it so that every time you click on the button, the bot sends the next news?
The bot was written in aiogram.
Bot code:
import datetime
import json
from aiogram import Bot, Dispatcher, executor, types
from aiogram.utils.markdown import hbold, hunderline, hcode, hlink
from aiogram.dispatcher.filters import Text
from config import token
from MAIN import check_news_update
bot = Bot(token=token, parse_mode=types.ParseMode.HTML)
dp = Dispatcher(bot)
@dp.message_handler(commands="start")
async def start(message: types.Message):
start_buttons = ["Все новости", "Последние 5 новостей", "Свежие новости"]
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
keyboard.add(*start_buttons)
await message.answer("Лента новостей", reply_markup=keyboard)
@dp.message_handler(Text(equals="Все новости"))
async def get_all_news(message: types.Message):
with open("news_dict.json") as file:
news_dict = json.load(file)
for k, v in sorted(news_dict.items()):
news = f"{hbold(datetime.datetime.fromtimestamp(v['article_date_timestamp']))}\n" \
f"{hlink(v['article_title'], v['article_url'])}"
await message.answer(news)
@dp.message_handler(Text(equals="Последние 5 новостей"))
async def get_last_five_news(message: types.Message):
with open("news_dict.json") as file:
news_dict = json.load(file)
for k, v in sorted(news_dict.items())[-5:]:
news = f"{hbold(datetime.datetime.fromtimestamp(v['article_date_timestamp']))}\n" \
f"{hlink(v['article_title'], v['article_url'])}"
await message.answer(news)
@dp.message_handler(Text(equals="Свежие новости"))
async def get_fresh_news(message: types.Message):
fresh_news = check_news_update()
if len(fresh_news) >= 1:
for k, v in sorted(fresh_news.items()):
news = f"{hbold(datetime.datetime.fromtimestamp(v['article_date_timestamp']))}\n" \
f"{hlink(v['article_title'], v['article_url'])}"
await message.answer(news)
else:
await message.answer("Пока нет свежих новостей...")
if __name__ == '__main__':
executor.start_polling(dp)
Answer the question
In order to leave comments, you need to log in
If there is no reaction at all to check the wires from the button to the motherboard so that they are plugged in, one of them may have turned off.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question