chat_sdk-whatsapp 0.5.0 → 0.7.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 +4 -4
- data/lib/chat_sdk/whatsapp/adapter.rb +64 -25
- data/lib/chat_sdk/whatsapp/api_client.rb +22 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3f3c28086e09ba4878d59fb136e601ef30f56a60e6baa4f0cb8bb88cd65937da
|
|
4
|
+
data.tar.gz: beaf809c9f76782afa2b06066f3f326b2b37d6a676d40656779f2fe6ef56c122
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 68b9eb36df3a4202f27fc9d6536cb00208e7f55b57fd6faec07d195c84b3680b0242225e29696112ec9fbccfc291ef13881fbf263092056deff315a6ae87485e
|
|
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 =
|
|
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 = {
|