How to automate UK bank holiday timetable
This Article explains how to automate UK bank holiday timetable.
Created: July 2024
Updated: August 2024
Permalink: https://wildix.atlassian.net/wiki/x/uBbOAQ
Scenario
A business may be closed on bank holidays and require the phone lines to go to an out-of-hours service on these dates. Bank holiday dates in the UK are not always on the same dates each year and normally require configuring each time and may be forgotten about or left until the last minute.
Solution
Each country within the United Kingdom has different bank holiday dates. They are split into the following groupings: England and Wales, Scotland, Northern Ireland. You need to select the relevant region you require in the script.
Using the Gov.UK website, you can automatically import bank holiday dates for each region of the UK, this eliminates the need to manually program the dates each year.
Step 1: Create a new timetable
- Go to WMS -> Dialplan -> Timetable / Switch
- Click +Add to create a new timetable, for this example we are going to use the title bank-holidays with the selected state check time:
- Once created, make note of the ID of the timetable as you will need this later:
Step 2: Create Simple Token
Next, you need to create a Simple Token to authenticate the API to be used.
- Go to WMS -> PBX -> Integrations -> Applications
- Select Simple Token, click +Add, give it a name and select the admin user:
- Click Save to get your secret key, which is the Simple/Bearer Token you will need to use:
With the previous steps completed, you can now edit the Python script that will be importing the dates from Gov.UK website.
- Download the script and open it in a text editor of your choice (Notepad, Notepad++, Microsoft Visual Studio Code etc): get_bankholidays.py
- You need to edit multiple lines in the script using the information from steps 1 and 2 and define which region you wish to pull dates for
- The region you want to pull dates for needs to be defined within the script. The options available are:
england-and-wales
scotland
northern-ireland
Setting the required region can be found on line 5 of the script. This is case-sensitive, so match it to the values in the comment. On line 8, you can define whether you want to include the date for St Andrew's Day or not, this is only applied if you have set “scotland” on line 5. Setting the value to True will include the date, False does not:
- Next, you have a set of variables to define. These start on line 13 in the script are the following:
- BEARER_TOKEN (simple token)
- TIMETABLE_ID
- TIMETABLE_NAME
PBX_NAME
# Variables BEARER_TOKEN = "your simple token" TIMETABLE_ID = 1 TIMETABLE_NAME = "name of timetable, case-sensitive" PBX_NAME = "name of the PBX (the part before .wildixin.com) e.g: telecomcorp"
- The comments on each line in the script offer hints on how to correctly define the variables. The timetable ID should only be the ID number, there is no requirement to wrap it within ““. The timetable name is case-sensitive, so if you called your timetable “Bank-holidays”, the B must be uppercase. The PBX name should only be the part before .wildixin.com, e.g. “telecomcorp”:
- Once you have defined the variables and set the region, save the file.
Step 4: Upload to PBX and set file permissions
The file now needs to be uploaded to the PBX. It is recommended that you upload it to the home directory of the admin user /home/admin/. To upload the script to the PBX, SSH must be enabled. You can then use an SFTP client of your choice (FileZilla, WinSCP, Cyberduck etc) or you can use SCP via terminal on your local machine. Remember the port used for SSH is 2222.
If using SCP via terminal on your local machine, you can use the following:
scp -P 2222 /local/file/path/get_bankholidays.py admin@PBXNAME.wildixin.com:/home/admin/
Once the script is uploaded, you need to make it executable. To do this, you need to change the file permissions while logged in as root user in terminal. To log in as root user in shell, run:
su Password: wildix
And then run:
chmod +x /home/admin/get_bankholidays.py
To verify the file permissions are set correctly, you can run the following:
ls -l /home/admin
- Find the file and, it should have -rwxr-xr-x set:
Step 5: First import: executing the script
In terminal logged in as root user, you can now run the script to do the initial import of the bank holiday dates into the timetable.
Run the following command:
/usr/bin/python3 /home/admin/get_bankholidays.py
This should return the following: API response:
{'type': 'result', 'result': 'Success'}
- If you now go back to check your timetable, you should see the dates populated:
Step 6: Automating the import: creating a crontask
To get the timetable to automatically load new bank holiday dates, you need to set up a cron task/job (scheduled task). It is at your discretion to how often you want the timetable to update. Bank holiday dates usually don’t change often, but there are occasions where additional ones may be added for special occasions such as coronations. This means you might want the frequency to be as often as monthly or weekly to ensure they are added.
Execute the script weekly, the following will run the script every Tuesday at 00:05:
05 0 * * 2 /usr/bin/python3 /home/admin/get_bankholidays.py
You can use the crontab.guru website to help you set the frequency you want the script to run.
Once a schedule is configured in the crontab, it will overwrite any manual changes made to the timetable each time it runs.
To add this task to the crontab, in terminal logged in as root user run the following:
crontab -e
If you are asked to select an editor, select nano or nano-tiny as these are the most straightforward to use
- Once you’re in the editor, it is a good idea to add a comment line to describe the purpose of the task, to do this start the line with #
- Then on a new line insert the string: