bbf2835341f626dfd6eda40d78e2a75a774eb73f — Enrico Lovisotto 3 years ago 7188272
1 files changed, 64 insertions(+), 0 deletions(-)

A README.org => README.org +64 -0
@@ 0,0 1,64 @@
#+title: Trenitalia notifications
#+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/' + \

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


** 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)
     - 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
   command=curl "https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat_id>&text=<message>"

* 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]].