chat_sdk-messenger 0.3.1 → 0.5.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: 8cf242ff225b396851ebe21ac3086fbfdb2d42d5f87e658e0e4a975aec56871f
|
|
4
|
+
data.tar.gz: aacfb426703ef8701c28425a1d7271085fce7755f7c96d36494ee3ca9063faed
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9383ed079f439b419e4ae2060353d7e570463a27a7583b1fda4b8a839db4ef1e7e5aaa5fc28efa1e65f1b0b0db8586fb344ee83ff5cb58529b92fda4834b288f
|
|
7
|
+
data.tar.gz: 48bcd0cddf5e3d7c512e49c880b4dee47d17abfa3dc0828db2a8f66f0125cbda5dd15ec3039b0a1ec8ea38f4a371425e1dd068ad1ec75c3575798e7fe46fad3f
|
|
@@ -9,10 +9,11 @@ module ChatSDK
|
|
|
9
9
|
|
|
10
10
|
attr_reader :client
|
|
11
11
|
|
|
12
|
-
def initialize(app_secret: nil, page_access_token: nil, verify_token: nil)
|
|
12
|
+
def initialize(app_secret: nil, page_access_token: nil, verify_token: nil, page_id: nil)
|
|
13
13
|
@app_secret = app_secret || ENV["FACEBOOK_APP_SECRET"]
|
|
14
14
|
@page_access_token = page_access_token || ENV["FACEBOOK_PAGE_ACCESS_TOKEN"]
|
|
15
15
|
@verify_token = verify_token || ENV["FACEBOOK_VERIFY_TOKEN"]
|
|
16
|
+
@page_id = page_id || ENV["FACEBOOK_PAGE_ID"]
|
|
16
17
|
|
|
17
18
|
raise ChatSDK::ConfigurationError, "Messenger app_secret required" unless @app_secret
|
|
18
19
|
raise ChatSDK::ConfigurationError, "Messenger page_access_token required" unless @page_access_token
|
|
@@ -36,7 +37,7 @@ module ChatSDK
|
|
|
36
37
|
|
|
37
38
|
def parse_events(rack_request)
|
|
38
39
|
payload = read_json_body(rack_request)
|
|
39
|
-
EventParser.parse(payload)
|
|
40
|
+
EventParser.parse(payload, bot_page_id: @page_id)
|
|
40
41
|
rescue JSON::ParserError
|
|
41
42
|
[]
|
|
42
43
|
end
|
|
@@ -4,7 +4,7 @@ module ChatSDK
|
|
|
4
4
|
module Messenger
|
|
5
5
|
class EventParser
|
|
6
6
|
class << self
|
|
7
|
-
def parse(payload)
|
|
7
|
+
def parse(payload, bot_page_id: nil)
|
|
8
8
|
return [] unless payload.is_a?(Hash)
|
|
9
9
|
return [] unless payload["object"] == "page"
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ module ChatSDK
|
|
|
12
12
|
|
|
13
13
|
(payload["entry"] || []).each do |entry|
|
|
14
14
|
(entry["messaging"] || []).each do |messaging|
|
|
15
|
-
event = parse_messaging(messaging)
|
|
15
|
+
event = parse_messaging(messaging, bot_page_id: bot_page_id)
|
|
16
16
|
events << event if event
|
|
17
17
|
end
|
|
18
18
|
end
|
|
@@ -22,14 +22,17 @@ module ChatSDK
|
|
|
22
22
|
|
|
23
23
|
private
|
|
24
24
|
|
|
25
|
-
def parse_messaging(messaging)
|
|
25
|
+
def parse_messaging(messaging, bot_page_id: nil)
|
|
26
26
|
sender_id = messaging.dig("sender", "id")&.to_s
|
|
27
27
|
return nil unless sender_id
|
|
28
|
+
return nil if bot_page_id && sender_id == bot_page_id
|
|
28
29
|
|
|
29
30
|
channel_id = sender_id
|
|
30
31
|
thread_id = "messenger:#{sender_id}"
|
|
31
32
|
|
|
32
|
-
if messaging["
|
|
33
|
+
if messaging["reaction"]
|
|
34
|
+
parse_reaction(messaging, sender_id, channel_id, thread_id)
|
|
35
|
+
elsif messaging["postback"]
|
|
33
36
|
parse_postback(messaging, sender_id, channel_id, thread_id)
|
|
34
37
|
elsif messaging["message"]
|
|
35
38
|
parse_message(messaging, sender_id, channel_id, thread_id)
|
|
@@ -66,6 +69,22 @@ module ChatSDK
|
|
|
66
69
|
)
|
|
67
70
|
end
|
|
68
71
|
|
|
72
|
+
def parse_reaction(messaging, sender_id, channel_id, thread_id)
|
|
73
|
+
reaction = messaging["reaction"]
|
|
74
|
+
|
|
75
|
+
ChatSDK::Events::Reaction.new(
|
|
76
|
+
emoji: reaction["emoji"],
|
|
77
|
+
added: reaction["action"] == "react",
|
|
78
|
+
user_id: sender_id,
|
|
79
|
+
message_id: reaction["mid"],
|
|
80
|
+
thread_id: thread_id,
|
|
81
|
+
channel_id: channel_id,
|
|
82
|
+
platform: :messenger,
|
|
83
|
+
adapter_name: :messenger,
|
|
84
|
+
raw: messaging
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
|
|
69
88
|
def parse_postback(messaging, sender_id, channel_id, thread_id)
|
|
70
89
|
postback = messaging["postback"]
|
|
71
90
|
payload = postback["payload"] || ""
|