2019-10-16 06:52:30 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
class Integrations::Facebook::MessageParser
|
|
|
|
def initialize(response_json)
|
2021-09-13 12:35:14 +00:00
|
|
|
@response = JSON.parse(response_json)
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def sender_id
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'sender', 'id'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def recipient_id
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'recipient', 'id'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def time_stamp
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'timestamp'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def content
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'text'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def sequence
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'seq'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def attachments
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'attachments'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def identifier
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'mid'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def echo?
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'is_echo'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-09-13 12:35:14 +00:00
|
|
|
# TODO : i don't think the payload contains app_id. if not remove
|
2020-01-09 07:36:40 +00:00
|
|
|
def app_id
|
2021-09-13 12:35:14 +00:00
|
|
|
@response.dig 'messaging', 'message', 'app_id'
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
|
2021-09-13 12:35:14 +00:00
|
|
|
# TODO : does this work ?
|
2020-01-09 07:36:40 +00:00
|
|
|
def sent_from_chatwoot_app?
|
|
|
|
app_id && app_id == ENV['FB_APP_ID'].to_i
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-27 14:52:55 +00:00
|
|
|
# Sample Response
|
2019-08-14 09:48:44 +00:00
|
|
|
# {
|
|
|
|
# "sender":{
|
|
|
|
# "id":"USER_ID"
|
|
|
|
# },
|
|
|
|
# "recipient":{
|
|
|
|
# "id":"PAGE_ID"
|
|
|
|
# },
|
|
|
|
# "timestamp":1458692752478,
|
|
|
|
# "message":{
|
|
|
|
# "mid":"mid.1457764197618:41d102a3e1ae206a38",
|
|
|
|
# "seq":73,
|
|
|
|
# "text":"hello, world!",
|
|
|
|
# "quick_reply": {
|
|
|
|
# "payload": "DEVELOPER_DEFINED_PAYLOAD"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# }
|