-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickatell.py
42 lines (34 loc) · 1.27 KB
/
clickatell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import requests
import global_vars
from yaml_config import config
import logging
import colorlog
logger = colorlog.getLogger(__name__)
logger.addHandler(global_vars.file_handler)
logger.addHandler(global_vars.handler)
logger.setLevel(global_vars.log_level)
# API url is fixed for clickatell, so not much point in putting in config
api_url = "https://api.clickatell.com/rest/message"
def send_sms(dest_list, message_str):
headers = {}
headers["content-type"] = "application/json"
headers["X-Version"] = "1"
headers["Authorization"] = "Bearer " + config['clickatell']['api_key']
payload = {}
payload["to"] = dest_list
payload["from"] = config['clickatell']['sender_name']
payload["text"] = message_str
# payload["type"] = "flash" #Probably don't want flash, but maybe in the future?
try:
r = requests.post(api_url, headers=headers, json=payload)
if r.status_code == 202:
for number in dest_list:
logger.warning("SMS sent to " + number)
return True
else:
for number in dest_list:
logger.error("Failed to send SMS to: " + number)
return False
except Exception as e:
logger.error("Clickatell sms failed: " + str(e))
return False