mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-10 17:53:17 +00:00
Add example. Simple Echo bot.
This commit is contained in:
parent
42e2630d3e
commit
2fdd804114
2 changed files with 43 additions and 0 deletions
BIN
examples/data/cats.jpg
Normal file
BIN
examples/data/cats.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 95 KiB |
43
examples/echo_bot.py
Normal file
43
examples/echo_bot.py
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from aiogram.bot import AIOGramBot
|
||||||
|
from aiogram.dispatcher import Dispatcher
|
||||||
|
|
||||||
|
API_TOKEN = 'BOT TOKEN HERE'
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
bot = AIOGramBot(token=API_TOKEN, loop=loop)
|
||||||
|
dp = Dispatcher(bot)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler(commands=['start', 'help'])
|
||||||
|
async def send_welcome(message):
|
||||||
|
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler(regexp='(^cat[s]?$|puss)')
|
||||||
|
async def cats(message):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
@dp.message_handler()
|
||||||
|
async def echo(message):
|
||||||
|
await bot.send_message(message.chat.id, message.text)
|
||||||
|
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
count = await dp.skip_updates()
|
||||||
|
print(f"Skipped {count} updates.")
|
||||||
|
await dp.start_pooling()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
try:
|
||||||
|
loop.run_until_complete(main())
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
loop.stop()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue