Class: Gupshup::REST::OutboundMessage

Inherits:
WhatsApp
  • Object
show all
Defined in:
lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, apikey, phone, version = '2') ⇒ OutboundMessage

Returns a new instance of OutboundMessage.



7
8
9
10
11
12
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 7

def initialize(app, apikey, phone, version = '2')
  super
  @channel = 'whatsapp'
  @source = phone
  @headers = { 'Content-type' => @content_type, 'apikey' => @apikey }
end

Instance Attribute Details

#apikeyObject

Returns the value of attribute apikey.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def apikey
  @apikey
end

#appObject

Returns the value of attribute app.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def app
  @app
end

#base_uriObject

Returns the value of attribute base_uri.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def base_uri
  @base_uri
end

#content_typeObject

Returns the value of attribute content_type.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def content_type
  @content_type
end

#headersObject

Returns the value of attribute headers.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def headers
  @headers
end

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def path
  @path
end

#phoneObject

Returns the value of attribute phone.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def phone
  @phone
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 5

def version
  @version
end

Instance Method Details

#send(destination, message_params) ⇒ Object



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
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 14

def send(destination, message_params)
# Exception handling for parameters to be added
case message_params[:type]
when 'text'
  if message_params[:isHSM]
    send_text_message(destination, message_params[:text], is_hsm = true)
  else
    send_text_message(destination, message_params[:text], is_hsm = false)
  end
when 'sticker'
  send_sticker(destination, message_params[:url])
when 'image'
  send_image(destination, message_params[:originalUrl], message_params[:previewUrl], message_params[:caption])
when 'video'
  send_video(destination, message_params[:url], message_params[:caption])
when 'audio'
  send_audio(destination, message_params[:url])
when 'file'
  send_file(destination, message_params[:url], message_params[:filename])
when 'location'
  send_location(destination, message_params[:longitude], message_params[:latitude], message_params[:name], message_params[:address])
when 'list'
  send_list(destination, message_params[:title], message_params[:body], message_params[:globalButtons], message_params[:items])
when 'quick_reply'
  send_button(destination, message_params[:content], message_params[:options])
else
  puts 'Done'
end
end

#send_audio(destination, url) ⇒ Object

Sends an audio

Parameters:

  • destination (String)

    destination phone number

  • url (String)

    url of the audio(publicly available) to send



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 136

def send_audio(destination, url)
  payload = {'url' => url,
             'type' => 'audio' }.to_json

  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_button(destination, content, options) ⇒ Object

Sends a button/quick reply

Parameters:

  • destination (String)

    destination phone number

  • content (String)

    message content

  • options (Array<String, String>)

    list of options with title text and message text.



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 194

def send_button(destination, content, options)
  uuid = UUID.new
  payload = { "type": 'quick_reply', "content": content, "msgid": uuid.generate,
              "options": options}.to_json
  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_file(destination, url, filename) ⇒ Object

Sends a file

Parameters:

  • destination (String)

    destination phone number

  • url (String)

    url of the video(publicly available) to send

  • filename (String)

    name of the file to be displayed in the chat



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 119

def send_file(destination, url, filename)
  payload = {'url' => url,
             'type' => 'file',
             'filename' => filename }.to_json

  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_image(destination, image_url, preview_url, caption) ⇒ Object

Sends an image the image

Parameters:

  • destination (String)

    destination phone number

  • image_url (String)

    url of the image(publicly available) to send

  • preview_url (String)

    url of the preview image(publicly available) to be displayed before the download of

  • caption (String)

    caption text for the image



68
69
70
71
72
73
74
75
76
77
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 68

def send_image(destination, image_url, preview_url, caption)
  payload = { 'type' => 'image', 'originalUrl' => image_url,
              'previewUrl' => preview_url, 'caption' => caption }.to_json
  data = { 'channel' => @channel, 'destination' => destination.to_s,
           'source' => @source, 'src.name' => @app,
           'message' => payload }
  puts payload
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_list(destination, title, body, global_buttons, items) ⇒ Object

Sends a list

Parameters:

  • destination (String)

    destination phone number

  • title (String)

    longitude

  • body (String)

    latitude

  • global_buttons (Array<String, String>)

    Buttons type(valid value: text) and Title of the button. [“text”, “title”: “Button title”].

  • items (Array<String, String>)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 175

def send_list(destination, title, body, global_buttons, items)
  uuid = UUID.new
  payload = { "type": 'list', "title": title, "body": body, "msgid": uuid.generate ,
              "globalButtons": [{ "type": global_buttons[0][:type], "title": global_buttons[0][:title] }],
              "items": items }.to_json
  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }

  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_location(destination, longitude, latitude, name, address) ⇒ Object

Sends a location

Parameters:

  • destination (String)

    destination phone number

  • longitude (String)

    longitude

  • latitude (String)

    latitude

  • name (String)

    name of the location to be displayed in the message

  • address (String)

    address displayed as a caption to the location attachment in the message



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 155

def send_location(destination, longitude, latitude, name, address)
  payload = { "type": 'location',
              "longitude": longitude, "latitude": latitude,
              "name": name, "address": address }.to_json

  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_sticker(destination, url) ⇒ Object

Sends a Whatsapp Sticker

Parameters:

  • destination (String)

    destination phone number

  • url (String)

    url of the sticker to send



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 82

def send_sticker(destination, url)
  payload = {
    'type': 'sticker',
    'url': url
  }.to_json

  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_text_message(destination, message, is_hsm = false) ⇒ Object

Sends a text

Parameters:

  • destination (String)

    destination phone number

  • message (String)

    text message

  • is_hsm (Boolean) (defaults to: false)

    specifies whether message is a template.



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 48

def send_text_message(destination, message, is_hsm=false)
  payload = {'isHSM' => is_hsm,
             'type' => 'text',
             'text' => message }.to_json
  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end

#send_video(destination, url, caption) ⇒ Object

Sends a video

Parameters:

  • destination (String)

    destination phone number

  • url (String)

    url of the video(publicly available) to send

  • caption (String)

    caption text for the video



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/gupshup_whatsapp/rest/whatsapp/message/outbound_message.rb', line 101

def send_video(destination, url, caption)
  payload = {'url' => url,
             'type' => 'video',
             'caption' => caption }.to_json

  data = { 'channel' => @channel,
           'destination' => destination.to_s,
           'source' => @source,
           'src.name' => @app,
           'message' => payload }
  r = Gupshup::HTTP::Client.new
  r.request(host = base_uri, port = 443, method = 'POST', url = path, data = data, headers = @headers)
end