-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvalentunes.py
173 lines (153 loc) · 5.17 KB
/
valentunes.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import uuid
import os, sys
path = os.path.dirname(__file__)
sys.path.append("%s/" %path)
import twilio
import config
import web
#######################################
## Switching between messages and songs
#######################################
class Switcher:
def __init__(self, args):
self._uid = args._uid
self._digit = args.Digits
def go(self):
global path
root = "%s/data/%s" %(path, self._uid)
## Default file, intro message
if self._digit == '0':
f = open(root, 'r')
## Songs files, or main menu is not exits
elif self._digit in ['1', '2', '3', '4', '5']:
_file = "%s-%s" %(root, self._digit)
if os.path.exists(_file):
f = open(_file, 'r')
else:
f = open("%s-menu" %root, 'r')
## Menu file
else:
f = open("%s-menu" %root, 'r')
web.header("Content-Type","text/html; charset=utf-8")
return ''.join(f.readlines())
################################
## Valentunes wrapper for Twilio
################################
class Valentunes:
def __init__(self, _phone, _songs, _from, _to, **kwargs):
###############
## Phone number
###############
self._phone = _phone
################
## Songs (5 max)
################
self._songs = _songs
if len(self._songs) > 5:
self._songs = self._songs[:5]
################
## Intro message
################
self._message = "Hello %s, this is %s." %(_to, _from)
if 'message' in kwargs.keys():
self._message += kwargs['message']
#################
## Voice
#################
self._voice = twilio.Say.MAN
self._args = kwargs
if 'voice' in kwargs.keys():
if kwargs['voice'] == 'woman':
self._voice = twilio.Say.WOMAN
def call(self):
global path
## Generate uniqid
uid = uuid.uuid4()
## XML header
head = "<?xml version='1.0' encoding='utf-8' ?>\n"
#########################
## Generate intro message
#########################
r = twilio.Response()
r.addSay(
self._message,
voice = self._voice,
)
## Automatic redirect to the menu
r.addRedirect(
config.ROOT + "/data/%s-menu" %uid,
)
## Save Twilio message
f = open("%s/data/%s" %(path, uid), 'w')
f.write('%s%s' %(head, r))
f.close()
###########################################
## Generate menu message with list of songs
###########################################
r = twilio.Response()
i = 1
listing = ''
for s in self._songs:
listing += "Number %s: %s..." %(i, s['title'])
i += 1
## Let user type in the song number (5 secs. delay)
r.addGather(
action = config.ROOT + '/cgi.py/change?_uid=%s' %uid,
method = 'GET',
timeout = 5
).append(
twilio.Say(
"%s Type their number and the hash key to listen to them. Type 9 to listen to this list and 0 for the introduction message." %listing,
voice = self._voice,
)
)
## Redirect to first song if time-outs
r.addRedirect(
config.ROOT + "/data/%s-1" %uid,
)
## Save Twilio message
f = open("%s/data/%s-menu" %(path, uid), 'w')
f.write('%s%s' %(head, r))
f.close()
################################
## Generate one message per song
################################
i = 1
for s in self._songs:
r = twilio.Response()
r.addGather(
action = config.ROOT + '/cgi.py/change?_uid=%s' %uid,
method = 'GET',
timeout = 10
).append(
twilio.Play(s['url'])
)
## Automated redirect to the next one, or to the first one
root = "%s/data/%s" %(path, uid)
_file = "%s-%s" %(root, i+1)
if os.path.exists(_file):
next = i+1
else:
next = 1
r.addRedirect(
config.ROOT + "/data/%s-%s" %(uid, next)
)
## Save Twilio message
f = open("%s/data/%s-%s" %(path, uid, i), 'w')
f.write('%s%s' %(head, r))
f.close()
i += 1
####################################
## Ring the phone with intro message
####################################
account = twilio.Account(config.ACCOUNT_SID, config.ACCOUNT_TOKEN)
d = {
'From' : config.CALLER_ID,
'To' : self._phone,
'Url' : config.ROOT + "/data/%s" %uid,
}
try:
request = account.request('/%s/Accounts/%s/Calls' %(config.API_VERSION, config.ACCOUNT_SID), 'POST', d)
except Exception, e:
print e
print e.read()