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)