aiogram/examples/echo_bot.py

46 lines
1.1 KiB
Python
Raw Normal View History

"""
This is a echo bot.
It echoes any incoming text messages.
"""
2017-05-27 03:11:20 +03:00
import logging
from aiogram import Bot, Dispatcher, executor, types
2017-05-27 03:11:20 +03:00
2019-06-29 19:53:18 +03:00
API_TOKEN = "BOT TOKEN HERE"
2017-05-27 03:11:20 +03:00
# Configure logging
2017-05-27 03:11:20 +03:00
logging.basicConfig(level=logging.INFO)
# Initialize bot and dispatcher
bot = Bot(token=API_TOKEN)
2017-05-27 03:11:20 +03:00
dp = Dispatcher(bot)
2019-06-29 19:53:18 +03:00
@dp.message_handler(commands=["start", "help"])
2017-06-03 10:53:13 +03:00
async def send_welcome(message: types.Message):
"""
This handler will be called when client send `/start` or `/help` commands.
"""
2017-05-27 03:11:20 +03:00
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
2019-06-29 19:53:18 +03:00
@dp.message_handler(regexp="(^cat[s]?$|puss)")
2017-06-03 10:53:13 +03:00
async def cats(message: types.Message):
2019-06-29 19:53:18 +03:00
with open("data/cats.jpg", "rb") as photo:
await bot.send_photo(
message.chat.id,
photo,
caption="Cats is here 😺",
reply_to_message_id=message.message_id,
)
2017-05-27 03:11:20 +03:00
@dp.message_handler()
2017-06-03 10:53:13 +03:00
async def echo(message: types.Message):
2017-05-27 03:11:20 +03:00
await bot.send_message(message.chat.id, message.text)
2019-06-29 19:53:18 +03:00
if __name__ == "__main__":
executor.start_polling(dp, skip_updates=True)