Class: Tropo::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/tropo_message.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Message) initialize(tropo_session = {})



6
7
8
9
# File 'lib/tropo_message.rb', line 6

def initialize(tropo_session = {})
  @params = {}
  @tropo_session = tropo_session
end

Instance Attribute Details

- (Object) params

Returns the value of attribute params



4
5
6
# File 'lib/tropo_message.rb', line 4

def params
  @params
end

- (Object) tropo_session

Returns the value of attribute tropo_session



4
5
6
# File 'lib/tropo_message.rb', line 4

def tropo_session
  @tropo_session
end

Instance Method Details

- (Object) action

Getter/Setter methods



111
112
113
# File 'lib/tropo_message.rb', line 111

def action
  params["action"] || unescape(tropo_parameters["action"])
end

- (Object) answer_on_media



115
116
117
# File 'lib/tropo_message.rb', line 115

def answer_on_media
  params["answer_on_media"] || unescape(tropo_parameters["answer_on_media"])
end

- (Object) bytesize(string)



197
198
199
# File 'lib/tropo_message.rb', line 197

def bytesize(string)
  string.size
end

- (Object) channel



119
120
121
# File 'lib/tropo_message.rb', line 119

def channel
  params["channel"] || unescape(tropo_parameters["channel"]) || "TEXT"
end

- (Object) from



123
124
125
# File 'lib/tropo_message.rb', line 123

def from
  params["from"] || unescape(tropo_parameters["from"])
end

- (Object) from=(value)



127
128
129
# File 'lib/tropo_message.rb', line 127

def from=(value)
  params["from"] = value
end

- (Object) headers



131
132
133
# File 'lib/tropo_message.rb', line 131

def headers
  params["headers"] || unescape(tropo_parameters["headers"])
end

- (Object) network



135
136
137
# File 'lib/tropo_message.rb', line 135

def network
  params["network"] || unescape(tropo_parameters["network"]) || "SMS"
end

- (Boolean) outgoing?

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?


22
23
24
# File 'lib/tropo_message.rb', line 22

def outgoing?
  tropo_session["session"] && tropo_session["session"]["parameters"]
end

- (Object) parse(tropo_session)

An alternative to the constructor

Example:

tropo_object = Tropo::Generator.parse(raw_json)
tropo_message = Tropo::Message.new
tropo_message.parse(tropo_object)


33
34
35
# File 'lib/tropo_message.rb', line 33

def parse(tropo_session)
  self.tropo_session = tropo_session
end

- (Object) recording



139
140
141
# File 'lib/tropo_message.rb', line 139

def recording
  params["recording"] || unescape(tropo_parameters["recording"])
end

- (Object) request_xml

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>"


52
53
54
55
56
57
58
59
60
# File 'lib/tropo_message.rb', line 52

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

- (Object) response_params

Generates a hash suitable for using to as input to: Tropo::Generator#message (see: 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


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/tropo_message.rb', line 96

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

- (Object) text



143
144
145
# File 'lib/tropo_message.rb', line 143

def text
  params["text"] || unescape(tropo_parameters["text"])
end

- (Object) text=(value)



147
148
149
# File 'lib/tropo_message.rb', line 147

def text=(value)
  params["text"] = value
end

- (Object) timeout



151
152
153
# File 'lib/tropo_message.rb', line 151

def timeout
  params["timeout"] || unescape(tropo_parameters["timeout"])
end

- (Object) to



155
156
157
# File 'lib/tropo_message.rb', line 155

def to
  params["to"] || unescape(tropo_parameters["to"])
end

- (Object) to=(value)



159
160
161
# File 'lib/tropo_message.rb', line 159

def to=(value)
  params["to"] = value
end

- (Object) token



163
164
165
# File 'lib/tropo_message.rb', line 163

def token
  params["token"] || tropo_parameters["token"]
end

- (Object) token=(value)



167
168
169
# File 'lib/tropo_message.rb', line 167

def token=(value)
  params["token"] = value
end