Search code examples
pythonasynchronoustelegramtelegram-botdispatcher

How to increment and store the value of a variable each time a button is pressed on a python telegram bot


Here is my code. Any time I run the code and press a button, it just increments once. For example if press team_1 button it will increment to 11 and keep on printing 11 but I want to increment the variable anytime the button is pressed add and not just one time

from aiogram import Bot, Dispatcher, executor, types
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton


button1 = InlineKeyboardButton(text= "Team 1", callback_data="team_1")
button2 = InlineKeyboardButton(text= "Team 2", callback_data="team_2")
keyboard_inline = InlineKeyboardMarkup().add(button1, button2)

bot = Bot(token='5603608851:AAH5VWjUoK-x_K3LScnIG6rbe87oBblHdrs')
dp = Dispatcher(bot)


@dp.message_handler(commands=['bet'])
async def option(message: types.Message):
    await message.reply("Select a team:", reply_markup=keyboard_inline)


@dp.message_handler(commands=['start'])
async def welcome(message: types.Message):
    await message.reply("Hello! Im Lele bot, Please follow the instruction")

#handler for clicking the buttom objects
@dp.callback_query_handler(text = ["team_1", "team_2"])
async def choice(call: types.CallbackQuery):
    a = 1
    b= 1
    if call.data == "team_1":
        b += 10
        await call.message.answer(b)
    if call.data == "team_2":
        a += 1
        await call.message.answer(a)
    await call.answer()


executor.start_polling(dp)

Solution

  • A little time and manipulations with global variables and the following option is ready:

    from aiogram import Bot, Dispatcher, executor, types
    
    keyboard_inline = types.InlineKeyboardMarkup()
    button1 = types.InlineKeyboardButton(text="Team 1", callback_data="team_1")
    button2 = types.InlineKeyboardButton(text="Team 2", callback_data="team_2")
    button3 = types.InlineKeyboardButton(text="result", callback_data="result")
    keyboard_inline.add(button1, button2)
    keyboard_inline.add(button3)
    
    
    bot = Bot(token='token')
    dp = Dispatcher(bot)
    
    
    @dp.message_handler(commands=['start'])
    async def welcome(message: types.Message):
        await message.reply("Hello! Im Lele bot, Please follow the instruction")
    
    
    @dp.message_handler(commands=['bet'])
    async def option(message):
        global a, b
        a = 0
        b = 0
        await message.reply("Select a team:", reply_markup=keyboard_inline)
        await bot.send_message(message.chat.id, "Team 1 - " + str(a) + " score\nTeam 2 - " + str(b) +" score")
    
    
    @dp.callback_query_handler(lambda call: True)
    async def choice(call: types.CallbackQuery):
        global a, b
        if call.data == "team_1":
            a += 1
            await bot.send_message(call.message.chat.id, "Team 1 - " + str(a) + " score\nTeam 2 - " + str(b) +" score")
        elif call.data == "team_2":
            b += 1
            await bot.send_message(call.message.chat.id, "Team 1 - " + str(a) + " score\nTeam 2 - " + str(b) +" score")
        elif call.data == "result":
            await bot.send_message(call.message.chat.id, "---FINAL SCORE---\n"
                                                         "Team 1 - " + str(a) + " score\nTeam 2 - " + str(b) + " score")
            a = b = 0
        await call.answer()
    
    
    if __name__ == '__main__':
        executor.start_polling(dp, skip_updates=True)
    

    I hope that programmers with experience do not throw stones at me;)