Skip to content

Instantly share code, notes, and snippets.

@XaviFortes
Last active June 12, 2023 10:46
Show Gist options
  • Save XaviFortes/008cf65e53e157e06ede7f6c4a5a9a65 to your computer and use it in GitHub Desktop.
Save XaviFortes/008cf65e53e157e06ede7f6c4a5a9a65 to your computer and use it in GitHub Desktop.
If a new thing appeared on history, it sends you a message via telegram

Welcome!

In case you don't see the python code click here

Installation

You just need python 3.X and you may need to run pip3 install requests to install the necessary lib to make http requests.

How to run

To be able to run this, you need to create a bot in telegram, then get the api key from the bot and get your chat id, it's like your used id.

After that edit the file and change the variables.

And now you are able to run the script 24/7 by leaving it running.

If you are on linux there's a thing called Screens!

I do create a screen with this command screen -S nameOfScreen for example screen -S glsTracker It should automatically put you inside the screen, if not run screen -r nameOfScreen if it seems to not be picking it up run screen -ls to view all screens.

Now execute the script with python gls-tracker.py and leave the screen with Ctrl + A, now press the key D.

You should be out now.

import requests
import json
import time
# Set your tracker number, API key, and chat ID
tracker_number = 'your_gls_tracker_id'
api_key = 'your_api_key_here'
chat_id = 'your_chat_id_here'
def check_history():
# Retrieve the response from the API
url = f'https://gls-group.eu/app/service/open/rest/GROUP/en/rstt001?match={tracker_number}'
response = requests.get(url)
data = response.json()
# Check if any new item is added to the history
previous_history = [] # Store the previous history
# Load the previous history from a file or a database
try:
with open('previous_history.json', 'r') as file:
previous_history = json.load(file)
except FileNotFoundError:
pass
new_items_added = False
for item in data['tuStatus'][0]['history']:
if item not in previous_history:
new_items_added = True
# Send a message to Telegram
message = f"New item added to history:\nDate: {item['date']}\nTime: {item['time']}\nEvent: {item['evtDscr']}"
telegram_url = f"https://api.telegram.org/bot{api_key}/sendMessage"
requests.post(telegram_url, params={"chat_id": chat_id, "text": message})
# Save the current history for future comparisons
with open('previous_history.json', 'w') as file:
json.dump(data['tuStatus'][0]['history'], file)
if new_items_added:
print("New items added to history. Message sent to Telegram.")
else:
print("No new items added to history.")
# Run the script every 15 minutes
while True:
check_history()
print("Waiting 900 seconds (15 minutes)")
time.sleep(900) # Delay for 15 minutes (900 seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment