aiogram/examples/echo_bot.py

42 lines
1.1 KiB
Python
Raw Normal View History

2017-05-27 03:11:20 +03:00
import asyncio
import logging
2017-06-03 10:53:13 +03:00
from aiogram import Bot, types
2017-05-27 03:11:20 +03:00
from aiogram.dispatcher import Dispatcher
2017-11-21 00:53:53 +02:00
from aiogram.utils.executor import start_polling
2017-05-27 03:11:20 +03:00
API_TOKEN = 'BOT TOKEN HERE'
logging.basicConfig(level=logging.INFO)
loop = asyncio.get_event_loop()
bot = Bot(token=API_TOKEN, loop=loop)
2017-05-27 03:11:20 +03:00
dp = Dispatcher(bot)
@dp.message_handler(commands=['start', 'help'])
2017-06-03 10:53:13 +03:00
async def send_welcome(message: types.Message):
2017-05-27 03:11:20 +03:00
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
@dp.message_handler(regexp='(^cat[s]?$|puss)')
2017-06-03 10:53:13 +03:00
async def cats(message: types.Message):
2017-05-27 03:11:20 +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)
if __name__ == '__main__':
2017-11-21 00:53:53 +02:00
start_polling(dp, loop=loop, skip_updates=True)
# Also you can use another execution method
2017-11-19 01:05:50 +02:00
# >>> try:
# >>> loop.run_until_complete(main())
# >>> except KeyboardInterrupt:
# >>> loop.stop()