Как сделать так что бы после определенной функции исполнялась та функция которую мне надо
Здраствуйте! Возникла проблема, надо что бы после того как в функции answer посчитала сколько раз нажали кнопку. После этого дать один ответ из трех.
Писал код в callback_query_handler но если на кнопку нажмут 10 человек то после 5 секунд человеку который отправлял запрос придёт 10 одинаковых ответов.
@bot.callback_query_handler(func = lambda call: True)
def answer(call):
global s_yes
global s_no
if call.data == 'yes':
s_yes += 1
if call.data == 'no':
s_no += 1
time.sleep(5)
@bot.message_handler(func = lambda message: True)
def replay_answer(message):
s_n = s_no
s_y = s_yes
global user_id
if s_y > s_n:
bot.send_message(user_id, 'Ты прошел')
elif s_y < s_n :
bot.send_message(user_id, 'Извены')
elif s_y == s_n:
bot.send_message(user_id, 'Подожди')
Ответы (1 шт):
Автор решения: Violet
→ Ссылка
bot = telebot.TeleBot(...)
yes = {}
no = {}
<...>
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.data == 'yes':
if call.message.chat.id in yes:
yes.update({call.message.chat.id: {'yes': yes[call.message.chat.id]['yes'] + 1}})
else:
yes.update({call.message.chat.id: {'yes': 1}})
elif call.data == 'no':
if call.message.chat.id in no:
no.update({call.message.chat.id: {'no': no[call.message.chat.id]['no'] + 1}})
else:
no.update({call.message.chat.id: {'no': 1}})
print('Y', yes)
print('N', no)
# noinspection PyBroadException
try:
if yes[call.message.chat.id]['yes'] > no[call.message.chat.id]['no']:
print('yes > no')
elif yes[call.message.chat.id]['yes'] < no[call.message.chat.id]['no']:
print('yes < no')
elif yes[call.message.chat.id]['yes'] == no[call.message.chat.id]['no']:
print('yes = no')
except Exception:
pass
при первом нажатии, допустим, на yes:
>>> Y {id: {'yes': 1}}
N {}
в той же сессии, при нажатии на no:
>>> Y {id: {'yes': 1}}
N {id: {'no': 1}}
yes = no
пример нужно оптимизировать