In the send_service_names function, I get information from the api, I start looping, and using InlineKeyboardMarkup, I set the information obtained from the api into callback_data, and using the arg in the next function, i.e. send_service_information, I use the obtained information and According to that information, I will send the next request to the server, and finally, with the obtained answer, I will create a menu like the previous function. But the main question here is that I want to send this using callaback_data to the next function (in a chain), but When I run the code and the user clicks on the menu, the next function code is not executed and the same function, send_service_information, is executed again.
from telebot import types
from json import loads
from math import floor
from services.apiPhone import getServices, getInfo
from services.apiPayment import createPayment
TOKEN = "6521484940:AAHzkep5lCmEOz0zCUPIPScHdJv3OqQzH3E"
bot = telebot.TeleBot(TOKEN)
@bot.message_handler(commands=["start"])
def start_message(message):
bot.reply_to(message,"درود به ربات ما خوش اومدی????️")
markup = types.ReplyKeyboardMarkup(resize_keyboard=True,one_time_keyboard=True)
virtualPhone = types.KeyboardButton("شماره مجازی")
markup.add(virtualPhone)
msg = bot.send_message(message.chat.id,'لطفا یکی از گزینه های زیر را انتخاب کنید:',reply_markup=markup)
bot.register_next_step_handler(msg,process_name_step)
def process_name_step(message):
if message.text == "شماره مجازی":
send_service_names(message)
else:
pass
def send_service_names(message):
serviceInfo = loads(getServices())
keyboard = types.InlineKeyboardMarkup()
for i in range(len(serviceInfo)):
buttonMarkup = types.InlineKeyboardButton(serviceInfo[i]["name"],callback_data= serviceInfo[i]["id"] + "," + serviceInfo[i]["name"])
keyboard.add(buttonMarkup)
msg = bot.send_message(message.chat.id,"لطفا سرویس مورد نظر خود را انتخاب کنید:",reply_markup=keyboard)
bot.register_next_step_handler(msg,send_service_information)
@bot.callback_query_handler(func=lambda call: True)
def send_service_information(call):
serviceInfo = call.data
idService = serviceInfo.split(",")[0]
nameService = serviceInfo.split(",")[1]
try:
bot.send_message(call.message.chat.id, nameService)
keyboard = types.InlineKeyboardMarkup()
service = loads(getInfo(idService))
if "RESULT" in service and int(service["RESULT"] == 0):
bot.send_message(call.message.chat.id,"شماره ای برای این سرویس موجود نیست")
exit
for i in range(len(service)):
if int(service[i]["count"]) > 0:
buttonMarkup = types.InlineKeyboardButton(
f"نام کشور: {service[i]['cname']} / قیمت: {floor(int(service[i]['amount']) * 0.2 + int(service[i]['amount'])):,} تومان"
, callback_data=service[i]["id"])
keyboard.add(buttonMarkup)
print(call.message.text)
msg = bot.send_message(call.message.chat.id, f"{nameService} تعرفه سرویس", reply_markup=keyboard)
bot.register_next_step_handler(msg,handle_button_click)
except Exception as e:
print(f"An error occurred: {str(e)}")
def handle_button_click(message):
print("hello")
if __name__ == "__main__":
bot.polling()