mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 01:15:31 +00:00
Remove deprecated code-stuff.
This commit is contained in:
parent
abaa6a6cf6
commit
2ac011521c
5 changed files with 4 additions and 1053 deletions
|
|
@ -1,91 +0,0 @@
|
|||
import asyncio
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.dispatcher.state import StateMachine, Controller
|
||||
|
||||
API_TOKEN = 'BOT TOKEN HERE'
|
||||
|
||||
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: types.Message):
|
||||
"""
|
||||
Entry point to conversation
|
||||
"""
|
||||
await message.reply("Hi there! What's your name?")
|
||||
|
||||
# Set state
|
||||
# This method lock all messages from user and all messages will be redirected to next step handler in state machine
|
||||
state.set_state(message.chat.id, message.from_user.id, "name")
|
||||
|
||||
|
||||
async def process_name(message: types.Message, controller: Controller):
|
||||
"""
|
||||
Process user name
|
||||
"""
|
||||
# Save name to storage
|
||||
controller["name"] = message.text
|
||||
|
||||
await message.reply("How old are you?")
|
||||
|
||||
# Go to next state
|
||||
controller.set_state('age')
|
||||
|
||||
|
||||
async def process_age(message: types.Message, controller: Controller):
|
||||
# Check age. Age must be is digit
|
||||
if not message.text.isdigit():
|
||||
return await message.reply("Age should be a number.\nHow old are you?")
|
||||
|
||||
# Save age to storage
|
||||
controller["age"] = int(message.text)
|
||||
|
||||
# Configure ReplyKeyboardMarkup
|
||||
markup = types.ReplyKeyboardMarkup()
|
||||
markup.add("Male", "Female")
|
||||
markup.add("Other")
|
||||
|
||||
await message.reply("What is your gender?", reply_markup=markup)
|
||||
|
||||
# Go to next state
|
||||
controller.set_state("sex")
|
||||
|
||||
|
||||
async def process_sex(message: types.Message, controller: Controller):
|
||||
# Check reply
|
||||
if message.text not in ["Male", "Female", "Other"]:
|
||||
return await message.reply("Bad gender name. Choose you gender from keyboard.")
|
||||
|
||||
# Save sex to storage
|
||||
controller["sex"] = message.text
|
||||
|
||||
# Remove keyboard
|
||||
markup = types.ReplyKeyboardRemove()
|
||||
# And send message
|
||||
await bot.send_message(message.chat.id,
|
||||
f"Hi!\n"
|
||||
f"Nice to meet you, {controller['name']}.\n"
|
||||
f"Age: {controller['age']}\n"
|
||||
f"Sex: {controller['sex']}",
|
||||
reply_markup=markup)
|
||||
|
||||
# Finish conversation
|
||||
controller.clear()
|
||||
|
||||
|
||||
# Configure state machine
|
||||
state = StateMachine(dp, {
|
||||
"name": process_name,
|
||||
"age": process_age,
|
||||
"sex": process_sex
|
||||
})
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
loop.run_until_complete(dp.start_pooling())
|
||||
except KeyboardInterrupt:
|
||||
loop.stop()
|
||||
|
|
@ -1,71 +0,0 @@
|
|||
import asyncio
|
||||
import logging
|
||||
|
||||
from aiogram import Bot, types
|
||||
from aiogram.dispatcher import Dispatcher
|
||||
from aiogram.types import ContentType
|
||||
|
||||
API_TOKEN = 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: types.Message):
|
||||
await message.reply("Hi!\nI'm EchoBot!\nPowered by aiogram.")
|
||||
|
||||
|
||||
@dp.message_handler(commands=['sticker'])
|
||||
async def save_sticker(message: types.Message):
|
||||
async def handle_bad_message(msg: types.Message):
|
||||
"""
|
||||
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,
|
||||
include_cancel=True)
|
||||
|
||||
if not msg:
|
||||
# If user send /cancel
|
||||
return await message.reply('Canceled.')
|
||||
|
||||
# Download file to memory (io.BytesIO)
|
||||
photo = await bot.download_file_by_id(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 document to user
|
||||
await bot.send_document(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