2017-06-03 11:58:26 +03:00
|
|
|
Quick start
|
|
|
|
|
===========
|
|
|
|
|
|
2017-06-04 11:10:22 +03:00
|
|
|
Simple template
|
|
|
|
|
---------------
|
|
|
|
|
|
2018-01-23 12:48:09 +03:00
|
|
|
At first you have to import all necessary modules
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2017-11-16 19:37:52 +02:00
|
|
|
.. code-block:: python3
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-09-07 21:24:13 +03:00
|
|
|
from aiogram import Bot, Dispatcher, executor, types
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-01-23 12:48:09 +03:00
|
|
|
Then you have to initialize bot and dispatcher instances.
|
2017-11-16 19:37:52 +02:00
|
|
|
Bot token you can get from `@BotFather <https://t.me/BotFather>`_
|
2017-06-04 11:10:22 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
.. code-block:: python3
|
|
|
|
|
|
2018-06-02 00:54:23 +03:00
|
|
|
bot = Bot(token='BOT TOKEN HERE')
|
|
|
|
|
dp = Dispatcher(bot)
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-01-23 12:48:09 +03:00
|
|
|
Next step: interaction with bots starts with one command. Register your first command handler:
|
2017-06-04 11:10:22 +03:00
|
|
|
|
|
|
|
|
.. code-block:: python3
|
|
|
|
|
|
2018-08-11 13:37:06 +03:00
|
|
|
@dp.message_handler(commands=['start', 'help'])
|
|
|
|
|
async def send_welcome(message: types.Message):
|
|
|
|
|
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-09-07 21:24:13 +03:00
|
|
|
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)
|
|
|
|
|
|
2018-01-23 12:48:09 +03:00
|
|
|
Last step: run long polling.
|
2017-06-04 11:10:22 +03:00
|
|
|
|
|
|
|
|
.. code-block:: python3
|
|
|
|
|
|
2018-08-11 13:37:06 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
executor.start_polling(dp)
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2017-11-16 19:37:52 +02:00
|
|
|
Summary
|
|
|
|
|
-------
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2017-11-16 19:37:52 +02:00
|
|
|
.. code-block:: python3
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-09-07 21:24:13 +03:00
|
|
|
from aiogram import Bot, Dispatcher, executor, types
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-06-02 00:54:23 +03:00
|
|
|
bot = Bot(token='BOT TOKEN HERE')
|
|
|
|
|
dp = Dispatcher(bot)
|
2017-06-04 11:10:22 +03:00
|
|
|
|
2018-09-07 21:24:13 +03:00
|
|
|
|
2018-08-11 13:37:06 +03:00
|
|
|
@dp.message_handler(commands=['start', 'help'])
|
|
|
|
|
async def send_welcome(message: types.Message):
|
|
|
|
|
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
2018-01-13 18:42:21 +03:00
|
|
|
|
2018-09-07 21:24:13 +03:00
|
|
|
|
|
|
|
|
@dp.message_handler()
|
|
|
|
|
async def echo(message: types.Message):
|
|
|
|
|
await bot.send_message(message.chat.id, message.text)
|
|
|
|
|
|
|
|
|
|
|
2018-08-11 13:37:06 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
|
executor.start_polling(dp)
|