chat_sdk-messenger 0.2.1 → 0.3.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/messenger/adapter.rb +5 -33
- data/lib/chat_sdk/messenger/api_client.rb +10 -38
- data/lib/chat_sdk/messenger/template_renderer.rb +2 -26
- 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: d9447e3bada6d3889692e0f3171e56f7acdb5e494570dccbdf6779197daf5442
|
|
4
|
+
data.tar.gz: 296772588dae65f137ca59ba423b8e4802989ef6c9f162d4bc68612f98adb1f5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9059d6f773e8b8934148077b278ff44c6726541d36f141bf745ff6c5138d3dc62064f6f2f09db54da96ae17ba2c3c4b5e9b8afb6a53cbf516e9b41f00a22a449
|
|
7
|
+
data.tar.gz: 1d7d6392c3dea7025bdc0f733fb7e6249e02b8124b5d30a3fd725bacf3b67c289df6bb4196df5b1907dfb1b281773054738ea2e765ab0216cee24ca4d97066b2
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "openssl"
|
|
4
|
-
require "rack/utils"
|
|
5
|
-
|
|
6
3
|
module ChatSDK
|
|
7
4
|
module Messenger
|
|
8
5
|
class Adapter < ChatSDK::Adapter::Base
|
|
6
|
+
include ChatSDK::Adapter::MetaVerification
|
|
7
|
+
|
|
9
8
|
capabilities :typing_indicator, :direct_messages, :file_uploads
|
|
10
9
|
|
|
11
10
|
attr_reader :client
|
|
@@ -28,42 +27,15 @@ module ChatSDK
|
|
|
28
27
|
|
|
29
28
|
# Inbound
|
|
30
29
|
def verify_request!(rack_request)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
unless signature
|
|
34
|
-
raise ChatSDK::SignatureVerificationError, "Missing Facebook signature header"
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
body = rack_request.body.read
|
|
38
|
-
rack_request.body.rewind
|
|
39
|
-
|
|
40
|
-
expected = "sha256=#{OpenSSL::HMAC.hexdigest("SHA256", @app_secret, body)}"
|
|
41
|
-
|
|
42
|
-
unless Rack::Utils.secure_compare(signature, expected)
|
|
43
|
-
raise ChatSDK::SignatureVerificationError, "Invalid Facebook signature"
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
true
|
|
30
|
+
verify_meta_signature!(rack_request, secret: @app_secret, platform_name: "Facebook")
|
|
47
31
|
end
|
|
48
32
|
|
|
49
33
|
def ack_response(rack_request)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
params = rack_request.params
|
|
53
|
-
mode = params["hub.mode"]
|
|
54
|
-
token = params["hub.verify_token"]
|
|
55
|
-
challenge = params["hub.challenge"]
|
|
56
|
-
|
|
57
|
-
if mode == "subscribe" && token == @verify_token
|
|
58
|
-
[200, {}, [challenge.to_s]]
|
|
59
|
-
end
|
|
34
|
+
meta_ack_response(rack_request, verify_token: @verify_token)
|
|
60
35
|
end
|
|
61
36
|
|
|
62
37
|
def parse_events(rack_request)
|
|
63
|
-
|
|
64
|
-
rack_request.body.rewind
|
|
65
|
-
|
|
66
|
-
payload = JSON.parse(body)
|
|
38
|
+
payload = read_json_body(rack_request)
|
|
67
39
|
EventParser.parse(payload)
|
|
68
40
|
rescue JSON::ParserError
|
|
69
41
|
[]
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module ChatSDK
|
|
4
4
|
module Messenger
|
|
5
|
-
class ApiClient
|
|
5
|
+
class ApiClient < ChatSDK::ApiClient::Base
|
|
6
6
|
BASE_URL = "https://graph.facebook.com/v21.0/"
|
|
7
7
|
|
|
8
8
|
def initialize(page_access_token)
|
|
@@ -27,49 +27,21 @@ module ChatSDK
|
|
|
27
27
|
|
|
28
28
|
private
|
|
29
29
|
|
|
30
|
-
def
|
|
31
|
-
|
|
32
|
-
f.request :json
|
|
33
|
-
f.response :json
|
|
34
|
-
f.adapter :net_http
|
|
35
|
-
f.params["access_token"] = @page_access_token
|
|
36
|
-
end
|
|
30
|
+
def base_url
|
|
31
|
+
BASE_URL
|
|
37
32
|
end
|
|
38
33
|
|
|
39
|
-
def
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
end
|
|
34
|
+
def adapter_name
|
|
35
|
+
:messenger
|
|
36
|
+
end
|
|
43
37
|
|
|
44
|
-
|
|
38
|
+
def configure_auth(faraday)
|
|
39
|
+
faraday.params["access_token"] = @page_access_token
|
|
45
40
|
end
|
|
46
41
|
|
|
47
|
-
def
|
|
42
|
+
def extract_error_message(response)
|
|
48
43
|
body = response.body
|
|
49
|
-
|
|
50
|
-
if response.success?
|
|
51
|
-
return body if body.is_a?(Hash)
|
|
52
|
-
|
|
53
|
-
return {}
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
if response.status == 429
|
|
57
|
-
raise ChatSDK::RateLimitedError.new(
|
|
58
|
-
"Messenger API rate limited",
|
|
59
|
-
retry_after: nil,
|
|
60
|
-
status: response.status,
|
|
61
|
-
body: body,
|
|
62
|
-
adapter_name: :messenger
|
|
63
|
-
)
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
error_message = body.is_a?(Hash) ? body.dig("error", "message") : response.status
|
|
67
|
-
raise ChatSDK::PlatformError.new(
|
|
68
|
-
"Messenger API error: #{error_message}",
|
|
69
|
-
status: response.status,
|
|
70
|
-
body: body,
|
|
71
|
-
adapter_name: :messenger
|
|
72
|
-
)
|
|
44
|
+
body.is_a?(Hash) ? body.dig("error", "message") : response.status.to_s
|
|
73
45
|
end
|
|
74
46
|
end
|
|
75
47
|
end
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
module ChatSDK
|
|
4
4
|
module Messenger
|
|
5
5
|
class TemplateRenderer
|
|
6
|
+
include ChatSDK::Cards::TextHelpers
|
|
7
|
+
|
|
6
8
|
def render(node)
|
|
7
9
|
case node.type
|
|
8
10
|
when :card then render_card(node)
|
|
@@ -86,26 +88,6 @@ module ChatSDK
|
|
|
86
88
|
buttons
|
|
87
89
|
end
|
|
88
90
|
|
|
89
|
-
def collect_text_parts(node)
|
|
90
|
-
parts = []
|
|
91
|
-
node.children.each do |child|
|
|
92
|
-
case child.type
|
|
93
|
-
when :text
|
|
94
|
-
parts << child.attributes[:content]
|
|
95
|
-
when :divider
|
|
96
|
-
parts << "---"
|
|
97
|
-
when :fields
|
|
98
|
-
child.children.each do |field|
|
|
99
|
-
parts << "#{field.attributes[:label]}: #{field.attributes[:value]}"
|
|
100
|
-
end
|
|
101
|
-
when :section
|
|
102
|
-
parts << child.attributes[:title] if child.attributes[:title]
|
|
103
|
-
parts.concat(collect_text_parts(child))
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
parts
|
|
107
|
-
end
|
|
108
|
-
|
|
109
91
|
def find_image_url(node)
|
|
110
92
|
node.children.each do |child|
|
|
111
93
|
return child.attributes[:url] if child.type == :image
|
|
@@ -141,12 +123,6 @@ module ChatSDK
|
|
|
141
123
|
{text: node.fallback_text}
|
|
142
124
|
end
|
|
143
125
|
end
|
|
144
|
-
|
|
145
|
-
def truncate(text, max)
|
|
146
|
-
return "" unless text
|
|
147
|
-
|
|
148
|
-
(text.length > max) ? "#{text[0..max - 4]}..." : text
|
|
149
|
-
end
|
|
150
126
|
end
|
|
151
127
|
end
|
|
152
128
|
end
|