Created
May 31, 2018 10:10
-
-
Save VivienGiraud/ce457b1c67db904ee344f55b46d14721 to your computer and use it in GitHub Desktop.
Petit script permettant de notifier votre téléphone (réseau Free) d'un sms !
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
from sys import exit, argv | |
from os import environ | |
try: | |
import requests | |
except Exception as e: | |
print("Need to install requests -> pip3 install requests") | |
print(e) | |
exit(-1) | |
try: | |
import logging | |
except Exception as e: | |
print("Need to install logging -> pip3 install logging") | |
print(e) | |
exit(-1) | |
# Get logger | |
logging.basicConfig(level=logging.INFO, | |
format='%(asctime)s %(levelname)-8s %(filename)s:%(lineno)-12d %(message)s') | |
logger = logging.getLogger(__name__) | |
if len(argv) != 2: | |
if len(argv) > 3: | |
logger.critical("Votre message doit être entre guillemet !") | |
else: | |
logger.critical("Vous devez entrer un message !") | |
exit(-1) | |
FREE_URL = "https://smsapi.free-mobile.fr/sendmsg" | |
payload = { | |
'user': environ.get('FREE_USER'), | |
'pass': environ.get('FREE_KEY'), | |
'msg': argv[1] | |
} | |
if payload['user'] is None: | |
logger.critical("Vous devez configurer la variable d'envirronement FREE_USER avec votre numéro d'utilisateur fourni par FREE") | |
exit(-1) | |
elif payload['pass'] is None: | |
logger.critical("Vous devez configurer la variable d'envirronement FREE_KEY avec votre clé d'utilisateur fourni par FREE") | |
exit(-1) | |
r = requests.get(FREE_URL, params=payload) | |
if r.status_code == 200: | |
logger.info("Le SMS a été envoyé sur votre mobile !") | |
elif r.status_code == 400: | |
logger.error("Un des paramètres obligatoires est manquant !") | |
logger.error(payload) | |
elif r.status_code == 402: | |
logger.error("Trop de SMS ont été envoyés en trop peu de temps !") | |
elif r.status_code == 403: | |
logger.error("Le service n'est pas activé sur l'espace abonné, ou login / clé incorrect !") | |
elif r.status_code == 500: | |
logger.error("Erreur côté serveur. Veuillez réessayer ultérieurement !") | |
else: | |
logger.critical("Erreur inconnue !") | |
logger.critical("Code : {}".format(r.status_code)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment