0
import openai
from aiogram import Bot, types
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor

token = '31232131231231231'

openai.api_key = '1231231232321'

bot = Bot(token)
dp = Dispatcher(bot)


@dp.message_handler()
async def send(message : types.Message):
    response = openai.Completion.create(
    model="text-davinci-003",
    prompt=message.text,
    temperature=0.9,
    max_tokens=1000,
    top_p=1,
    frequency_penalty=0.0,
    presence_penalty=0.6,
    stop=["You:"]
)
    await message.answer(response['choices'][0]['text'])

executor.start_polling(dp, skip_updates=True)

1 ответ 1

1

у Вас создан только пустой handler, который улавливает всё подряд. Если хотите, чтобы бот реагировал на команды, аргументом нужно передавать команды:

@dp.message_handler(commands=['start']) #реагирует на команду /start(команды можно делать и на кириллице)
async def ru_menu(message: types.Message):
    await bot.send_message(message.from_user.id, 'Привет')

Ещё можно сделать пустой handler, который будет реагировать, если введен определенный текст(Способ подходит, когда боту нужно отвечать на нажатые reply-кнопки от юзера:

    @dp.message_handler()
    async def ru_menu(message: types.Message):
        if message.text =='Привет': #реагирует при отправке юзером 'Привет'
            await bot.send_message(message.from_user.id, 'И тебе привет!')

Также можно сделать callback_query_handler, который будет реагировать на callback_data из инлайн-кнопок:

hello_markup = InlineKeyboardMarkup(row_width=1)
btn_1 = InlineKeyboardButton(text='Привет', callback_data='hello')
hello_markup.insert(btn_1)

@dp.callback_query_handler(text ="hello") #реагирует на callback_data, которую мы ввели в инлайн-кнопку выше
async def hello(callback: types.CallbackQuery):
    await bot.send_message(callback.from_user.id, 'И тебе привет!')

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.