Top.Mail.Ru
Ответы

Помогите с кодом python для тг бота aiogram

Взял код из интернета, запускаю а там выдаёт ошибку в командной строке.
Можно ли import executor чем то заменить? Помогите пж

12345678910111213141516171819202122
 from aiogram import Bot, types 
from aiogram.dispatcher import dispatcher 
from aiogram.utils import executor 
 
 

bot = Bot(token=TOKEN) 
dp = dispatcher(bot) 
 
@dp.message_handler(commands=['start', 'help']) 
async def send_welcome(msg: types.Message): 
    await msg.reply_to_message("Я бот. Приятно познакомиться,") 
     
@dp.message_handler(content_types=['text']) 
async def get_text_messages(msg: types.Message): 
   if msg.text.lower() == 'привет': 
       await msg.answer('Привет!') 
   else: 
       await msg.answer('Не понимаю, что это значит.')     
 
if __name__ == '__main__': 
    executor.start_polling(dp) 
Дополнен

Строку с токеном я убрал только тут

По дате
По Рейтингу
Аватар пользователя
Мудрец
8мес

from aiogram import Bot, Dispatcher, types
from aiogram.filters import Command
import asyncio
import aiohttp

TOKEN = 'ваш_токен_бота'

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

async def complex_calculation(text):
async with aiohttp.ClientSession() as session:
async with session.get(f'https://api.example.com/process?text={text}') as resp:
await asyncio.sleep(1)
return f"Результат обработки: {text.upper()}"

@dp.message(Command(['start', 'help']))
async def send_welcome(msg: types.Message):
await msg.reply("Я бот. Приятно познакомиться!")

@dp.message()
async def handle_messages(msg: types.Message):
text = msg.text.lower()
if text == 'привет':
response = 'Привет!'
else:
response = await complex_calculation(text)

await msg.answer(response)

async def main():
await asyncio.gather(
dp.start_polling(bot),
background_task()
)

async def background_task():
while True:
await asyncio.sleep(60)
print("Выполняется фоновая задача...")

if __name__ == '__main__':
asyncio.run(main())