P
P
Python Newbie2021-10-15 11:52:00
Python
Python Newbie, 2021-10-15 11:52:00

How to make buttons and vote count under a post on pytelegrambotapi?

I want to make that under the message there were buttons. You click on one: the value is added to it. On the other also. But what would you press it was possible only 1 time.

Can you please advise how to do this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InternetMaster, 2021-10-15
@InternetMaster

Welcome to the very nasty side of the Telegram API!
The topic touches on the most annoying, complex and forever idle side of the Telegram Bot!
We have to work with edit_message_text also with call, and of course with the database!
PS The following code assumes that you know very well how to work with sqlite3 in python
You should create a table in the database where you will record the user_id of all who pressed at least one button, and 2 more columns for each button with the total result of the number of all users who pressed her.
Add a message with a button

def deffers():
    cursor.execute('SELECT obshee_1 FROM tablica')
    knopach1 = cursor.fetchone()
    cursor.execute('SELECT obshee_2 FROM tablica')
    knopach2 = cursor.fetchone()
    klava = types.InlineKeyboardMarkup()
    button1=types.InlineKeyboardButton(text='Всего: '+knopach1[0], callback_data='knopka1')
    button2=types.InlineKeyboardButton(text='Всего: '+knopach2[0], callback_data='knopka2')
    bot.send_message(message.text, 'Ваше абсолютно любое сообщение. Абсолютно любое.', reply_markup=klava)

Now we need to handle the user button click.
Then, in the handler, you must pull out the value of how many people voted for the pressed button, add +1 to it and return the new value to the database, entering the user_id, then clicking on the same or another button with the same user_id will not work.
Then you need to do bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text='Total: [WHAT WAS+1]',reply_markup=klava)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question