riban-bw / lone_working

Telegram messenger bot lone working service
GNU General Public License v3.0
0 stars 0 forks source link

riban lone working

Telegram messenger bot lone working service

About

There are risks involved when someone must work alone or as part of a team in dangerous locations. This system provides a mechanism to monitor such situations and reduce the risk of lone-working. Users start a session and receive notifications (by default every 30 minutes) to which they should respond. If a user does not respond, they receive further notifications (by default 3) at a different interval (by default 4 minutes). If they still do not respond then the list of users supervising the session will start to receive alerts at the repeat interval (by default 4 minutes). If the user acknowledges the notification then alerts cease. The user can end the session to stop notifications.

User Guide

There are two types of user:

There may be any quantity of monitored users and supervisors and each monitored user can select any quantity of available supervisors for their monitored session.

Monitored User Workflow

First time configuration

Supervisor Workflow

First time configuration

Hidden commands

There are some commands that are not in the menu:

Build

The system consists of two parts:

The Telegram bot is a simple bot, configured as described in the Telegram bot instructions. The bot's API token is required by the Python script. The only configuration for the bot is to add commands:

begin - Start a monitored user session
end - End a monitored user session
supervise - Start supervising
unsupervise - End supervising
sessions - List active sessions

The Python script (hosted in this git repository) needs to be run on an Internet connected host. It is tested on Debian 10 Bullseye on Raspberry Pi 4 but should work on most platforms supporting Python 3 and the dependencies.

Dependencies

The script is written in Python 3 and requires some Python modules as listed below. These may be provided as OS distribution packages or may require to be installed via PIP.

The teleport module version 12.7 does not handle some Telegram bot messages. The work around for this is to add 'my_chat_member' to the list of keys handled by the function relay_to_collector in init.py, e.g.:

        def relay_to_collector(update):
            key = _find_first_key(update, ['message',
                                           'edited_message',
                                           'channel_post',
                                           'edited_channel_post',
                                           'callback_query',
                                           'inline_query',
                                           'chosen_inline_result',
                                           'shipping_query',
                                           'pre_checkout_query',
                                           'my_chat_member'])
            collect_queue.put(update[key])
            return update['update_id']

on the Debian test machine this file is found here: /usr/local/lib/python3.9/dist-packages/telepot/__init__.py. This is a bit of a bodge that is prone to be overwritten by updates but it solves the problem.

Running the service

The Python script lone_working.py can be run without elevated rights. It must be given the Telegram bot API token either as a command line parameter or reading from a configuration file. By default it looks for a configuration file /etc/riban_lone_working.conf which is an ini style configuration file with one section `[Default]':

[Default]
API_TOKEN = "API_TOKEN_AS_A_STRIG" # The Telegram bot API token
NOTIFY_INTERVAL = 30 # Interval in minutes between user notifications
REPEAT_INTERVAL = 3 # Interval in minutes between notification repeats
ALERT_COUNT = 3 # Quantity of missed user notifications before alert sent to supervisor
SAVE_FILENAME = '/tmp/riban_loan_working.json'

Command line parameters in short or long form can be provided for the following options:

-t --api_token : Telegram API token
-c --config : Full path and filename of configuration file [Default: /etc/riban_lone_working.conf]
-n, --notify_interval : Interval (in minutes) between user notifications [Default: 30]
-r, --repeat_interval : Interval (in minutes) beteween notification repeat [Default: 4]
-a, --alert_count : Quantity of notifications before an alert is sent [Default: 3]
-s, --save_filename : Full path and filename of persistent state save file [Default: /tmp/riban_loan_working.json]

If the service is stopped, e.g. with ctrl+c, it saves its current list of users, supervisors and sessions. When it restarts it restores these.

Defects and enhancements

Please use issue tracker to report bugs an feature requests. Feel free to submit pull requests to resolve any bugs or implement new features. Add comments to the issue tracker to advance the discussion of such issues. All known issues have been entered into the issue tracker.