Compare commits

...

6 Commits

4 changed files with 48 additions and 30 deletions

View File

@ -3,10 +3,10 @@
__author__ = "Davide Testa"
__email__ = "davide@davte.it"
__license__ = "GNU General Public License v3.0"
__version__ = "1.2.3"
__version__ = "1.2.6"
__maintainer__ = "Davide Testa"
__contact__ = "t.me/davte"
from . import ciclopi
from . import ciclopi, messages
__all__ = [ciclopi]
__all__ = ['ciclopi', 'messages']

View File

@ -8,6 +8,7 @@ import asyncio
import datetime
import inspect
import math
from collections import OrderedDict
# Third party modules
from typing import Union
@ -527,7 +528,9 @@ def _get_stations(data, location):
return stations
async def set_ciclopi_location(bot, update, user_record):
async def set_ciclopi_location(bot: davtelepot.bot.Bot,
update: dict, user_record: OrderedDict,
language: str):
"""Take a location update and store it as CicloPi place.
CicloPi stations will be sorted by distance from this place.
@ -553,7 +556,8 @@ async def set_ciclopi_location(bot, update, user_record):
)
# Remove individual text message handler which was set to catch `/cancel`
bot.remove_individual_text_message_handler(telegram_id)
return await _ciclopi_command(bot, update, user_record)
return await _ciclopi_command(bot=bot, update=update,
user_record=user_record, language=language)
async def cancel_ciclopi_location(bot, update, user_record):
@ -577,21 +581,16 @@ async def cancel_ciclopi_location(bot, update, user_record):
)
# The service is currently suspended: code is unreachable of course
# noinspection PyUnreachableCode,PyUnusedLocal
async def _ciclopi_command(bot: davtelepot.bot.Bot, update, user_record, sent_message=None,
async def _ciclopi_command(bot: davtelepot.bot.Bot, update: dict,
user_record: OrderedDict,
language: str,
sent_message=None,
show_all=False):
if ('ciclopi' not in bot.shared_data
or 'is_working' not in bot.shared_data['ciclopi']
or not bot.shared_data['ciclopi']['is_working']):
return {
'text': {
'it': "⚠️ Il servizio è momentaneamente sospeso a causa dell'emergenza COVID-19🦠\n"
"#stiamoacasa 🏠",
'en': "⚠️ The service is currently suspended due to COVID-19 emergency.🦠\n"
"#stayathome 🏠"
}
}
return bot.get_message('ciclopi', 'service_unavailable',
language=language)
chat_id = update['chat']['id']
default_stations_to_show = 5
stations = []
@ -1102,7 +1101,9 @@ async def _ciclopi_button_limit(bot, update, user_record, arguments):
return result, text, reply_markup
async def _ciclopi_button_show(bot, update, user_record, arguments):
async def _ciclopi_button_show(bot: davtelepot.bot.Bot, update: dict,
user_record: OrderedDict, language: str,
arguments: list):
result, text, reply_markup = '', '', None
fake_update = update['message']
fake_update['from'] = update['from']
@ -1115,7 +1116,8 @@ async def _ciclopi_button_show(bot, update, user_record, arguments):
show_all=(
True if len(arguments) == 1 and arguments[0] == 'all'
else False
)
),
language=language
)
)
return result, text, reply_markup
@ -1477,7 +1479,7 @@ async def _ciclopi_button_favourites(bot, update, user_record, arguments):
return result, text, reply_markup
async def _ciclopi_button_setpos(bot, update, user_record):
async def _ciclopi_button_setpos(bot, update, user_record, language):
result, text, reply_markup = '', '', None
chat_id = (
update['message']['chat']['id'] if 'message' in update
@ -1490,7 +1492,8 @@ async def _ciclopi_button_setpos(bot, update, user_record):
)
bot.set_individual_location_handler(
await async_wrapper(
set_ciclopi_location
set_ciclopi_location,
language=language
),
update
)
@ -1544,7 +1547,9 @@ _ciclopi_button_routing_table = {
}
async def _ciclopi_button(bot, update, user_record, data):
async def _ciclopi_button(bot: davtelepot.bot.Bot, update: dict,
user_record: OrderedDict, language: str,
data: list):
command, *arguments = data
if command in _ciclopi_button_routing_table:
handler = _ciclopi_button_routing_table[command]
@ -1553,6 +1558,7 @@ async def _ciclopi_button(bot, update, user_record, data):
for name, value in {'bot': bot,
'update': update,
'user_record': user_record,
'language': language,
'arguments': arguments
}.items()
if name in inspect.signature(handler).parameters
@ -1578,6 +1584,7 @@ async def check_service_status(bot: davtelepot.bot.Bot,
Store service status in `bot.shared_data['ciclopi']`.
"""
# TODO: adapt interval to events (check 1 minute after first
if isinstance(interval, datetime.timedelta):
interval = interval.total_seconds()
while 1:
@ -1658,9 +1665,15 @@ def init(telegram_bot: davtelepot.bot.Bot, ciclopi_messages=None,
),
help_section=telegram_bot.messages['ciclopi']['help'],
authorization_level='everybody')
async def ciclopi_command(bot, update, user_record):
return await _ciclopi_command(bot, update, user_record)
async def ciclopi_command(bot: davtelepot.bot.Bot, update: dict,
user_record: OrderedDict, language: str):
return await _ciclopi_command(bot=bot, update=update,
user_record=user_record, language=language)
@telegram_bot.button(prefix='ciclopi:///', separator='|', authorization_level='everybody')
async def ciclopi_button(bot, update, user_record, data):
return await _ciclopi_button(bot=bot, update=update, user_record=user_record, data=data)
async def ciclopi_button(bot: davtelepot.bot.Bot, update: dict,
user_record: OrderedDict, language: str,
data: list):
return await _ciclopi_button(bot=bot, update=update,
user_record=user_record, language=language,
data=data)

View File

@ -449,7 +449,11 @@ default_ciclopi_messages = {
'it': "Annulla",
},
},
}
},
'service_unavailable': {
'it': "⚠ Il servizio è momentaneamente sospeso, riprova più tardi! ⚠",
'en': "⚠ The service is currently unavailable, try again later! ⚠"
},
}
default_help_messages = {

View File

@ -7,10 +7,10 @@ configuration_file="$this_script_directory/my_config.sh";
passwords_file="$this_script_directory/ciclopibot/data/passwords.py";
# Create intermediate path for passwords_file, if it does not exist
mkdir -r "$this_script_directory/ciclopibot/data/";
mkdir -p "$this_script_directory/ciclopibot/data/";
read -r "Enter a name for your virtual environment
" venv_name;
echo "Enter a name for your virtual environment";
read venv_name;
python3 -m venv "$venv_name";
"$venv_name"/bin/pip install -r "$this_script_directory/requirements.txt";
@ -18,7 +18,8 @@ python3 -m venv "$venv_name";
# Other systems may require a different path.
echo "python_virtual_environment=\"$(pwd)/$venv_name/bin\";" >> "$configuration_file";
echo "python_script=\"$this_script_directory/ciclopibot/bot.py\";" >> "$configuration_file";
read -r "Enter a valid Telegram bot token " bot_token;
echo "Enter a valid Telegram bot token";
read bot_token;
echo "bot_token = \"$bot_token\"" >> "$passwords_file";
# Run bot