A
A
Arthur2021-06-26 10:21:29
Python
Arthur, 2021-06-26 10:21:29

Why does the Telegram bot give an error message when clicking on the buttons?

Why, when I click on the start, it writes the message that I indicated, it gives out the keyboard too. But for example, if I click on any button, it displays a message from def welcome

import telebot
from telebot import types

bot = telebot.TeleBot('ТУТ ТОКЕН')


markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
butt1 = types.KeyboardButton('ЗАРАБОТАТЬ')
butt2 = types.KeyboardButton('КАНАЛ')
markup.add(butt1, butt2)

markup2 = types.ReplyKeyboardMarkup(resize_keyboard = True)
butt1 = types.KeyboardButton('Дальше')
butt2 = types.KeyboardButton('Назад')
markup2.add(butt1, butt2)

@bot.message_handler(content_types = ['text'])
def welcome(message):
    bot.send_message(message.chat.id, 'Привет епта ', reply_markup = markup)

@bot.message_handler(content_types= ['text'])
def start(message):
    bot.send_message(message.chat.id, 'Ага', reply_markup = markup)
    if message.text == 'ЗАРАБОТАТЬ':
        bot.send_message(message.chat.id, 'Что-бы заработать подними жопу', reply_markup = markup2)
    elif message.text =='КАНАЛ':
        bot.send_message(message.chat.id, 'Это наш канал', reply_markup = markup2)

def lala(message):
    if message.text == 'Дальше':
        bot.send_message(message.chat.id, 'Ага ага')
    elif message.text == 'Назад':
        bot.send_message(message.chat.id, 'Вы вернулись', reply_markup = markup)

bot.polling(none_stop = True)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Fallerwood, 2021-06-26
@TellS

In the decorator, you specify that it will work on any text coming to the bot. Text you send via buttons. Also, the start function is under exactly the same decorator.
Should be replaced by :
@bot.message_handler(commands=['start'])
Specified without changing anything

import telebot
from telebot import types

bot = telebot.TeleBot('ТУТ ТОКЕН')


markup = types.ReplyKeyboardMarkup(resize_keyboard = True)
butt1 = types.KeyboardButton('ЗАРАБОТАТЬ')
butt2 = types.KeyboardButton('КАНАЛ')
markup.add(butt1, butt2)

markup2 = types.ReplyKeyboardMarkup(resize_keyboard = True)
butt1 = types.KeyboardButton('Дальше')
butt2 = types.KeyboardButton('Назад')
markup2.add(butt1, butt2)

@bot.message_handler(content_types = ['text']) # Здесь вы указываете декоратор
def welcome(message):
    bot.send_message(message.chat.id, 'Привет епта ', reply_markup = markup)

@bot.message_handler(content_types= ['text']) # Здесь заменить
def start(message):
    bot.send_message(message.chat.id, 'Ага', reply_markup = markup)
    if message.text == 'ЗАРАБОТАТЬ':
        bot.send_message(message.chat.id, 'Что-бы заработать подними жопу', reply_markup = markup2)
    elif message.text =='КАНАЛ':
        bot.send_message(message.chat.id, 'Это наш канал', reply_markup = markup2)

def lala(message):
    if message.text == 'Дальше':
        bot.send_message(message.chat.id, 'Ага ага')
    elif message.text == 'Назад':
        bot.send_message(message.chat.id, 'Вы вернулись', reply_markup = markup)

bot.polling(none_stop = True)

B
balak_in, 2021-06-26
@balak_in

To process messages from buttons, use:
@bot.message_handler(regexp='ЗАРАБОТАТЬ')
I didn’t understand the question about start, try to explain

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question