|
| 1 | +from typing import Dict |
| 2 | + |
1 | 3 | from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove
|
2 | 4 | from telegram.constants import ChatAction
|
3 | 5 | from telegram.ext import ContextTypes, ConversationHandler
|
4 | 6 |
|
5 | 7 | from src.device.application.request_wifi import request_wifi
|
| 8 | +from src.shared.infrastructure.validators.validate_mac import is_valid_mac_address |
| 9 | + |
| 10 | +ACTIONABLE, ADD_MAC, SAVE_DEVICE = range(3) |
| 11 | +options = ["Mis dispositivos", "Agregar uno nuevo"] |
6 | 12 |
|
7 |
| -DEVICES, PHOTO, LOCATION, BIO = range(4) |
8 |
| -OPTIONS = ["Mis dispositivos", "Agregar uno nuevo"] |
| 13 | +data = {} |
9 | 14 |
|
10 | 15 |
|
11 |
| -async def device(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
| 16 | +async def device(update: Update, context: ContextTypes.DEFAULT_TYPE): |
12 | 17 | """Starts the conversation and asks the user about their gender."""
|
13 |
| - reply_keyboard = [OPTIONS] |
| 18 | + |
| 19 | + options = ["Mis dispositivos", "Agregar uno nuevo"] |
| 20 | + |
| 21 | + reply_keyboard = [ |
| 22 | + options, |
| 23 | + ] |
| 24 | + markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True) |
14 | 25 |
|
15 | 26 | await update.message.reply_text(
|
16 |
| - "En está sección podrás agregar y ver los dispositivos que has registrado.\n\n" |
17 |
| - "Envia /cancelar para terminar la conversación.\n\n" |
| 27 | + "🧾 Registra y gestiona tus dispositivos aquí.\n" |
18 | 28 | "Por favor selecciona una opción para continuar:",
|
19 |
| - reply_markup=ReplyKeyboardMarkup( |
20 |
| - reply_keyboard, one_time_keyboard=True, input_field_placeholder="Elije:" |
21 |
| - ), |
| 29 | + reply_markup=markup, |
22 | 30 | )
|
23 | 31 |
|
24 |
| - return DEVICES |
| 32 | + return ACTIONABLE |
| 33 | + |
25 | 34 |
|
| 35 | +async def show_response(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
| 36 | + """Store info provided by user and ask for the next category.""" |
| 37 | + text = update.message.text |
26 | 38 |
|
27 |
| -async def devices(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
28 |
| - """Stores the selected gender and asks for a photo.""" |
29 |
| - user = update.message.from_user |
30 |
| - message = update.message.text |
31 |
| - if message == OPTIONS[0]: |
| 39 | + if text == options[0]: |
32 | 40 | await update.message.reply_text(
|
33 |
| - f"Elegiste: {message}" |
| 41 | + "A continuación te mostraremos tus dispositivos agregados", |
| 42 | + reply_markup=ReplyKeyboardRemove() |
34 | 43 | )
|
35 |
| - return PHOTO |
36 |
| - if update.message.text == OPTIONS[1]: |
| 44 | + return ConversationHandler.END |
| 45 | + if text == options[1]: |
| 46 | + types = ['Telefono', 'PC'] |
| 47 | + reply_keyboard = [ |
| 48 | + types, |
| 49 | + ] |
| 50 | + markup = ReplyKeyboardMarkup(reply_keyboard, one_time_keyboard=True) |
37 | 51 | await update.message.reply_text(
|
38 |
| - f"Elegiste: {message}" |
| 52 | + "📱 Elige el tipo de dispositivo que agregarás.\n" |
| 53 | + "La red se optimizará para el tipo de dispositivo que agregues", |
| 54 | + reply_markup=markup, |
39 | 55 | )
|
40 |
| - return PHOTO |
41 | 56 |
|
| 57 | + return ADD_MAC |
42 | 58 |
|
43 |
| -async def photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
44 |
| - """Stores the photo and asks for a location.""" |
45 |
| - user = update.message.from_user |
46 |
| - photo_file = await update.message.photo[-1].get_file() |
47 |
| - await photo_file.download("user_photo.jpg") |
48 |
| - await update.message.reply_text( |
49 |
| - "Gorgeous! Now, send me your location please, or send /skip if you don't want to." |
50 |
| - ) |
51 | 59 |
|
52 |
| - return LOCATION |
| 60 | +async def add_mac(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
| 61 | + """Store info provided by user and ask for the next category.""" |
| 62 | + |
| 63 | + type = update.message.text |
| 64 | + data[update.message.chat_id] = type |
53 | 65 |
|
| 66 | + print(type) |
54 | 67 |
|
55 |
| -async def skip_photo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
56 |
| - """Skips the photo and asks for a location.""" |
57 |
| - user = update.message.from_user |
58 | 68 | await update.message.reply_text(
|
59 |
| - "I bet you look great! Now, send me your location please, or send /skip." |
| 69 | + "Agregaremos un nuevo dispositivo", |
| 70 | + reply_markup=ReplyKeyboardRemove() |
| 71 | + ) |
| 72 | + await update.message.reply_text( |
| 73 | + 'Escribe la dirección MAC' |
60 | 74 | )
|
61 | 75 |
|
62 |
| - return LOCATION |
| 76 | + return SAVE_DEVICE |
63 | 77 |
|
64 | 78 |
|
65 |
| -async def location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
66 |
| - """Stores the location and asks for some info about the user.""" |
67 |
| - user = update.message.from_user |
68 |
| - user_location = update.message.location |
69 |
| - await update.message.reply_text( |
70 |
| - "Maybe I can visit you sometime! At last, tell me something about yourself." |
71 |
| - ) |
| 79 | +async def save_device(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
| 80 | + """Store info provided by user and ask for the next category.""" |
72 | 81 |
|
73 |
| - return BIO |
| 82 | + mac = update.message.text |
| 83 | + print(mac, data.get(update.message.chat_id)) |
74 | 84 |
|
| 85 | + if is_valid_mac_address(mac): |
| 86 | + await update.message.reply_text( |
| 87 | + 'Dirección valida', |
| 88 | + reply_markup=ReplyKeyboardRemove() |
| 89 | + ) |
| 90 | + else: |
| 91 | + await update.message.reply_text( |
| 92 | + '❌ Dirección MAC invalida\nIntenta de nuevo en /dispositivos', |
| 93 | + reply_markup=ReplyKeyboardRemove() |
| 94 | + ) |
75 | 95 |
|
76 |
| -async def skip_location(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
77 |
| - """Skips the location and asks for info about the user.""" |
78 |
| - user = update.message.from_user |
79 |
| - await update.message.reply_text( |
80 |
| - "You seem a bit paranoid! At last, tell me something about yourself." |
81 |
| - ) |
| 96 | + return ConversationHandler.END |
82 | 97 |
|
83 |
| - return BIO |
84 | 98 |
|
| 99 | +async def done(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
| 100 | + """Display the gathered info and end the conversation.""" |
| 101 | + user_data = context.user_data |
| 102 | + if "choice" in user_data: |
| 103 | + del user_data["choice"] |
85 | 104 |
|
86 |
| -async def bio(update: Update, context: ContextTypes.DEFAULT_TYPE) -> int: |
87 |
| - """Stores the info about the user and ends the conversation.""" |
88 |
| - user = update.message.from_user |
89 |
| - await update.message.reply_text("Thank you! I hope we can talk again some day.") |
| 105 | + await update.message.reply_text( |
| 106 | + f"I learned these facts about you: {user_data}Until next time!", |
| 107 | + reply_markup=ReplyKeyboardRemove(), |
| 108 | + ) |
90 | 109 |
|
| 110 | + user_data.clear() |
91 | 111 | return ConversationHandler.END
|
0 commit comments