E
E
elric1802019-11-18 13:52:33
Python
elric180, 2019-11-18 13:52:33

Why is the button not processed in the TelegramAPI and the function is not called?

The button is not processed and the function is not called in the following code:

def inlin2(c):
  print("hi")
  if c.data == 'news':
    print("Hi")
    bot.send_message(c.message.chat.id, 'Новость1')
bot.polling(none_stop=True)

I can not understand what the problem is, I did everything according to the instructions, but there is no result. When you press the button, there are no emotions, although the rest were written according to the same algorithm and work.
All code
# -*- coding: utf-8 -*-

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

@bot.message_handler(commands=['start'])
def exchange_command(message):  
  keyboard = telebot.types.InlineKeyboardMarkup()
  keyboard.row(
      telebot.types.InlineKeyboardButton('Жилье в Москве', callback_data='homeMoscow'),
      telebot.types.InlineKeyboardButton('Работа в Москве', callback_data='workMoscow'),
      telebot.types.InlineKeyboardButton('Новости', callback_data='news'),
      )
  keyboard.row(
    telebot.types.InlineKeyboardButton('Найти попутчиков для поездки', callback_data='findpeop'))

  keyboard.row(
      telebot.types.InlineKeyboardButton('Передача вещей из городов', callback_data='getGorod'),
      telebot.types.InlineKeyboardButton('Скидки от наших партнеров', callback_data='sale'),
      )

  bot.send_message(message.chat.id, 'Добро пожаловать ! Тут будет Ваш текст', reply_markup=keyboard)



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


def inlin(c):
  if c.data == 'homeMoscow':
    keyboard = telebot.types.InlineKeyboardMarkup()
    keyboard.row(
      telebot.types.InlineKeyboardButton('Ищу жилье в Москве', callback_data='findMoscow'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Предложу жилье в Москве без комиссии', callback_data='getMoscow'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Квартира сдалась удалить объявление', callback_data='delstatus'),
      )
    bot.send_message(c.message.chat.id, 'Раздел Жилье в Москве',reply_markup=keyboard)
  if c.data == 'workMoscow':
    keyboard = telebot.types.InlineKeyboardMarkup()
    keyboard.row(
      telebot.types.InlineKeyboardButton('Ищу работу', callback_data='findwork'),
      telebot.types.InlineKeyboardButton('Предложить работу', callback_data='getwork'),
      )
    bot.send_message(c.message.chat.id, 'Раздел Работа в Москве',reply_markup=keyboard)	
  if c.data == 'news':
    keyboard = telebot.types.InlineKeyboardMarkup()
    keyboard.row(
      telebot.types.InlineKeyboardButton('Новости', callback_data='findnews'),
      telebot.types.InlineKeyboardButton('Предложить новсть', callback_data='getnews'),
      )
    bot.send_message(c.message.chat.id, 'Раздел Новости',reply_markup=keyboard)


  if c.data == 'findpeop':
    keyboard = telebot.types.InlineKeyboardMarkup()
    keyboard.row(
      telebot.types.InlineKeyboardButton('Я еду из Астрахани', callback_data='downAstra'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Я еду в Астрахань', callback_data='getAstra'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Готов взять с собой в Москву', callback_data='getsaleMoscow'),
      telebot.types.InlineKeyboardButton('Помощь(FAQ)', callback_data='help'),
      )
    bot.send_message(c.message.chat.id, 'Раздел Поиск попутчиков',reply_markup=keyboard)
  if c.data == 'getGorod':
    keyboard = telebot.types.InlineKeyboardMarkup()
    keyboard.row(
      telebot.types.InlineKeyboardButton('Я еду из Астрахани в Москву, готов взять до 1 кг', callback_data='getMoscow1'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Я еду из Москвы в Астрахань, готов передать до 1кг', callback_data='getMoscowAstra'),
      )
    keyboard.row(
      telebot.types.InlineKeyboardButton('Мне надо передать в Москву до 1 кг', callback_data='getMoscow1'),
      )
    bot.send_message(c.message.chat.id, 'Раздел Передача вещей из городов',reply_markup=keyboard)


@bot.callback_query_handler(func=lambda c:c.data in ["findnews", "sds"])


def inlin2(c):
  print("hi")
  if c.data == 'news':
    print("Hi")
    bot.send_message(c.message.chat.id, 'Новость1')



bot.polling(none_stop=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
neggod, 2019-12-23
@neggod

1) Decorators should be "pressed close" to the method declaration.
2) The code is executed from top to bottom.

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


def inlin(c):
intercepts all callbacks and
@bot.callback_query_handler(func=lambda c:c.data in ["findnews", "sds"])


def inlin2(c):
nothing gets.
Swap them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question