chat_sdk-whatsapp 0.4.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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 53567ff88a8c5c23d1a547b877b427c44562ddc8d44a7b1123b1c8fa0b32ceb4
|
|
4
|
+
data.tar.gz: beaf809c9f76782afa2b06066f3f326b2b37d6a676d40656779f2fe6ef56c122
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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 =
|
|
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
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module ChatSDK
|
|
4
4
|
module WhatsApp
|
|
5
5
|
class ApiClient < ChatSDK::ApiClient::Base
|
|
6
|
-
BASE_URL = "https://graph.facebook.com/
|
|
6
|
+
BASE_URL = "https://graph.facebook.com/v25.0"
|
|
7
7
|
|
|
8
8
|
def initialize(access_token, phone_number_id)
|
|
9
9
|
@access_token = access_token
|
|
@@ -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 = {
|
|
@@ -29,11 +29,30 @@ module ChatSDK
|
|
|
29
29
|
build_direct_message(msg, msg.dig("text", "body") || "", author, from, thread_id)
|
|
30
30
|
when "interactive"
|
|
31
31
|
parse_interactive_message(msg, author, from, thread_id)
|
|
32
|
-
when "
|
|
32
|
+
when "reaction"
|
|
33
|
+
parse_reaction_message(msg, from, thread_id)
|
|
34
|
+
when "image", "document", "audio", "video", "sticker"
|
|
33
35
|
parse_media_message(msg, author, from, thread_id)
|
|
34
36
|
end
|
|
35
37
|
end
|
|
36
38
|
|
|
39
|
+
def parse_reaction_message(msg, from, thread_id)
|
|
40
|
+
reaction = msg["reaction"] || {}
|
|
41
|
+
emoji = reaction["emoji"] || ""
|
|
42
|
+
|
|
43
|
+
ChatSDK::Events::Reaction.new(
|
|
44
|
+
emoji: emoji,
|
|
45
|
+
added: !emoji.empty?,
|
|
46
|
+
user_id: from,
|
|
47
|
+
message_id: reaction["message_id"],
|
|
48
|
+
thread_id: thread_id,
|
|
49
|
+
channel_id: from,
|
|
50
|
+
platform: :whatsapp,
|
|
51
|
+
adapter_name: :whatsapp,
|
|
52
|
+
raw: msg
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
37
56
|
def parse_interactive_message(msg, author, channel_id, thread_id)
|
|
38
57
|
interactive = msg["interactive"] || {}
|
|
39
58
|
action_id = interactive.dig("button_reply", "id") || interactive.dig("list_reply", "id") || ""
|
|
@@ -41,6 +41,8 @@ module ChatSDK
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
{type: "interactive", interactive: interactive}
|
|
44
|
+
elsif buttons.length > 3
|
|
45
|
+
render_list_message(title, body_text, buttons)
|
|
44
46
|
else
|
|
45
47
|
# Fallback to plain text
|
|
46
48
|
fallback = [title, subtitle, *text_parts].compact.reject(&:empty?).join("\n")
|
|
@@ -52,6 +54,27 @@ module ChatSDK
|
|
|
52
54
|
end
|
|
53
55
|
end
|
|
54
56
|
|
|
57
|
+
def render_list_message(title, body_text, buttons)
|
|
58
|
+
interactive = {
|
|
59
|
+
"type" => "list",
|
|
60
|
+
"body" => {"text" => body_text}
|
|
61
|
+
}
|
|
62
|
+
interactive["header"] = {"type" => "text", "text" => truncate(title, 60)} if title
|
|
63
|
+
interactive["action"] = {
|
|
64
|
+
"button" => "Options",
|
|
65
|
+
"sections" => [{
|
|
66
|
+
"rows" => buttons.first(10).map do |btn|
|
|
67
|
+
{
|
|
68
|
+
"id" => btn[:id],
|
|
69
|
+
"title" => truncate(btn[:text], 24)
|
|
70
|
+
}
|
|
71
|
+
end
|
|
72
|
+
}]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
{type: "interactive", interactive: interactive}
|
|
76
|
+
end
|
|
77
|
+
|
|
55
78
|
def collect_reply_buttons(node)
|
|
56
79
|
buttons = []
|
|
57
80
|
node.children.each do |child|
|