Как сделать так чтобы при нажатии на кнопку "хорошо" выполнялась функция gift1() и отправлялась в чат

Рейтинг: 1Ответов: 1Опубликовано: 04.03.2025
import random 
import telebot 
from telebot import types 

bot = telebot.TeleBot("###") 
mm = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True) 
button1 = types.KeyboardButton("🐣 Привет") 
button2 = types.KeyboardButton("😀 Как дела?") 
mm.add(button1,button2) 

@bot.message_handler(commands=['start']) 
def start(message): 
    bot.send_message(message.chat.id, "Hello World!", reply_markup=mm) 

@bot.message_handler(content_types=['text']) 
def handler(message): 
    if message.text == "🐣 Привет": 
        bot.send_message(message.chat.id, "Привет!") 
    if message.text == "😀 Как дела?": 
        otvet = types.InlineKeyboardMarkup(row_width=2) 
        button1 = types.InlineKeyboardButton("😎 Хорошо", callback_data='good') 
        button2 = types.InlineKeyboardButton("😥 Плохо", callback_data='bad') 
        otvet.add(button1,button2) 
        bot.send_message(message.chat.id, "Отлично! А у тебя?", reply_markup=otvet) 

@bot.callback_query_handler(func=lambda call: True) 
def callback_inline(call): 
    try: 
        if call.message: 
            if call.data == "good": 
                gift_message = gift1()  # Получаем результат функции gift1 
                bot.send_message(call.message.chat.id, gift_message)  # Отправляем результат в чат 
            if call.data == "bad": 
                bot.send_message(call.message.chat.id, "Ничего, все наладится!") 
    except Exception as e: 
        print(repr(e)) 

def gift1(min_value = 1000, max_value = 10000): 
    random_number = random.randint(min_value, max_value) 

    print(f" http://t.me/nft/RecordPlayer-{random_number} ") 

bot.polling(none_stop=True)

Ответы

▲ 0
def gift1(min_value = 1000, max_value = 10000): 
    random_number = random.randint(min_value, max_value) 

    print(f" http://t.me/nft/RecordPlayer-{random_number} ")
    return random_number # Добавлено

Ваша gift1 ничего не возвращала, добавьте return