feat: Added the ability to create Instagram channel (#11182)
This PR is part of https://github.com/chatwoot/chatwoot/pull/11054 to make the review cycle easier.
This commit is contained in:
@@ -16,13 +16,47 @@
|
||||
#
|
||||
class Channel::Instagram < ApplicationRecord
|
||||
include Channelable
|
||||
|
||||
include Reauthorizable
|
||||
self.table_name = 'channel_instagram'
|
||||
|
||||
validates :access_token, presence: true
|
||||
validates :instagram_id, uniqueness: true, presence: true
|
||||
|
||||
after_create_commit :subscribe
|
||||
before_destroy :unsubscribe
|
||||
|
||||
def name
|
||||
'Instagram'
|
||||
end
|
||||
|
||||
def subscribe
|
||||
# ref https://developers.facebook.com/docs/instagram-platform/webhooks#enable-subscriptions
|
||||
HTTParty.post(
|
||||
"https://graph.instagram.com/v22.0/#{instagram_id}/subscribed_apps",
|
||||
query: {
|
||||
subscribed_fields: %w[messages message_reactions messaging_seen],
|
||||
access_token: access_token
|
||||
}
|
||||
)
|
||||
rescue StandardError => e
|
||||
Rails.logger.debug { "Rescued: #{e.inspect}" }
|
||||
true
|
||||
end
|
||||
|
||||
def unsubscribe
|
||||
HTTParty.delete(
|
||||
"https://graph.instagram.com/v22.0/#{instagram_id}/subscribed_apps",
|
||||
query: {
|
||||
access_token: access_token
|
||||
}
|
||||
)
|
||||
true
|
||||
rescue StandardError => e
|
||||
Rails.logger.debug { "Rescued: #{e.inspect}" }
|
||||
true
|
||||
end
|
||||
|
||||
def access_token
|
||||
Instagram::RefreshOauthTokenService.new(channel: self).access_token
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user