~lobisquit/personal-website

551a0903f3839d29808c23d0baec0ecbf50ea4ec — Enrico Lovisotto 3 years ago 0c12b8f
Wrong README committed: fixed
1 files changed, 2 insertions(+), 61 deletions(-)

M README.org
M README.org => README.org +2 -61
@@ 1,64 1,5 @@
#+title: Trenitalia notifications
#+title: Personal website
#+startup: indent
#+options: toc:nil

The app uses the Viaggiatreno Trenitalia API to alerts users for
delays and train cancellations.

* Proof of concept
  The app is written in Rust and implements this sample scripts in a
  more strucuted way.

** Python script
   #+begin_src python
     import json

     import requests

     # hard coded constants
     VENEZIA_SL_CODE = 'S02593'
     TRAIN_CODE = '16306'

     URL = 'http://www.viaggiatreno.it/viaggiatrenonew/resteasy/viaggiatreno/' + \
	 f'tratteCanvas/{VENEZIA_SL_CODE}/{TRAIN_CODE}'

     # obtain information regarding delays
     stops = json.loads(requests.get(URL).content)
     delays = {item['fermata']['stazione']: item['fermata']['ritardo']
		       for item in stops}

     print(delays)
   #+end_src

** Send notification
   See [[https://gist.github.com/dlaptev/7f1512ee80b7e511b0435d3ba95d88cc#file-push_notifications_to_telegram-md][here]].

   - Create a new Telegram bot:
     - start a chat with [[https://web.telegram.org/#/im?p=@BotFather][botfather]];
     - type `/newbot`, select a name (to be shown in chats) and handle for your bot;
     - note the bot token to access HTTP API - a long string with a colon in the middle (later referred to as `<token>`);
     - optionally `/setdescription` and `/setuserpic`.
   - Add the bot to the chat/channel and note its id:
     - add the bot to a new or existing chat or group - this is where your bot will send notifications to;
     - go to `https://api.telegram.org/bot<token>/getUpdates` (replace `<token>` with your token);
     - within the `chat` part, find and note the `id` field - a positive or negative number (later referred to as `<chat_id>`).
   - Programmatically send notifications:
     - Essentially, you just need to construct a special URL and open it;
     - For example, this can be done like this in Python (replace `<token>`, `<chat_id>` and `<message>`):
       #+begin_src python
	 import urllib, requests
	 url = 'https://api.telegram.org/bot%s/sendMessage?chat_id=%s&text=%s' % (
	     '<token>', '<chat_id>', urllib.quote_plus('<message>'))
	 _ = requests.get(url, timeout=10)
       #+end_src
     - For options, refer to [the official documentation](https://core.telegram.org/bots/api#available-methods).

   To use telegram notifications with `supervisor`, modify `/etc/supervisor/supervisord.conf` to include the following lines ([possible events](http://supervisord.org/events.html)):
   #+begin_src python
   [eventlistener:telegramnotitfactor]
   command=curl "https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat_id>&text=<message>"
   events=PROCESS_STATE_STARTING
   #end_src

* Heroku app
  Already configured using [[https://federicojordan.github.io/blog/2020/09/22/simplertapp-how-to-use-heroku-scheduler-to-run-cron-jobs-3ff1c0c1e1f7.html][Heroku scheduler]] in a related [[https://dashboard.heroku.com/apps/notifica-ritardi-treno/][app]].
Generator to publish a personal website on Gitlab/Github CI/CD.