C
C
Cyril2020-04-21 01:19:43
Python
Cyril, 2020-04-21 01:19:43

Why doesn't bot.callback_query_handler work?

Hello.
I'm trying to create a bot for surveys, but pitfalls immediately pop up, namely:

1. Why does the last function (def call_opros (call)) not respond to a button click? I found the register_next_step_handler method, but it didn't work with it either.
2. How to implement auto-posting to a group, like in Bot controller?
3. How to implement the choice of the right / wrong answer?
I would appreciate any help, thanks in advance.

THE CODE:

import telebot
import config
from telebot import types
bot = telebot.TeleBot(config.TOKEN)

@bot.message_handler(commands=["start"])

def start(message):
    key = types.InlineKeyboardMarkup()
    key_start = types.InlineKeyboardButton(text="Создать опрос",callback_data="first")
    key.add(key_start)
    mess = "Привет, этот бот умеет создавать опросы"
    bot.send_message(message.chat.id, mess, reply_markup=key)


@bot.callback_query_handler(func=lambda call:True)

def call_opros(call):
    if call.data == "first":
        bot.send_message(call.message.chat.id, text="Введите свой вопрос")


@bot.message_handler(content_types=["text"])

def question(message):
    key = types.InlineKeyboardMarkup()
    key_v = types.InlineKeyboardButton(text="Добавить варианты ответов", callback_data="sec")
    key.add(key_v)
    mess = "Отлично, Ваш вопрос:\n" + message.text + "\nТеперь добавьте варианты ответов"
    msg=bot.send_message(message.chat.id, mess, reply_markup=key)
    bot.register_next_step_handler(msg, call_answer)


@bot.callback_query_handler(func=lambda call:True)

def call_answer(call):
    if call.data == "sec":
        bot.send_message(call.message.chat.id, text="Пришлите свои варианты ответа в столбик")



bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cyril, 2020-04-21
@Pus1st

The first one finally worked. The first handler pulls everything onto itself, if I understand correctly.
here is the code

@bot.callback_query_handler(lambda first: first.data=="first")

def call_opros(first):
    if first.data == "first":
        bot.send_message(first.message.chat.id, text="Введите свой вопрос")

With the second it is similar. How exactly the parameters work, I have not yet fully understood, can someone explain.

A
Aruy137, 2021-08-28
@Aruy137

It is necessary to clarify in the body of @bot.callback_query_handler(lambda first: True) what value this function should handle, like if.
lambda first: first.data=="what it should handle"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question