mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 01:15:31 +00:00
Add steps and downloads example.
This commit is contained in:
parent
4c210a2641
commit
bf9657dfc5
1 changed files with 65 additions and 0 deletions
65
examples/steps_and_downloading_file.py
Normal file
65
examples/steps_and_downloading_file.py
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.types import ContentType
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
bot = Bot(token=API_TOKEN, loop=loop)
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
@dp.message_handler(commands=['start'])
|
||||
async def send_welcome(message):
|
||||
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
||||
|
||||
|
||||
@dp.message_handler(commands=['sticker'])
|
||||
async def save_sticker(message):
|
||||
async def handle_bad_message(msg):
|
||||
"""
|
||||
Handler for unknown messages
|
||||
"""
|
||||
await msg.reply('That is not a sticker!')
|
||||
|
||||
# Create an reply markup (ForceReply)
|
||||
markup = types.ForceReply(selective=True)
|
||||
|
||||
# Send reply to user
|
||||
await message.reply('Please send me a sticker.', reply_markup=markup)
|
||||
|
||||
# Wait next message
|
||||
# It can only be a sticker
|
||||
msg = await dp.next_message(message,
|
||||
content_types=[ContentType.STICKER],
|
||||
otherwise=handle_bad_message)
|
||||
|
||||
# Download file to memory (io.BytesIO)
|
||||
photo = await bot.download_file(msg.sticker.file_id)
|
||||
# And you can use other syntax:
|
||||
# photo = io.BytesIO()
|
||||
# await bot.download_file(msg.sticker.file_id, photo)
|
||||
# Or use filename for download file to filesystem:
|
||||
# await bot.download_file(msg.sticker.file_id, 'sticker.webp')
|
||||
|
||||
# Send photo to user
|
||||
await bot.send_photo(message.chat.id, photo, caption=msg.sticker.emoji,
|
||||
reply_to_message_id=message.message_id)
|
||||
|
||||
|
||||
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