-
Notifications
You must be signed in to change notification settings - Fork 0
/
tropo_message.rb
207 lines (180 loc) · 5.46 KB
/
tropo_message.rb
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
module Tropo
class Message
attr_accessor :params, :tropo_session
def initialize(tropo_session = {})
@params = {}
@tropo_session = tropo_session
end
# Interesting methods
# Determines whether a message is meant for sending by checking
# if it has session parameters. This is useful for example if you have the same
# handler url for incoming and outgoing messages
#
# Example:
# tropo_object = Tropo::Generator.parse(raw_json)
# tropo_message = Tropo::Message.new(tropo_object)
# tropo_message.outgoing?
#
def outgoing?
tropo_session["session"] && tropo_session["session"]["parameters"]
end
# An alternative to the constructor
#
# Example:
# tropo_object = Tropo::Generator.parse(raw_json)
# tropo_message = Tropo::Message.new
# tropo_message.parse(tropo_object)
#
def parse(tropo_session)
self.tropo_session = tropo_session
end
# Generates xml suitable for an XML POST request to Tropo
#
# Example:
# tropo_message = Tropo::Message.new
# tropo_message.to = "44122782474"
# tropo_message.text = "Hi John, how r u today?"
# tropo_message.token = "1234512345"
#
# tropo_message.request_xml # =>
# # <sessions>
# # <token>1234512345</token>
# # <var name="to" value="44122782474"/>
# # <var name="text" value="Hi+John%2C+how+r+u+today%3F"/>
# # </sessions>"
#
def request_xml
request_params = @params.dup
token = request_params.delete("token")
xml = ""
request_params.each do |key, value|
xml << "<var name=\"#{escape(key)}\" value=\"#{escape(value)}\"/>"
end
"<sessions><token>#{token}</token>#{xml}</sessions>"
end
# Generates a hash suitable for using to as input to:
# Tropo::Generator#message (see: http://github.com/voxeo/tropo-webapi-ruby)
#
# By default, "channel" => "TEXT" and "network" => "SMS"
# You can override these values and any other optional parameters by setting
# their values e.g. tropo_message.channel = "VOICE"
#
# Example:
# tropo_object = Tropo::Generator.parse(raw_json)
#
# tropo_object # => {
# # "session" => {
# # ...
# # "parameters" => {
# # "to" => "44122782474",
# # "text" => "Hi+John%2C+how+r+u+today%3F",
# # "my_favourite_food" => "Pizza"
# # }
# # }
# #}
#
# tropo_message = Tropo::Message.new(tropo_object)
# response_params = tropo_message.response_params # => {
# # "to" => "44122782474",
# # "channel" => "TEXT",
# # "network" => "SMS"
# #}
#
# text = tropo_message.text
#
# Tropo::Generator.new.message(response_params) do
# say :value => text
# end
#
def response_params
params = {
"to" => to,
"channel" => channel,
"network" => network
}
params.merge!("from" => from) if from
params.merge!("timeout" => timeout) if timeout
params.merge!("answer_on_media" => answer_on_media) if answer_on_media
params.merge!("headers" => headers) if headers
params.merge!("recording" => recording) if recording
params
end
# Getter/Setter methods
def action
params["action"] || unescape(tropo_parameters["action"])
end
def answer_on_media
params["answer_on_media"] || unescape(tropo_parameters["answer_on_media"])
end
def channel
params["channel"] || unescape(tropo_parameters["channel"]) || "TEXT"
end
def from
params["from"] || unescape(tropo_parameters["from"])
end
def from=(value)
params["from"] = value
end
def headers
params["headers"] || unescape(tropo_parameters["headers"])
end
def network
params["network"] || unescape(tropo_parameters["network"]) || "SMS"
end
def recording
params["recording"] || unescape(tropo_parameters["recording"])
end
def text
params["text"] || unescape(tropo_parameters["text"])
end
def text=(value)
params["text"] = value
end
def timeout
params["timeout"] || unescape(tropo_parameters["timeout"])
end
def to
params["to"] || unescape(tropo_parameters["to"])
end
def to=(value)
params["to"] = value
end
def token
params["token"] || tropo_parameters["token"]
end
def token=(value)
params["token"] = value
end
private
def tropo_parameters
session = tropo_session["session"]
parameters = session["parameters"] if session
parameters || {}
end
# Performs URI escaping so that you can construct proper
# query strings faster. Use this rather than the cgi.rb
# version since it's faster. (Stolen from Camping).
def escape(s)
s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n) {
'%'+$1.unpack('H2'*bytesize($1)).join('%').upcase
}.tr(' ', '+')
end
# Unescapes a URI escaped string. (Stolen from Camping).
def unescape(s)
s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){
[$1.delete('%')].pack('H*')
} if s
end
# Return the bytesize of String; uses String#length under Ruby 1.8 and
# String#bytesize under 1.9.
if ''.respond_to?(:bytesize)
def bytesize(string)
string.bytesize
end
else
def bytesize(string)
string.size
end
end
end
end