-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathseven.py
More file actions
108 lines (87 loc) · 2.54 KB
/
seven.py
File metadata and controls
108 lines (87 loc) · 2.54 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/env python
import argparse
import json
import sys
from ssl import SSLContext, PROTOCOL_SSLv23
requiredArgs = {
'api_key': 'seven.io API key',
'to': 'Receiver(s) separated by comma',
'text': 'Message text'
}
def bool_arg(obj):
return dict(obj, **{
'action': 'store_const',
'const': 1,
})
optionalArgs = {
'delay': {
'help': 'Dispatch at yyyy-mm-dd hh:ii or timestamp'
},
'flash': bool_arg({
'help': 'Show directly on the recipients display'
}),
'foreign_id': {
'help': 'Custom foreign id'
},
'from': {
'help': 'Sender identifier',
},
'json': bool_arg({
'help': 'Return JSON'
}),
'label': {
'help': 'Custom label'
},
'performance_tracking': bool_arg({
'help': 'Enable performance tracking'
}),
'ttl': {
'help': 'Custom time to live',
'type': int
},
'udh': {
'help': 'Custom user data header'
},
}
parser = argparse.ArgumentParser()
parser.add_argument('api_key', help=requiredArgs['api_key'])
parser.add_argument('to', help=requiredArgs['to'])
parser.add_argument('text', help=requiredArgs['text'])
for k in optionalArgs:
parser.add_argument('--%s' % k, **optionalArgs[k])
def terminate(msg, status_code):
print(msg)
sys.exit(status_code)
if __name__ == '__main__':
version = int(sys.version[:1])
if 2 != version and 3 != version:
terminate('Unsupported python version %d' % version, 2)
try:
args = vars(parser.parse_args())
except argparse.ArgumentError as e:
terminate(e.message, 3)
for k in requiredArgs:
if not len(requiredArgs[k]):
terminate('Argument %s must be lengthy!' % k, 3)
headers = {
'Accept': 'application/json',
'Authorization': 'Basic %s' % args.pop('api_key'),
'SentWith': 'Zabbix'
}
url = 'https://gateway.seven.io/api/sms'
ssl_context = SSLContext(PROTOCOL_SSLv23)
if 2 == version:
from urllib import urlencode
from urllib2 import Request, urlopen
req = Request(url, urlencode(args), headers)
res = urlopen(req, context=ssl_context).read()
else:
from urllib.request import Request, urlopen
from urllib.parse import urlencode
req = urlopen(
Request(url, method='POST', headers=headers, data=urlencode(args).encode()),
context=ssl_context)
res = req.read()
res = json.loads(res)
code = res['success']
terminate(res, int(100 != int(code)))