I am facing a TypeError: 'NoneType' object is not callable while developing a Telegram bot using pyTelegramBotAPI

I am currently in the process of building my initial Telegram bot. An error arises immediately after the send_team_info() function attempts to send the message “hello”. I have tried replacing it with a message handler using a lambda function, but that did not solve the issue. Could anyone assist me in identifying what might be wrong?

Here’s a snippet of my code for reference:

import telebot
from telebot import types
from data_fetcher import fetch_team_data

bot = telebot.TeleBot('###')

@bot.message_handler(commands=['start'])
def begin(message):
    welcome_message = f"Hello, <b>{message.from_user.first_name}</b> \n/help for commands list \n/search for search options"
    bot.send_message(message.chat.id, welcome_message, parse_mode='html')

@bot.message_handler(commands=['search'])
def initiate_search(message):
    markup = types.ReplyKeyboardMarkup(row_width=2)
    player_button = types.KeyboardButton("Look for a player")
    team_button = types.KeyboardButton("Look for a team")
    markup.add(player_button, team_button)
    bot.send_message(message.chat.id, 'Select an option:', reply_markup=markup)

@bot.message_handler(func=lambda message: message.text in ['Look for a player', 'Look for a team'])
def process_search(message):
    if message.text == 'Look for a team':
        msg = bot.send_message(message.chat.id, "Please provide the team name", reply_markup=types.ForceReply(selective=False))
        bot.register_next_step_handler(msg, create_search_link)

def create_search_link(message):
    search_terms = message.text.split()
    query_string = "+".join(search_terms)
    url = f"https://example.com/search?query={query_string}"
    results = fetch_team_data(url)
    bot.register_next_step_handler(message, display_results(message, results))

def display_results(message, results):
    reply_text = "Please select your team:
\n"
    markup = types.ReplyKeyboardMarkup(True)
    for index in range(len(results)):
        reply_text += f"{index + 1}. {results[index]['team_name']}
"
        markup.add(types.KeyboardButton(str(index + 1)))
    final_message = bot.send_message(message.chat.id, reply_text, reply_markup=markup)
    bot.register_next_step_handler(final_message, navigate_to_team_page, results)

def navigate_to_team_page(message, results):
    message_to_send = "hello"
    bot.send_message(message.chat.id, message_to_send, parse_mode='html')

while True:
    bot.polling(none_stop=True, timeout=5)

It’s essential to ensure every function call is passed correctly when using register_next_step_handler. Besides verification of function references, inspect any cases where the fetched data, if returned as None, might yield NoneType errors when processed later. In the create_search_link function, if fetch_team_data(url) returns None, it might be causing downstream issues. Ensure fetch_team_data is returning expected data or handling empty results before passing them to display_results. This might help isolate the source of the error.

Hi @DancingButterfly, you might want to recheck the register_next_step_handler. Make sure to pass fn_name (e.g., display_results) directly without calling it. Try bot.register_next_step_handler(message, display_results, results) instead of display_results(message, results). That might solve your issue.

It seems that the TypeError is caused by how the register_next_step_handler is utilized. Experience shows that this happens when functions are called rather than passed as a reference. Make sure when you pass display_results or any other function to register_next_step_handler, you do so without the parentheses. Additionally, verify the imports and initialization of any modules or packages you’re utilizing, as any missing or incorrect setups can trigger unexpected NoneType errors on function calls.