mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Rewrite quick start // include code from file
This commit is contained in:
parent
e12121f3d2
commit
e044267b50
2 changed files with 24 additions and 43 deletions
|
|
@ -1,4 +1,7 @@
|
||||||
Echo bot
|
Echo bot
|
||||||
========
|
========
|
||||||
|
|
||||||
example
|
|
||||||
|
.. literalinclude:: ../../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
|
:linenos:
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
===========
|
||||||
Quick start
|
Quick start
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
|
@ -6,62 +7,39 @@ Simple template
|
||||||
|
|
||||||
At first you have to import all necessary modules
|
At first you have to import all necessary modules
|
||||||
|
|
||||||
.. code-block:: python3
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
from aiogram import Bot, Dispatcher, executor, types
|
:lines: 1-4
|
||||||
|
|
||||||
Then you have to initialize bot and dispatcher instances.
|
Then you have to initialize bot and dispatcher instances.
|
||||||
Bot token you can get from `@BotFather <https://t.me/BotFather>`_
|
Bot token you can get from `@BotFather <https://t.me/BotFather>`_
|
||||||
|
|
||||||
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
.. code-block:: python3
|
:language: python
|
||||||
|
:lines: 10-12
|
||||||
bot = Bot(token='BOT TOKEN HERE')
|
|
||||||
dp = Dispatcher(bot)
|
|
||||||
|
|
||||||
Next step: interaction with bots starts with one command. Register your first command handler:
|
Next step: interaction with bots starts with one command. Register your first command handler:
|
||||||
|
|
||||||
.. code-block:: python3
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
@dp.message_handler(commands=['start', 'help'])
|
:lines: 15-17
|
||||||
async def send_welcome(message: types.Message):
|
|
||||||
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
|
||||||
|
|
||||||
If you want to handle all messages in the chat simply add handler without filters:
|
If you want to handle all messages in the chat simply add handler without filters:
|
||||||
|
|
||||||
.. code-block:: python
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
@dp.message_handler()
|
:lines: 27-29
|
||||||
async def echo(message: types.Message):
|
|
||||||
await bot.send_message(message.chat.id, message.text)
|
|
||||||
|
|
||||||
Last step: run long polling.
|
Last step: run long polling.
|
||||||
|
|
||||||
.. code-block:: python3
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
if __name__ == '__main__':
|
:lines: 32-33
|
||||||
executor.start_polling(dp)
|
|
||||||
|
|
||||||
Summary
|
Summary
|
||||||
-------
|
-------
|
||||||
|
|
||||||
.. code-block:: python3
|
.. literalinclude:: ../../examples/echo_bot.py
|
||||||
|
:language: python
|
||||||
from aiogram import Bot, Dispatcher, executor, types
|
:linenos:
|
||||||
|
:lines: -19,27-
|
||||||
bot = Bot(token='BOT TOKEN HERE')
|
|
||||||
dp = Dispatcher(bot)
|
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler(commands=['start', 'help'])
|
|
||||||
async def send_welcome(message: types.Message):
|
|
||||||
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
|
||||||
|
|
||||||
|
|
||||||
@dp.message_handler()
|
|
||||||
async def echo(message: types.Message):
|
|
||||||
await bot.send_message(message.chat.id, message.text)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
executor.start_polling(dp)
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue