I’ve got a code:
import asyncio
import logging
import sys
import aiohttp
from aiogram import Bot, Dispatcher, html
from aiogram.client.default import DefaultBotProperties
from aiogram.enums import ParseMode
from aiogram.filters import CommandStart
from aiogram.types import Message
TELEGRAM_TOKEN = '!'
OPENAI_API_URL = '!'
OPENAI_API_KEY = '!'
dp = Dispatcher()
async def generate_response(user_message):
headers = {
'Authorization': f'Bearer {OPENAI_API_KEY}',
'Content-Type': 'application/json'
}
payload = {
'message': user_message
}
async with aiohttp.ClientSession() as session:
async with session.post(OPENAI_API_URL, headers=headers, json=payload) as response:
response_data = await response.json()
return response_data['response']
@dp.message(CommandStart())
async def command_start_handler(message: Message) -> None:
"""
This handler receives messages with `/start` command
"""
await message.answer(f"Привет! Я ваш персональный GPT бот. Как я могу помочь вам сегодня?")
@dp.message()
async def echo_handler(message: Message) -> None:
"""
Handler will forward receive a message back to the sender
"""
user_message = message.text
response = await generate_response(user_message)
await message.answer(response)
async def main() -> None:
# Initialize Bot instance with default bot properties which will be passed to all API calls
bot = Bot(token=TELEGRAM_TOKEN, default=DefaultBotProperties(parse_mode=ParseMode.HTML))
# And the run events dispatching
await dp.start_polling(bot)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, stream=sys.stdout)
asyncio.run(main())
And I see this error while trying to chat in the telegram:
I’ve created the action to generate a response via bot:
The main thing is that it works in the gpt:
So, how to use my custom gpt via telegram bot? What am I doing wrong?
I’ve tried to generate the response in the chatgpt and successfully received it: