-
Notifications
You must be signed in to change notification settings - Fork 0
/
twiliohelper.py
82 lines (75 loc) · 2.79 KB
/
twiliohelper.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
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
#!/usr/bin/python
import confighelper
import datetime
import jirahelpers
import logging
import twilio
module = 'twiliohelper'
logger = logging.getLogger(module)
API_VERSION = '2010-04-01'
def get_account(config):
account = twilio.Account(config.twilio_account_sid, config.twilio_account_token)
return account
def sms_issue(config, issue, recipient_list):
"""Helper method to send an sms of the issue defined by the bson dictionary issue"""
logger.info('Send sms of %s' % (issue["_id"]))
account = get_account(config)
msg = get_sms_body(issue)
receivers = recipient_list.split(',')
logger.debug(receivers)
for receiver in receivers:
if len(receiver.strip()) > 0:
logger.info('Sending SMS to %s' % (receiver))
body = {
'From' : config.twilio_caller_id,
'To' : receiver,
'Body' : msg
}
account.request('/%s/Accounts/%s/SMS/Messages' % (API_VERSION, config.twilio_account_sid), 'POST', body)
logger.info('Sent SMS')
def get_sms_body(issue):
"""Helper method to create an sms body of the issue bson"""
logger.info('Get sms body')
mail_body = ['Id=']
mail_body.append(issue["_id"])
mail_body.append(' priority=')
mail_body.append(str(issue["priority"]))
mail_body.append(' company=')
mail_body.append(issue.get('company', 'Unknown'))
mail_body.append(' reporter=')
mail_body.append(issue["reporter"])
mail_body.append(' created=')
mail_body.append(issue["jiraCreationTime"].strftime('%Y/%m/%d %H:%M:%S'))
return ''.join(mail_body)
def escalate(config, recipient_list, subject, issues):
logger.info('Escalate to phone')
account = get_account(config)
receivers = recipient_list.split(',')
logger.debug(receivers)
for receiver in receivers:
if len(receiver.strip()) > 0:
logger.info('Escalating to %s' % (receiver))
body = {
'From' : config.twilio_caller_id,
'To' : receiver,
'Url' : config.twilio_callback_url
}
account.request('/%s/Accounts/%s/Calls' % (API_VERSION, config.twilio_account_sid), 'POST', body)
if __name__ == '__main__':
logfilename = module+'.log'
logging.basicConfig(filename=logfilename, level=logging.DEBUG)
config = confighelper.get_config()
logger.debug(config.__dict__)
issue = {
'_id' : 'ID1',
'priority' : 3,
'company' : '10gen.com',
'reporter' : 'sridhar',
'jiraCreationTime' : datetime.datetime.now()
}
try:
#sms_issue(config, issue)
escalate(config, '6504835343', '', 'test')
except Exception, e:
print e;
print e.read()