chat_sdk-whatsapp 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b0736e051ec87a94ff8d73f8c0c023dfcbeefc8acf82c7772b4220289cf35266
4
- data.tar.gz: f86066dd39e8e99245630268b79f407a3f1d6c17dbcc82e41b4fd2edf49d538f
3
+ metadata.gz: 53567ff88a8c5c23d1a547b877b427c44562ddc8d44a7b1123b1c8fa0b32ceb4
4
+ data.tar.gz: beaf809c9f76782afa2b06066f3f326b2b37d6a676d40656779f2fe6ef56c122
5
5
  SHA512:
6
- metadata.gz: cac44b97beda98cdade923724bf7a36eda1501cf77b1e098a10fcc35765fd90ab956080b3d907383f8d8296657c6261465c6aef44b8e888c045d84c553fb3880
7
- data.tar.gz: 985a93b4c1655dcfc51de67bf0ca3db479cd7952d4aeb32c589cdd2b67d58a80e66227358098f197f68ec685c2ec63d35c1e71e6e455ce39c8ac9631cbdbcacf
6
+ metadata.gz: 7fe44f7b47094876654623c43e4c9ad6e68eaa2285e4ada4a7d88ff2172178db273923422e6683884d17bdb3d6af5e03830647cdbb0823bb43dfe3a268338ea9
7
+ data.tar.gz: cba377fb4474be0d2f6128b61baacea40913a3a9d260fade70b77ea8890c70036da5b818f3f52889143e63460e5f19e32d73bd6ece96c2a1523120a778616d20
@@ -4,6 +4,7 @@ module ChatSDK
4
4
  module WhatsApp
5
5
  class Adapter < ChatSDK::Adapter::Base
6
6
  include ChatSDK::Adapter::MetaVerification
7
+ include ChatSDK::Adapter::MediaTypes
7
8
 
8
9
  capabilities :direct_messages, :file_uploads, :reactions
9
10
 
@@ -46,11 +47,55 @@ module ChatSDK
46
47
  def post_message(channel_id:, message:, thread_id: nil) # rubocop:disable Lint/UnusedMethodArgument
47
48
  payload = prepare_message_payload(message)
48
49
 
49
- result = @client.send_message(to: channel_id, **payload)
50
+ result = if payload[:type] == "text" && payload.dig(:text, :body)
51
+ chunks = split_message(payload[:text][:body])
52
+ chunks.reduce(nil) do |_, chunk|
53
+ @client.send_message(to: channel_id, type: "text", text: {body: chunk})
54
+ end
55
+ else
56
+ @client.send_message(to: channel_id, **payload)
57
+ end
50
58
 
51
59
  parse_whatsapp_message(result, channel_id)
52
60
  end
53
61
 
62
+ def self.template_components(header: nil, body_params: [], button_payloads: [])
63
+ components = []
64
+
65
+ if header
66
+ components << {
67
+ "type" => "header",
68
+ "parameters" => [{"type" => "text", "text" => header}]
69
+ }
70
+ end
71
+
72
+ if body_params.any?
73
+ components << {
74
+ "type" => "body",
75
+ "parameters" => body_params.map { |p| {"type" => "text", "text" => p.to_s} }
76
+ }
77
+ end
78
+
79
+ button_payloads.each_with_index do |payload, index|
80
+ components << {
81
+ "type" => "button",
82
+ "sub_type" => "quick_reply",
83
+ "index" => index.to_s,
84
+ "parameters" => [{"type" => "payload", "payload" => payload}]
85
+ }
86
+ end
87
+
88
+ components
89
+ end
90
+
91
+ def post_template(channel_id:, template_name:, language_code: "en", components: nil)
92
+ @client.send_template(to: channel_id, template_name: template_name, language_code: language_code, components: components)
93
+ end
94
+
95
+ def mark_as_read(message_id:)
96
+ @client.mark_as_read(message_id: message_id)
97
+ end
98
+
54
99
  def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) # rubocop:disable Lint/UnusedMethodArgument
55
100
  content_type = detect_content_type(filename)
56
101
  media_type = media_type_for(content_type)
@@ -90,8 +135,26 @@ module ChatSDK
90
135
  end
91
136
  end
92
137
 
138
+ WHATSAPP_MESSAGE_LIMIT = 4096
139
+
93
140
  private
94
141
 
142
+ def split_message(text)
143
+ return [text] if text.length <= WHATSAPP_MESSAGE_LIMIT
144
+
145
+ chunks = []
146
+ remaining = text
147
+ while remaining.length > WHATSAPP_MESSAGE_LIMIT
148
+ cut_at = remaining.rindex("\n\n", WHATSAPP_MESSAGE_LIMIT) ||
149
+ remaining.rindex("\n", WHATSAPP_MESSAGE_LIMIT) ||
150
+ WHATSAPP_MESSAGE_LIMIT
151
+ chunks << remaining[0...cut_at]
152
+ remaining = remaining[cut_at..].lstrip
153
+ end
154
+ chunks << remaining unless remaining.empty?
155
+ chunks
156
+ end
157
+
95
158
  def prepare_message_payload(message)
96
159
  msg = ChatSDK::PostableMessage.from(message)
97
160
 
@@ -125,30 +188,6 @@ module ChatSDK
125
188
  raw: data
126
189
  )
127
190
  end
128
-
129
- CONTENT_TYPES = {
130
- ".jpg" => "image/jpeg", ".jpeg" => "image/jpeg", ".png" => "image/png",
131
- ".gif" => "image/gif", ".webp" => "image/webp",
132
- ".mp4" => "video/mp4", ".3gp" => "video/3gpp",
133
- ".mp3" => "audio/mpeg", ".ogg" => "audio/ogg", ".amr" => "audio/amr",
134
- ".pdf" => "application/pdf", ".doc" => "application/msword",
135
- ".docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
136
- ".xls" => "application/vnd.ms-excel",
137
- ".xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
138
- }.freeze
139
-
140
- def detect_content_type(filename)
141
- CONTENT_TYPES.fetch(File.extname(filename).downcase, "application/octet-stream")
142
- end
143
-
144
- def media_type_for(content_type)
145
- case content_type
146
- when %r{^image/} then "image"
147
- when %r{^video/} then "video"
148
- when %r{^audio/} then "audio"
149
- else "document"
150
- end
151
- end
152
191
  end
153
192
  end
154
193
  end
@@ -29,6 +29,28 @@ module ChatSDK
29
29
  )
30
30
  end
31
31
 
32
+ def send_template(to:, template_name:, language_code: "en", components: nil)
33
+ body = {
34
+ "messaging_product" => "whatsapp",
35
+ "to" => to,
36
+ "type" => "template",
37
+ "template" => {
38
+ "name" => template_name,
39
+ "language" => {"code" => language_code}
40
+ }
41
+ }
42
+ body["template"]["components"] = components if components
43
+ request(:post, "#{@phone_number_id}/messages", body)
44
+ end
45
+
46
+ def mark_as_read(message_id:)
47
+ request(:post, "#{@phone_number_id}/messages", {
48
+ "messaging_product" => "whatsapp",
49
+ "status" => "read",
50
+ "message_id" => message_id
51
+ })
52
+ end
53
+
32
54
  def upload_media(io:, filename:, content_type:)
33
55
  response = upload_connection.post("#{@phone_number_id}/media") do |req|
34
56
  req.body = {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chat_sdk-whatsapp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Quentin Rousseau