Rewrite quick start // include code from file

This commit is contained in:
Alex Root Junior 2018-09-07 23:17:20 +03:00
parent e12121f3d2
commit e044267b50
2 changed files with 24 additions and 43 deletions

View file

@ -1,4 +1,7 @@
Echo bot
========
example
.. literalinclude:: ../../../examples/echo_bot.py
:language: python
:linenos:

View file

@ -1,3 +1,4 @@
===========
Quick start
===========
@ -6,62 +7,39 @@ Simple template
At first you have to import all necessary modules
.. code-block:: python3
from aiogram import Bot, Dispatcher, executor, types
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:lines: 1-4
Then you have to initialize bot and dispatcher instances.
Bot token you can get from `@BotFather <https://t.me/BotFather>`_
.. code-block:: python3
bot = Bot(token='BOT TOKEN HERE')
dp = Dispatcher(bot)
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:lines: 10-12
Next step: interaction with bots starts with one command. Register your first command handler:
.. code-block:: python3
@dp.message_handler(commands=['start', 'help'])
async def send_welcome(message: types.Message):
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:lines: 15-17
If you want to handle all messages in the chat simply add handler without filters:
.. code-block:: python
@dp.message_handler()
async def echo(message: types.Message):
await bot.send_message(message.chat.id, message.text)
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:lines: 27-29
Last step: run long polling.
.. code-block:: python3
if __name__ == '__main__':
executor.start_polling(dp)
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:lines: 32-33
Summary
-------
.. code-block:: python3
from aiogram import Bot, Dispatcher, executor, types
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)
.. literalinclude:: ../../examples/echo_bot.py
:language: python
:linenos:
:lines: -19,27-