Class: API::Messages::Service

Inherits:
Object
  • Object
show all
Extended by:
T::Helpers, T::Sig
Defined in:
lib/api/messages/service.rb

Constant Summary collapse

MESSAGING_PRODUCT =
::T.let("whatsapp", ::String)
RECIPIENT_TYPE =
::T.let("individual", ::String)

Instance Method Summary collapse

Constructor Details

#initialize(config:) ⇒ Service

Returns a new instance of Service.



15
16
17
# File 'lib/api/messages/service.rb', line 15

def initialize(config:)
  @config = config
end

Instance Method Details

#send_audio(recipient:, link:, reply_message_id: nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/api/messages/service.rb', line 69

def send_audio(recipient:, link:, reply_message_id: nil)
  audio_type = "audio"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": audio_type,
  }

  payload["audio"] = { link: link }
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_contact(recipient:, contacts:, reply_message_id: nil) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/api/messages/service.rb', line 158

def send_contact(recipient:, contacts:, reply_message_id: nil)
  contacts_type = "contacts"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": contacts_type,
  }

  payload["contacts"] = contacts.map(&:serialize)
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_document(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/api/messages/service.rb', line 111

def send_document(recipient:, caption:, link:, reply_message_id: nil)
  document_type = "document"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": document_type,
  }

  payload["document"] = { link: link, caption: caption }
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_image(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/api/messages/service.rb', line 48

def send_image(recipient:, caption:, link:, reply_message_id: nil)
  image_type = "image"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": image_type,
  }

  payload["image"] = { link: link, caption: caption }
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_location(recipient:, longitude:, latitude:, name:, address:, reply_message_id: nil) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/api/messages/service.rb', line 132

def send_location(recipient:, longitude:, latitude:, name:, address:, reply_message_id: nil)
  location_type = "location"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": location_type,
  }

  payload["location"] = {
    "longitude": longitude,
    "latitude": latitude,
    "name": name,
    "address": address
  }
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_template(recipient:, template_name:, template_lang:, components:) ⇒ Object



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/api/messages/service.rb', line 184

def send_template(recipient:, template_name:, template_lang:, components:)
  template_type = "template"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": template_type,
    "template": {
      "name": template_name,
      "language": {
        "code": template_lang
      },
      "components": []
    }
  }

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_text(recipient:, body:, reply_message_id: nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/api/messages/service.rb', line 23

def send_text(recipient:, body:, reply_message_id: nil)
  text_type = "text"
  enable_preview = false

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": text_type,
    "text": {
      "preview_url": enable_preview,
      "body": body
    }
  }

  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end

#send_video(recipient:, caption:, link:, reply_message_id: nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/api/messages/service.rb', line 90

def send_video(recipient:, caption:, link:, reply_message_id: nil)
  video_type = "video"

  payload = {
    "messaging_product": MESSAGING_PRODUCT,
    "recipient_type": RECIPIENT_TYPE,
    "to": recipient,
    "type": video_type,
  }

  payload["video"] = { link: link, caption: caption }
  payload["context"] = { "message_id": reply_message_id } unless reply_message_id.nil?

  response = http_client.post(body: payload, headers: {})
  parsed_response = JSON.parse(response.body.to_s)
  ::CloudWaba::Models::Messages::Response.parse(hash: parsed_response)
end