2019-11-30 13:39:55 +00:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: channel_facebook_pages
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# page_access_token :string not null
|
|
|
|
# user_access_token :string not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# account_id :integer not null
|
|
|
|
# page_id :string not null
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-12-05 07:50:04 +00:00
|
|
|
# index_channel_facebook_pages_on_page_id (page_id)
|
|
|
|
# index_channel_facebook_pages_on_page_id_and_account_id (page_id,account_id) UNIQUE
|
2019-11-30 13:39:55 +00:00
|
|
|
#
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
class Channel::FacebookPage < ApplicationRecord
|
|
|
|
self.table_name = 'channel_facebook_pages'
|
2019-10-20 10:49:12 +00:00
|
|
|
|
2020-09-29 19:42:32 +00:00
|
|
|
include Reauthorizable
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
validates :account_id, presence: true
|
|
|
|
validates :page_id, uniqueness: { scope: :account_id }
|
|
|
|
belongs_to :account
|
2019-10-20 10:49:12 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
has_one :inbox, as: :channel, dependent: :destroy
|
2019-10-20 10:49:12 +00:00
|
|
|
|
2020-05-17 18:14:50 +00:00
|
|
|
after_create_commit :subscribe
|
2020-01-09 07:36:40 +00:00
|
|
|
before_destroy :unsubscribe
|
2019-10-20 10:49:12 +00:00
|
|
|
|
2020-09-04 13:43:47 +00:00
|
|
|
def name
|
|
|
|
'Facebook'
|
|
|
|
end
|
|
|
|
|
2020-07-25 17:24:45 +00:00
|
|
|
def has_24_hour_messaging_window?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2020-05-17 18:14:50 +00:00
|
|
|
def subscribe
|
|
|
|
# ref https://developers.facebook.com/docs/messenger-platform/reference/webhook-events
|
|
|
|
response = Facebook::Messenger::Subscriptions.subscribe(
|
|
|
|
access_token: page_access_token,
|
|
|
|
subscribed_fields: %w[
|
|
|
|
messages message_deliveries message_echoes message_reads
|
|
|
|
]
|
|
|
|
)
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.debug "Rescued: #{e.inspect}"
|
|
|
|
true
|
|
|
|
end
|
2019-10-20 10:49:12 +00:00
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
def unsubscribe
|
|
|
|
Facebook::Messenger::Subscriptions.unsubscribe(access_token: page_access_token)
|
|
|
|
rescue => e
|
2020-05-17 18:14:50 +00:00
|
|
|
Rails.logger.debug "Rescued: #{e.inspect}"
|
2020-01-09 07:36:40 +00:00
|
|
|
true
|
2019-10-20 10:49:12 +00:00
|
|
|
end
|
|
|
|
end
|