July 14

echo bot

1-tg bot


# 1. Kerakli kutubxonalarni import qilish

```python
from aiogram import Bot, Dispatcher, types, F
from asyncio import run
```

## Bot

Telegram API bilan ishlaydi.

Misol:

```python
bot = Bot(token=TOKEN)
```

Bot orqali:

- xabar yuborish
- rasm yuborish
- fayl yuborish
- foydalanuvchini bloklash
- va boshqa amallar bajariladi.

---

## Dispatcher

Botning boshqaruv markazi.

Kelgan barcha xabarlarni kerakli funksiyaga yuboradi.

Misol:

```
User
 ↓
Dispatcher
 ↓
Handler
```

---

## types

Telegram obyektlari.

Masalan:

```python
types.Message
```

Bu oddiy xabar.

---

## F

Filter.

Masalan:

```python
F.text
F.photo
F.document
```

Aiogram 3 da juda ko'p ishlatiladi.

---

## run

Async funksiyani ishga tushiradi.

```python
run(start())
```

Dastur shu joydan boshlanadi.

---

# 2. Dispatcher yaratish

```python
dp = Dispatcher()
```

Bu yerda Dispatcher obyekti yaratilmoqda.

Keyinchalik barcha handlerlar unga qo'shiladi.

---

# 3. Echo funksiyasi

```python
async def echo(message: types.Message, bot: Bot):
```

Bu asinxron funksiya.

Parametrlar:

### message

Foydalanuvchi yuborgan xabar.

Masalan:

```
Salom
```

---

### bot

Bot obyekti.

Lekin sizning funksiyangizda ishlatilmayapti.

Uni olib tashlasa ham bo'ladi.

---

# 4. copy_to()

```python
await message.copy_to(
    chat_id=message.chat.id
)
```

Bu xabarni nusxalaydi.

Masalan

User yozadi:

```
Assalomu alaykum
```

Bot ham

```
Assalomu alaykum
```

deb yuboradi.

---

## chat_id

```python
message.chat.id
```

Xabar kelgan chatning ID raqami.

---

# 5. start() funksiyasi

```python
async def start():
```

Bu botni ishga tushirish funksiyasi.

---

## Startup event

```python
dp.startup.register(startup_answer)
```

Bot ishga tushganda

```python
startup_answer()
```

funksiyasi ishlaydi.

Ammo sizning kodingizda

```python
startup_answer
```

yo'q.

Shuning uchun

```
NameError
```

chiqadi.

---

## Message handler

```python
dp.message.register(echo)
```

Har qanday kelgan xabar

```
echo()
```

funksiyasiga yuboriladi.

---

## Shutdown event

```python
dp.shutdown.register(shutdown_answer)
```

Bot to'xtaganda ishlaydi.

Ammo

```python
shutdown_answer
```

ham mavjud emas.

---

# 6. TOKEN

```python
TOKEN = "..."
```

Bu BotFather bergan maxfiy kalit.

Uni GitHub yoki internetga joylash mumkin emas.

Eng yaxshi usul:

```python
from os import getenv

TOKEN = getenv("BOT_TOKEN")
```

yoki `.env` faylidan o'qish.

---

# 7. Bot yaratish

```python
bot = Bot(token=TOKEN)
```

Bu yerda Telegram bot obyekti yaratilmoqda.

---

# 8. Polling

```python
await dp.start_polling(bot)
```

Bot Telegram serveriga ulanadi.

Keyin doimiy ravishda

```
Yangi xabar keldimi?
```

deb tekshiradi.

Bu Polling deyiladi.

---

# 9. run()

```python
run(start())
```

Dastur shu yerdan boshlanadi.

```
Python
 ↓
run()
 ↓
start()
 ↓
start_polling()
 ↓
Bot ishlaydi
```

# Kodning ishlash ketma-ketligi

```
Python

↓

run()

↓

start()

↓

Dispatcher

↓

Telegram

↓

User xabar yuboradi

↓

echo()

↓

copy_to()

↓

Userga xabar qaytadi
```

# Kodingizdagi xatolar

### 1.

```python
dp.startup.register(startup_answer)
```

`startup_answer` funksiyasi yozilmagan.

Masalan:

```python
async def startup_answer(bot: Bot):
    print("Bot ishga tushdi")
```

---

### 2.

```python
dp.shutdown.register(shutdown_answer)
```

Bu funksiya ham yozilmagan.

Masalan:

```python
async def shutdown_answer(bot: Bot):
    print("Bot to'xtadi")
```

---

### 3.

```python
async def echo(message: types.Message, bot: Bot):
```

Bu yerda `bot` ishlatilmayapti.

Shuning uchun oddiyroq yozish mumkin:

```python
async def echo(message: types.Message):
    await message.copy_to(chat_id=message.chat.id)
```

---

### 4.

Bot tokenini kod ichida saqlash tavsiya etilmaydi.

`.env` fayl ishlatish xavfsizroq.

from aiogram import Bot , Dispatcher, types, F
from asyncio import run

dp = Dispatcher()


async def echo(message: types.Message, bot: Bot):
    await message.copy_to( chat_id = message.chat.id)


async def start():
    #dp.startup.register(startup_answer)
    dp.message.register(echo)
    #dp.shutdown.register(shutdown_answer)
    TOKEN = "8735603900:AAFtmzVkH9By8Ypn7y2XRoZtB6TfERp67aY"
    bot = Bot(token = TOKEN)
    await dp.start_polling(bot)
            
run(start())