O
O
Oleksandr Serbinov2021-08-29 11:45:43
Python
Oleksandr Serbinov, 2021-08-29 11:45:43

How to implement a click counter on an inline button?

I can’t implement this counter, after the photo has been sent to the channel with a description and two buttons, I click on it, and nothing happens, maybe the handler doesn’t catch, or you need to start polling, but messages are sent to the channel until polling occurs, when it starts messages are not sent, I don’t know how to fix all this, I would be very happy if someone could help !!!

import asyncio

from asyncio.log import logger
from os import listdir

from aiogram import types
from aiogram.types import InputFile
from aiogram.utils.markdown import hlink

from config import channels
from keyboards.good_or_bad import good_or_bad_keyboard
from loader import bot, dp
from utils.notify_admins import on_startup_notify
import datetime


async def on_startup(dp):
    await on_startup_notify(dp)


async def send_photo(chat_id: int, photo: InputFile, caption: str):
    await bot.send_photo(chat_id, photo, caption)


# Переменные для итерации фото
mypath = "images"
photos = list()
formats = ['.jpg', '.jpeg', '.png']
savedSet = set()

# Переменные для ссылки на канал
one = '9x16 - Name Channel'
two = 't.me/link_on_channel'
three = '@name_channel'
url = two

# Переменные для итерации времени
time_3 = str(datetime.time(hour=11, minute=39))
time_3 = time_3[:-3]
time_5 = str(datetime.time(hour=11, minute=40))
time_5 = time_5[:-3]
time_7 = str(datetime.time(hour=11, minute=41))
time_7 = time_7[:-3]


# Переменные для итерации реакций

async def main(dp):

    async def generator_1(param):
        param += 1
        return param

    x_global = 0
    y_global = 0
    while True:
        @dp.callback_query_handler(text_contains='good')
        async def good_reaction(call: types.CallbackQuery):
            await call.answer('Вам понравилась публикация')
            x_local = 0
            x_local = x_global
            await generator_1(param=x_local)

        @dp.callback_query_handler(text_contains='bad')
        async def bad_reaction(call: types.CallbackQuery):
            await call.answer('Вам не понравилась публикация')
            y_local = 0
            y_local = y_global
            await generator_1(param=y_local)

        now_global = datetime.datetime.now()
        now_global = str(now_global.time())
        now_global = now_global.split('.')[0]
        now_global = now_global[:-3]
        if now_global == time_3:
            for i in formats:
                for j in filter(lambda x: x.endswith(i), listdir(mypath)):
                    photos.append(j)
            for i in photos:
                now_local = datetime.datetime.now()
                now_local = str(now_local.time())
                now_local = now_local.split('.')[0]
                now_local = now_local[:-3]
                if now_local == time_3 or time_5 or time_7:
                    with open(f"{mypath}/{i}", 'rb') as photo:
                        print(f'Сейчас {now_local}')
                        print('Отправляю фото')
                        await bot.send_photo(chat_id=channels, photo=photo, caption=f'{hlink(one, url=two)}\n'
                                                                                       f'{hlink(two, url=two)}\n'
                                                                                       f'{hlink(three, url=two)}',
                                                reply_markup=good_or_bad_keyboard)

                        @dp.callback_query_handler(text_contains='good')
                        async def good_reaction(call: types.CallbackQuery):
                            await call.answer('Вам понравилась публикация')
                            x_local = 0
                            x_local = x_global
                            await generator_1(param=x_local)

                        @dp.callback_query_handler(text_contains='bad')
                        async def bad_reaction(call: types.CallbackQuery):
                            await call.answer('Вам не понравилась публикация')
                            y_local = 0
                            y_local = y_global
                            await generator_1(param=y_local)
                    await asyncio.sleep(60)
                    if now_local == time_7:
                        print('Остановка')
                        await asyncio.sleep(120)
        else:
            pass
        try:
            await dp.start_polling()

        finally:
            await dp.storage.close()
            await dp.storage.wait_closed()
            await bot.session.close()

if __name__ == '__main__':
    try:
        asyncio.run(main(dp))
    except (KeyboardInterrupt, SystemExit):
        logger.error("Bot stopped!")

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question