Last active
June 4, 2018 07:08
-
-
Save pinhopro/f9dd299f5688fb5253a89f083814c216 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import asyncio | |
from aiogram import Bot, types | |
from aiogram.contrib.fsm_storage.memory import MemoryStorage | |
from aiogram.dispatcher import Dispatcher | |
from aiogram.types import ParseMode | |
from aiogram.utils import executor | |
from aiogram.utils.markdown import text, bold | |
API_TOKEN = '' | |
loop = asyncio.get_event_loop() | |
bot = Bot(token=API_TOKEN, loop=loop) | |
# For example use simple MemoryStorage for Dispatcher. | |
storage = MemoryStorage() | |
dp = Dispatcher(bot, storage=storage) | |
# States | |
ST_INITIAL = 'initial' | |
ST_CREATE_ACCT_EMAIL = 'create_acct_email' | |
ST_CREATE_ACCT_CONFIRM_EMAIL = 'create_acct_confirrm_email' | |
@dp.message_handler(commands=['start']) | |
async def cmd_start(message: types.Message): | |
""" | |
Conversation's entry point | |
""" | |
# Get current state | |
state = dp.current_state(chat=message.chat.id, user=message.from_user.id) | |
# Update user's state | |
await state.set_state(ST_INITIAL) | |
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, selective=True) | |
markup.add("Criar nova conta", "Sobre a VeloxTransfer") | |
await message.reply("Seja bem vindo a VeloxTransfer", reply_markup=markup, reply=False) | |
# You can use state '*' if you need to handle all states | |
@dp.message_handler(state='*', commands=['cancel']) | |
@dp.message_handler(state='*', func=lambda message: message.text.lower() == 'cancel') | |
async def cancel_handler(message: types.Message): | |
""" | |
Allow user to cancel any action | |
""" | |
with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state: | |
# Ignore command if user is not in any (defined) state | |
if await state.get_state() is None: | |
return | |
# Otherwise cancel state and inform user about it | |
# And remove keyboard (just in case) | |
await state.reset_state(with_data=True) | |
await message.reply('Canceled.', reply_markup=types.ReplyKeyboardRemove()) | |
@dp.message_handler(state=ST_INITIAL, func=lambda message: message.text == "Criar nova conta") | |
async def process_create_account(message: types.Message): | |
with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state: | |
await state.set_state(ST_CREATE_ACCT_EMAIL) | |
await message.reply("Qual é o seu email?", reply_markup=types.ReplyKeyboardRemove(), reply=False) | |
@dp.message_handler(state=ST_INITIAL, func=lambda message: message.text == "Sobre a VeloxTransfer") | |
async def process_create_account(message: types.Message): | |
markup = types.ReplyKeyboardMarkup(resize_keyboard=True, selective=True) | |
markup.add("Criar nova conta", "Sobre a VeloxTransfer") | |
await message.reply("A VeloxTransfers é uma marca da Blinktrade, operada no Brasil pela Intersowa.", | |
reply_markup=markup, | |
reply=False) | |
@dp.message_handler(state=ST_CREATE_ACCT_EMAIL, regexp='^([^@]+@[^@]+\.[^@]+)$' ) | |
async def process_user_signup_email(message: types.Message): | |
with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state: | |
user_email_confirmation_code=111111 # TODO: Generate and send a confirmation code to the user email | |
await state.update_data(unconfirmed_user_email=message.text) | |
await state.update_data(user_email_confirmation_code=user_email_confirmation_code) | |
await state.set_state(ST_CREATE_ACCT_CONFIRM_EMAIL) | |
return await message.reply("Por favor, digite o código de confirmação para o seu email.",reply=False) | |
@dp.message_handler(state=ST_CREATE_ACCT_EMAIL) | |
async def process_user_signup_email(message: types.Message): | |
await message.reply("Email Inválido.") | |
return await message.reply("Por favor, digite um endereço de email válido", reply=False) | |
@dp.message_handler(state=ST_CREATE_ACCT_CONFIRM_EMAIL, func=lambda message: not message.text.isdigit()) | |
async def process_user_signup_invalid_confirmation_email(message: types.Message): | |
return await message.reply("Código inválido!\nPor favor, digite o código de confirmação para o seu email.", | |
reply=False) | |
@dp.message_handler(state=ST_CREATE_ACCT_CONFIRM_EMAIL, func=lambda message: message.text.isdigit()) | |
async def process_user_signup_confirmation_email(message: types.Message): | |
with dp.current_state(chat=message.chat.id, user=message.from_user.id) as state: | |
data = await state.get_data() | |
if data['user_email_confirmation_code'] == int(message.text): | |
await state.update_data(user_email=data['unconfirmed_user_email']) | |
await state.finish() | |
else: | |
return await message.reply("Código inválido!\nPor favor, digite o código de confirmação para o seu email.", | |
reply=False) | |
async def shutdown(dispatcher: Dispatcher): | |
await dispatcher.storage.close() | |
await dispatcher.storage.wait_closed() | |
if __name__ == '__main__': | |
executor.start_polling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment