Error white creating a telegram bot

I am enrolled in Artificial Intelligence training where in module 3 there is tutorial to create a chat gpt telegram bot. But when I execute the code below it shows an error as seen in the screenshot.

I have seen the aiogram documentation and it turns out that executor is entirely removed from the library due to which the error is occurring. Please help to provide a workaround for this.

Hi @abdullahmd2002,

The error you’re encountering, ImportError: cannot import name 'executor' from 'aiogram' , indicates that the aiogram library doesn’t have an executor attribute.
To fix this issue, you should remove the executor import from the aiogram package. Instead, you can use the executor from aiogram.dispatcher :

from aiogram import Bot, Dispatcher, types
from aiogram.dispatcher import executor

Try this and let me know if it works.

Hi @rohit.ts,

I tried importing executor from aiogram.dispatcher but still getting import error in this as well.

ImportError: cannot import name 'executor' from 'aiogram.dispatcher'

Hi @abdullahmd2002,

To work around the removal of the executor in the aiogram library, you’ll need to handle the polling loop yourself. Here’s a modified version of your to achieve this:

import os
import openai
from aiogram import Bot, Dispatcher, types
from aiogram.types import ParseMode
from aiogram.contrib.middlewares.logging import LoggingMiddleware
from aiogram.utils import executor
from example import example

bot = Bot(token='YOUR_BOT_TOKEN')
dp = Dispatcher(bot)
openai.api_key = os.environ['OPENAI_API_KEY']

@dp.message_handler(commands=['start', 'help'])
async def welcome(message: types.Message):
    await message.reply('Hello! I am GPT Chat BOT. You can ask me anything :)')

async def gpt(message: types.Message):
    response = openai.Completion.create(
    await message.reply(response.choices[0].text, parse_mode=ParseMode.HTML)

async def on_startup(dp):
    logging_middleware = LoggingMiddleware()

if __name__ == "__main__":
    from aiogram import executor

    executor.start_polling(dp, skip_updates=True, on_startup=on_startup)

The main change is in the last block where we import executor and use it to start polling. We also define an on_startup function to set up any middleware (in this case, a logging middleware). Adjust the YOUR_BOT_TOKEN to your actual bot token. Make sure you have the necessary dependencies installed (aiogram , openai , etc.).

Let me know if this works.