Skip to content

Commit

Permalink
add gotify as a new notification provider
Browse files Browse the repository at this point in the history
  • Loading branch information
mek-x committed Mar 25, 2023
1 parent cbd928e commit aed658c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
42 changes: 39 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ medihunter find-appointment -s 9 -c 174 -c 49088
a co jeśli chcemy znaleźć wizytę u dowolnego ortopedy w Trójmieście w przeciągu następnych dwóch tygodni a nie za miesiąc? **bez daty rozpoczęcia nie działa**

```bash
medihunter find-appointment -s 163 -d 2021-12-19 -f 2022-12-31 -r 200
medihunter find-appointment -s 163 -d 2021-12-19 -f 2022-12-31 -r 200
```

Lub można dodać bezpośrednio do Crontaba jak poniżej **wpis w cronie działa tylko z medicover_pushover.py**
Expand Down Expand Up @@ -164,7 +164,7 @@ opcja|domyślna wartość
--pushover_user|brak, Pushover user Token
--pushover_device|brak, None nazwa device w Pushover domyślnie pusta=wszystkie
--pushover_msgtitle|brak - prefix dodawany przed tytułem powiadomienia
-t, --notification-title|brak, dostępna tylko w medihunter.py, wspierana tylko przez Pushover i Telegram
-t, --notification-title|brak, dostępna tylko w medihunter.py, wspierana tylko przez Pushover, Telegram i Gotify

## Pushbullet w medihunter.py

Expand Down Expand Up @@ -274,6 +274,42 @@ Teraz możemy wyszukać wizyty i otrzymać notyfikacje poprzez XMPP:
medihunter find-appointment -r 204 -s 4798 --user 00000 --password psw1234 -i 1 -d 2019-05-22 -n xmpp
```

## Gotify w medihunter.py

[Gotify](https://gotify.net/) jest prostym serwisem do wysyłania powiadomień typu push głównie na urządzenia z systemem Android.
Różni się od innych tego typu aplikacji, że jest w pełni open source oraz do przeznaczony do hostowania na własnym serwerze.

W celu skorzystania z tej metody powiadomień musimy ustawić kilka zmiennych środowiskowych:

```shell
# bash
export GOTIFY_TOKEN=AbCDeFg2yvapfPA
export GOTIFY_HOST=https://gotify.my-server.com
```

lub

```shell
# fish
set -x GOTIFY_TOKEN 'AbCDeFg2yvapfPA'
set -x GOTIFY_HOST 'https://gotify.my-server.com'
```

lub w Windows z venv
```shell
set GOTIFY_TOKEN=AbCDeFg2yvapfPA
set GOTIFY_HOST=https://gotify.my-server.com
```

Istnieje również opcjonalna zmienna `GOTIFY_PRIORITY` która pozwala na ustawienie priorytetu powiadomienia (domyślnie `5`).

Teraz możemy wyszukać wizyty i otrzymać notyfikacje poprzez Gotify:

```shell
medihunter find-appointment -r 204 -s 4798 --user 00000 --password psw1234 -i 1 -d 2019-05-22 -n gotify
```


## Docker
Aby uruchomić aplikację w kontenerze należy w pierwszej kolejności zbudować obraz

Expand All @@ -291,4 +327,4 @@ Możliwe jest także użycie zmiennych środowiskowych lub pliku .env
docker run -it -e MEDICOVER_USER=00000 -e MEDICOVER_PASS=psw1234 medihunter find-appointment -r 204 -s 4798d

docker run -it --env-file=.env medihunter find-appointment -r 204 -s 4798
```
```
8 changes: 5 additions & 3 deletions medihunter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
Appointment,
MedicoverSession,
)
from medihunter_notifiers import pushbullet_notify, pushover_notify, telegram_notify, xmpp_notify
from medihunter_notifiers import pushbullet_notify, pushover_notify, telegram_notify, xmpp_notify, gotify_notify

load_dotenv()
now = datetime.now()
Expand Down Expand Up @@ -54,6 +54,8 @@ def notify_external_device(message: str, notifier: str, **kwargs):
telegram_notify(message, title)
elif notifier == "xmpp":
xmpp_notify(message)
elif notifier == "gotify":
gotify_notify(message, title)

def process_appointments(
appointments: List[Appointment], iteration_counter: int, notifier: str, **kwargs
Expand All @@ -74,7 +76,7 @@ def process_appointments(
if duplicate_checker(appointment):
echo_appointment(appointment)
notification_message += f"{appointment.appointment_datetime} {appointment.doctor_name} {appointment.clinic_name}" +(" (Telefonicznie)\n" if appointment.is_phone_consultation else " (Stacjonarnie)\n")

if notification_message:
notification_title = kwargs.get("notification_title")
notify_external_device(
Expand Down Expand Up @@ -118,7 +120,7 @@ def validate_arguments(**kwargs) -> bool:
@click.option("--service", "-e", default=-1)
@click.option("--interval", "-i", default=0, show_default=True, help='Checking interval in minutes')
@click.option("--days-ahead", "-j", default=1, show_default=True)
@click.option("--enable-notifier", "-n", type=click.Choice(["pushbullet", "pushover", "telegram", "xmpp"]))
@click.option("--enable-notifier", "-n", type=click.Choice(["pushbullet", "pushover", "telegram", "xmpp", "gotify"]))
@click.option("--notification-title", "-t")
@click.option("--user", prompt=True, envvar='MEDICOVER_USER')
@click.password_option(confirmation_prompt=False, envvar='MEDICOVER_PASS')
Expand Down
28 changes: 28 additions & 0 deletions medihunter_notifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from notifiers.exceptions import BadArguments
from os import environ
from xmpp import *
import requests

pushbullet = get_notifier('pushbullet')
pushover = get_notifier('pushover')
Expand Down Expand Up @@ -66,3 +67,30 @@ def xmpp_notify(message):
except KeyError as e:
print(f'XMPP notifications require NOTIFIERS_XMPP_JID, NOTIFIERS_XMPP_PASSWORD'
f' and NOTIFIERS_XMPP_RECEIVER to be exported. Detailed exception:\n{e}')

def gotify_notify(message, title: str = None):
try:
host = environ['GOTIFY_HOST']
token = environ['GOTIFY_TOKEN']
except KeyError as e:
print(f'GOTIFY notifications require GOTIFY_HOST (base url with port),'
f' GOTIFY_TOKEN to be exported. Detailed exception:\n{e}')
return

try:
prio = int(environ['GOTIFY_PRIORITY'])
except (KeyError, ValueError):
prio = 5

if title is None:
title = "medihunter"

try:
resp = requests.post(host+'/message?token='+token, json={
"message": message,
"priority": int(prio),
"title": title
})

except requests.exceptions.RequestException as e:
print(f'GOTIFY notification failed:\n{e}')

0 comments on commit aed658c

Please sign in to comment.