2021-05-25 00:56:44 +03:00
|
|
|
import logging
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
from aiogram import Bot, Dispatcher, types
|
2021-05-25 00:56:44 +03:00
|
|
|
from aiogram.types import Message
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
TOKEN = "42:TOKEN"
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
|
2021-05-25 00:56:44 +03:00
|
|
|
logger = logging.getLogger(__name__)
|
2021-01-26 21:20:52 +02:00
|
|
|
|
2021-05-25 00:56:44 +03:00
|
|
|
|
2022-03-26 15:30:46 +00:00
|
|
|
@dp.message(commands=["start"])
|
2021-05-25 00:56:44 +03:00
|
|
|
async def command_start_handler(message: Message) -> None:
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
|
|
|
|
This handler receive messages with `/start` command
|
|
|
|
|
"""
|
2021-05-25 00:56:44 +03:00
|
|
|
# Most of event objects has an aliases for API methods to be called in event context
|
|
|
|
|
# For example if you want to answer to incoming message you can use `message.answer(...)` alias
|
|
|
|
|
# and the target chat will be passed to :ref:`aiogram.methods.send_message.SendMessage` method automatically
|
|
|
|
|
# or call API method directly via Bot instance: `bot.send_message(chat_id=message.chat.id, ...)`
|
|
|
|
|
await message.answer(f"Hello, <b>{message.from_user.full_name}!</b>")
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
|
2021-05-25 00:56:44 +03:00
|
|
|
@dp.message()
|
2022-03-26 15:30:46 +00:00
|
|
|
async def echo_handler(message: types.Message) -> None:
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
|
|
|
|
Handler will forward received message back to the sender
|
|
|
|
|
|
2021-05-25 00:56:44 +03:00
|
|
|
By default message handler will handle all message types (like text, photo, sticker and etc.)
|
2021-01-26 21:20:52 +02:00
|
|
|
"""
|
2021-05-25 00:56:44 +03:00
|
|
|
try:
|
|
|
|
|
# Send copy of the received message
|
|
|
|
|
await message.send_copy(chat_id=message.chat.id)
|
|
|
|
|
except TypeError:
|
|
|
|
|
# But not all the types is supported to be copied so need to handle it
|
|
|
|
|
await message.answer("Nice try!")
|
2021-01-26 21:20:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def main() -> None:
|
2021-05-25 00:56:44 +03:00
|
|
|
# Initialize Bot instance with an default parse mode which will be passed to all API calls
|
2021-01-26 21:20:52 +02:00
|
|
|
bot = Bot(TOKEN, parse_mode="HTML")
|
2021-05-25 00:56:44 +03:00
|
|
|
# And the run events dispatching
|
2021-01-26 21:20:52 +02:00
|
|
|
dp.run_polling(bot)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main()
|