From 57abb7ecc658d74032680bdb5c288d069a22cea1 Mon Sep 17 00:00:00 2001 From: Davte Date: Sat, 17 Aug 2019 15:01:54 +0200 Subject: [PATCH] Ciclopi messages moved to dedicated module --- ciclopibot/ciclopi.py | 343 +++++------------------------------------ ciclopibot/messages.py | 318 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 357 insertions(+), 304 deletions(-) create mode 100644 ciclopibot/messages.py diff --git a/ciclopibot/ciclopi.py b/ciclopibot/ciclopi.py index 6e46e0d..b7c024e 100644 --- a/ciclopibot/ciclopi.py +++ b/ciclopibot/ciclopi.py @@ -15,291 +15,6 @@ from davtelepot.utilities import ( make_lines_of_buttons ) -default_ciclopi_messages = { - 'command': { - 'description': { - 'en': "CiloPi stations status", - 'it': "Stato delle stazioni CicloPi" - }, - 'reply_keyboard_button': { - 'en': "CicloPi 🚲", - 'it': "CicloPi 🚲" - }, - 'updating': { - 'en': "Updating", - 'it': "Aggiornamento in corso", - }, - 'unavailable_website': { - 'en': "CicloPi's website cannot be reached at the moment.\n" - "Please retry later :/", - 'it': "Il sito del CicloPi è momentaneamente irraggiungibile.\n" - "Riprova tra un po' :/" - }, - 'no_station_available': { - 'en': "No station available", - 'it': "Nessuna stazione" - }, - 'title': { - 'en': "CicloPi stations", - 'it': "Stazioni CicloPi" - }, - 'buttons': { - 'all': { - 'en': "All", - 'it': "Tutte" - }, - 'only_fav': { - 'en': "Favourites only", - 'it': "Solo preferite" - }, - 'first_n': { - 'en': "First {n}", - 'it': "Prime {n}" - }, - 'update': { - 'en': "🔄 Update", - 'it': "🔄 Aggiorna" - }, - 'legend': { - 'en': "📜 Legend", - 'it': "📜 Legenda" - }, - 'settings': { - 'en': "⚙️ Settings", - 'it': "⚙️ Impostazioni" - }, - } - }, - 'settings': { - 'sort': { - 'name': { - 'en': "Order", - 'it': "Ordina" - }, - 'description': { - 'en': "customize CicloPi stations viewing order.", - 'it': "scegli in che ordine visualizzare le stazioni CicloPi." - }, - 'symbol': { - 'en': "⏬", - 'it': "⏬" - } - }, - 'limit': { - 'name': { - 'en': "Number of stations", - 'it': "Numero di stazioni" - }, - 'description': { - 'en': "choose how many stations you want to view.", - 'it': "scegli quante stazioni visualizzare." - }, - 'symbol': { - 'en': "#️⃣", - 'it': "#️⃣" - } - }, - 'fav': { - 'name': { - 'en': "Favourite stations", - 'it': "Stazioni preferite" - }, - 'description': { - 'en': "edit favourite stations.", - 'it': "cambia le tue stazioni preferite." - }, - 'symbol': { - 'en': "⭐️", - 'it': "⭐️" - } - }, - 'setpos': { - 'name': { - 'en': "Set location", - 'it': "Cambia posizione", - }, - 'description': { - 'en': "set a location from which stations may be sorted by " - "distance.", - 'it': "imposta una posizione da cui ordinare le stazioni per " - "distanza." - }, - 'symbol': { - 'en': "🧭", - 'it': "🧭" - } - } - }, - 'sorting': { - 'center': { - 'name': { - 'en': "City center", - 'it': "Borgo" - }, - 'description': { - 'en': "sorted by distance from city center (Borgo Stretto " - " station.)", - 'it': "in ordine di distanza crescente da Borgo Stretto." - }, - 'short_description': { - 'en': "by distance from city center", - 'it': "per distanza da Borgo Stretto" - } - }, - 'alphabetical': { - 'name': { - 'en': "Alphabetical", - 'it': "Alfabetico" - }, - 'description': { - 'en': "in alphabetical order.", - 'it': "in ordine alfabetico." - }, - 'short_description': { - 'en': "by name", - 'it': "per nome" - } - }, - 'position': { - 'name': { - 'en': "Position", - 'it': "Posizione" - }, - 'description': { - 'en': "sorted by distance from last set position. " - "City center position is set by default.", - 'it': "in ordine di distanza crescente dall'ultima posizione " - "inviata. Di default sarà Borgo Stretto." - }, - 'short_description': { - 'en': "by distance", - 'it': "per distanza" - } - }, - 'custom': { - 'name': { - 'en': "Favourites", - 'it': "Preferite" - }, - 'description': { - 'en': "sorted by custom order.", - 'it': "nell'ordine che hai scelto." - }, - 'short_description': { - 'en': "customly ordered", - 'it': "in ordine personalizzato" - }, - } - }, - 'filters': { - 'fav': { - 'name': { - 'en': "Only favourite stations", - 'it': "Solo le preferite" - }, - 'all': { - 'en': "favourite stations first", - 'it': "prima le preferite" - }, - 'only': { - 'en': "only favourite stations", - 'it': "solo le preferite" - } - }, - 'num': { - 'en': "first {n}", - 'it': "prime {n}" - }, - 'all': { - 'name': { - 'en': "All stations", - 'it': "Tutte" - }, - }, - '3': { - 'name': { - 'en': "3", - 'it': "3" - }, - }, - '5': { - 'name': { - 'en': "5", - 'it': "5" - }, - }, - '10': { - 'name': { - 'en': "10", - 'it': "10" - }, - } - }, - 'status': { - 'not_available': { - 'en': "Not available", - 'it': "Non disponibile" - } - }, - 'set_position': { - 'success': { - 'en': "Position set!\n" - "From now on, stations will be sorted by distance from " - "this position.", - 'it': "Ho salvato questa posizione!\n" - "D'ora in poi ordinerò le stazioni dalla più vicina alla " - "più lontana da qui." - }, - 'cancel': { - 'en': "Operation cancelled.", - 'it': "Operazione annullata." - }, - 'cancel_and_remind': { - 'en': "I could not understand your position.\n" - "Try again with /ciclopi > Settings > Set location", - 'it': "Non ho capito la tua posizione.\n" - "Per riprovare fai " - "/ciclopi > Impostazioni > Cambia posizione" - } - }, - 'button': { - 'title': { - 'en': "CicloPi Settings", - 'it': "Impostazioni CicloPi" - }, - 'back_to_settings': { - 'en': "Back to settings", - 'it': "Torna alle impostazioni" - }, - 'back_to_stations': { - 'en': "Back to stations", - 'it': "Torna alle stazioni" - }, - 'legend': { - 'name': { - 'en': "Station name", - 'it': "Nome della stazione", - }, - 'distance': { - 'en': "Distance in meters", - 'it': "Distanza in m", - }, - 'description': { - 'en': "Station address", - 'it': "Indirizzo della stazione", - }, - 'bikes': { - 'en': "Available bikes", - 'it': "Bici disponibili", - }, - 'free': { - 'en': "Free parking stalls", - 'it': "Posti liberi", - } - } - } -} - _URL = "http://www.ciclopi.eu/frmLeStazioni.aspx" ciclopi_webpage = CachedPage.get( @@ -1185,8 +900,6 @@ async def _ciclopi_button_main(bot, update, user_record, arguments): ) return result, text, reply_markup -# TODO: Multilanguage support from this line - async def _ciclopi_button_sort(bot, update, user_record, arguments): result, text, reply_markup = '', '', None @@ -1211,9 +924,15 @@ async def _ciclopi_button_sort(bot, update, user_record, arguments): else 0 ) if new_choice == ciclopi_record['sorting']: - return "È già così!", '', None + return bot.get_message( + 'ciclopi', 'button', 'no_change', + update=update, user_record=user_record + ), '', None elif new_choice not in CICLOPI_SORTING_CHOICES: - return "Opzione sconosciuta!", '', None + return bot.get_message( + 'ciclopi', 'button', 'unknown_option', + update=update, user_record=user_record + ), '', None db['ciclopi'].upsert( dict( chat_id=chat_id, @@ -1223,11 +942,13 @@ async def _ciclopi_button_sort(bot, update, user_record, arguments): ensure=True ) ciclopi_record['sorting'] = new_choice - result = "Fatto!" - text = ( - "📜 Modalità di visualizzazione delle stazioni CicloPi 🚲\n\n" - "{options}\n\n" - "Scegli una nuova modalità o torna all'elenco delle stazioni." + result = bot.get_message( + 'ciclopi', 'button', 'done', + update=update, user_record=user_record + ) + text = bot.get_message( + 'ciclopi', 'button', 'sorting_header', + update=update, user_record=user_record ).format( options='\n'.join( "- {symbol} {name}: {description}".format( @@ -1294,9 +1015,15 @@ async def _ciclopi_button_limit(bot, update, user_record, arguments): else 0 ) if new_choice == ciclopi_record['stations_to_show']: - return "È già così!", '', None + return bot.get_message( + 'ciclopi', 'button', 'no_change', + update=update, user_record=user_record + ), '', None elif new_choice not in CICLOPI_STATIONS_TO_SHOW: - return "Opzione sconosciuta!", '', None + return bot.get_message( + 'ciclopi', 'button', 'unknown_option', + update=update, user_record=user_record + ), '', None db['ciclopi'].upsert( dict( chat_id=chat_id, @@ -1306,12 +1033,13 @@ async def _ciclopi_button_limit(bot, update, user_record, arguments): ensure=True ) ciclopi_record['stations_to_show'] = new_choice - result = "Fatto!" - text = ( - "📜 Modalità di visualizzazione delle stazioni CicloPi 🚲\n\n" - "{options}\n\n" - "Scegli quante stazioni vedere (quando filtrate per distanza) o torna " - "alle impostazioni o all'elenco delle stazioni." + result = bot.get_message( + 'ciclopi', 'button', 'done', + update=update, user_record=user_record + ) + text = bot.get_message( + 'ciclopi', 'button', 'limit_header', + update=update, user_record=user_record ).format( options='\n'.join( "- {symbol} {name}".format( @@ -1351,6 +1079,9 @@ async def _ciclopi_button_limit(bot, update, user_record, arguments): return result, text, reply_markup +# TODO: Multilanguage support from this line + + async def _ciclopi_button_show(bot, update, user_record, arguments): result, text, reply_markup = '', '', None fake_update = update['message'] @@ -1793,7 +1524,8 @@ async def _ciclopi_button(bot, update, user_record): return result -def init(bot, ciclopi_messages=None, _default_location=(43.718518, 10.402165)): +def init(bot, ciclopi_messages=None, ciclopi_messages_json=None, + _default_location=(43.718518, 10.402165)): """Take a bot and assign CicloPi-related commands to it. `ciclopi_messages` : dict @@ -1836,7 +1568,10 @@ def init(bot, ciclopi_messages=None, _default_location=(43.718518, 10.402165)): ) if ciclopi_messages is None: - ciclopi_messages = default_ciclopi_messages + try: + from .messages import default_ciclopi_messages as ciclopi_messages + except ImportError: + ciclopi_messages = {} bot.messages['ciclopi'] = ciclopi_messages @bot.command(command='/ciclopi', aliases=["CicloPi 🚲", "🚲 CicloPi 🔴"], diff --git a/ciclopibot/messages.py b/ciclopibot/messages.py new file mode 100644 index 0000000..cecc157 --- /dev/null +++ b/ciclopibot/messages.py @@ -0,0 +1,318 @@ +"""Default messages for bot functions.""" + +default_ciclopi_messages = { + 'command': { + 'description': { + 'en': "CiloPi stations status", + 'it': "Stato delle stazioni CicloPi", + }, + 'reply_keyboard_button': { + 'en': "CicloPi 🚲", + 'it': "CicloPi 🚲", + }, + 'updating': { + 'en': "Updating", + 'it': "Aggiornamento in corso", + }, + 'unavailable_website': { + 'en': "CicloPi's website cannot be reached at the moment.\n" + "Please retry later :/", + 'it': "Il sito del CicloPi è momentaneamente irraggiungibile.\n" + "Riprova tra un po' :/", + }, + 'no_station_available': { + 'en': "No station available", + 'it': "Nessuna stazione", + }, + 'title': { + 'en': "CicloPi stations", + 'it': "Stazioni CicloPi", + }, + 'buttons': { + 'all': { + 'en': "All", + 'it': "Tutte", + }, + 'only_fav': { + 'en': "Favourites only", + 'it': "Solo preferite", + }, + 'first_n': { + 'en': "First {n}", + 'it': "Prime {n}", + }, + 'update': { + 'en': "🔄 Update", + 'it': "🔄 Aggiorna", + }, + 'legend': { + 'en': "📜 Legend", + 'it': "📜 Legenda", + }, + 'settings': { + 'en': "⚙️ Settings", + 'it': "⚙️ Impostazioni", + }, + } + }, + 'settings': { + 'sort': { + 'name': { + 'en': "Order", + 'it': "Ordina" + }, + 'description': { + 'en': "customize CicloPi stations display order.", + 'it': "scegli in che ordine visualizzare le stazioni CicloPi." + }, + 'symbol': { + 'en': "⏬", + 'it': "⏬" + } + }, + 'limit': { + 'name': { + 'en': "Number of stations", + 'it': "Numero di stazioni" + }, + 'description': { + 'en': "choose how many stations you want to view.", + 'it': "scegli quante stazioni visualizzare." + }, + 'symbol': { + 'en': "#️⃣", + 'it': "#️⃣" + } + }, + 'fav': { + 'name': { + 'en': "Favourite stations", + 'it': "Stazioni preferite" + }, + 'description': { + 'en': "edit favourite stations.", + 'it': "cambia le tue stazioni preferite." + }, + 'symbol': { + 'en': "⭐️", + 'it': "⭐️" + } + }, + 'setpos': { + 'name': { + 'en': "Set location", + 'it': "Cambia posizione", + }, + 'description': { + 'en': "set a location from which stations may be sorted by " + "distance.", + 'it': "imposta una posizione da cui ordinare le stazioni per " + "distanza." + }, + 'symbol': { + 'en': "🧭", + 'it': "🧭" + } + } + }, + 'sorting': { + 'center': { + 'name': { + 'en': "City center", + 'it': "Borgo" + }, + 'description': { + 'en': "sorted by distance from city center (Borgo Stretto " + " station.)", + 'it': "in ordine di distanza crescente da Borgo Stretto." + }, + 'short_description': { + 'en': "by distance from city center", + 'it': "per distanza da Borgo Stretto" + } + }, + 'alphabetical': { + 'name': { + 'en': "Alphabetical", + 'it': "Alfabetico" + }, + 'description': { + 'en': "in alphabetical order.", + 'it': "in ordine alfabetico." + }, + 'short_description': { + 'en': "by name", + 'it': "per nome" + } + }, + 'position': { + 'name': { + 'en': "Position", + 'it': "Posizione" + }, + 'description': { + 'en': "sorted by distance from last set position. " + "City center position is set by default.", + 'it': "in ordine di distanza crescente dall'ultima posizione " + "inviata. Di default sarà Borgo Stretto." + }, + 'short_description': { + 'en': "by distance", + 'it': "per distanza" + } + }, + 'custom': { + 'name': { + 'en': "Favourites", + 'it': "Preferite" + }, + 'description': { + 'en': "sorted by custom order.", + 'it': "nell'ordine che hai scelto." + }, + 'short_description': { + 'en': "customly ordered", + 'it': "in ordine personalizzato" + }, + } + }, + 'filters': { + 'fav': { + 'name': { + 'en': "Only favourite stations", + 'it': "Solo le preferite" + }, + 'all': { + 'en': "favourite stations first", + 'it': "prima le preferite" + }, + 'only': { + 'en': "only favourite stations", + 'it': "solo le preferite" + } + }, + 'num': { + 'en': "first {n}", + 'it': "prime {n}" + }, + 'all': { + 'name': { + 'en': "All stations", + 'it': "Tutte" + }, + }, + '3': { + 'name': { + 'en': "3", + 'it': "3" + }, + }, + '5': { + 'name': { + 'en': "5", + 'it': "5" + }, + }, + '10': { + 'name': { + 'en': "10", + 'it': "10" + }, + } + }, + 'status': { + 'not_available': { + 'en': "Not available", + 'it': "Non disponibile" + } + }, + 'set_position': { + 'success': { + 'en': "Position set!\n" + "From now on, stations will be sorted by distance from " + "this position.", + 'it': "Ho salvato questa posizione!\n" + "D'ora in poi ordinerò le stazioni dalla più vicina alla " + "più lontana da qui." + }, + 'cancel': { + 'en': "Operation cancelled.", + 'it': "Operazione annullata." + }, + 'cancel_and_remind': { + 'en': "I could not understand your position.\n" + "Try again with /ciclopi > Settings > Set location", + 'it': "Non ho capito la tua posizione.\n" + "Per riprovare fai " + "/ciclopi > Impostazioni > Cambia posizione" + } + }, + 'button': { + 'title': { + 'en': "CicloPi Settings", + 'it': "Impostazioni CicloPi" + }, + 'back_to_settings': { + 'en': "Back to settings", + 'it': "Torna alle impostazioni" + }, + 'back_to_stations': { + 'en': "Back to stations", + 'it': "Torna alle stazioni" + }, + 'legend': { + 'name': { + 'en': "Station name", + 'it': "Nome della stazione", + }, + 'distance': { + 'en': "Distance in meters", + 'it': "Distanza in m", + }, + 'description': { + 'en': "Station address", + 'it': "Indirizzo della stazione", + }, + 'bikes': { + 'en': "Available bikes", + 'it': "Bici disponibili", + }, + 'free': { + 'en': "Free parking stalls", + 'it': "Posti liberi", + } + }, + 'no_change': { + 'en': "No change detected!", + 'it': "È già così!", + }, + 'unknown_option': { + 'en': "Unknown option!", + 'it': "Opzione sconosciuta!", + }, + 'done': { + 'en': "Done!", + 'it': "Fatto!", + }, + 'sorting_header': { + 'en': "📜 CicloPi stations display order 🚲\n\n" + "{{options}}\n\n" + "Choose a mode or go back to stations list using the " + "buttons.", + 'it': "📜 Ordine di visualizzazione delle stazioni CicloPi 🚲\n\n" + "{{options}}\n\n" + "Scegli una nuova modalità o torna all'elenco delle " + "stazioni usando i bottoni." + }, + 'limit_header': { + 'en': "📜 Number of CicloPi stations to show 🚲\n\n" + "{{options}}\n\n" + "Choose a mode or go back to stations list using the " + "buttons.", + 'it': "📜 Numero di stazioni CicloPi da mostrare 🚲\n\n" + "{{options}}\n\n" + "Scegli una nuova modalità o torna all'elenco delle " + "stazioni usando i bottoni." + }, + } +}