2019-08-14 09:48:44 +00:00
|
|
|
require 'rest-client'
|
|
|
|
require 'telegram/bot'
|
|
|
|
class Api::V1::CallbacksController < ApplicationController
|
2019-10-20 08:47:26 +00:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:register_facebook_page]
|
|
|
|
skip_before_action :authenticate_user!, only: [:register_facebook_page], raise: false
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
def register_facebook_page
|
|
|
|
user_access_token = params[:user_access_token]
|
|
|
|
page_access_token = params[:page_access_token]
|
|
|
|
page_name = params[:page_name]
|
|
|
|
page_id = params[:page_id]
|
|
|
|
inbox_name = params[:inbox_name]
|
2019-10-21 05:48:47 +00:00
|
|
|
facebook_channel = current_account.facebook_pages.create!(
|
|
|
|
name: page_name, page_id: page_id, user_access_token: user_access_token,
|
|
|
|
page_access_token: page_access_token, remote_avatar_url: set_avatar(page_id)
|
|
|
|
)
|
2019-08-14 09:48:44 +00:00
|
|
|
inbox = current_account.inboxes.create!(name: inbox_name, channel: facebook_channel)
|
|
|
|
render json: inbox
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_facebook_pages
|
2019-10-20 08:47:26 +00:00
|
|
|
@page_details = mark_already_existing_facebook_pages(fb_object.get_connections('me', 'accounts'))
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
def reauthorize_page # get params[:inbox_id], current_account, params[:omniauth_token]
|
2019-08-14 09:48:44 +00:00
|
|
|
inbox = current_account.inboxes.find_by(id: params[:inbox_id])
|
|
|
|
if inbox
|
|
|
|
fb_page_id = inbox.channel.page_id
|
2019-10-20 08:47:26 +00:00
|
|
|
page_details = fb_object.get_connections('me', 'accounts')
|
2019-08-14 09:48:44 +00:00
|
|
|
(page_details || []).each do |page_detail|
|
2019-10-20 08:47:26 +00:00
|
|
|
next unless fb_page_id == page_detail['id'] # found the page which has to be reauthorised
|
|
|
|
|
|
|
|
fb_page = current_account.facebook_pages.find_by(page_id: fb_page_id)
|
|
|
|
if fb_page
|
|
|
|
fb_page.update_attributes!(
|
|
|
|
user_access_token: @user_access_token,
|
|
|
|
page_access_token: page_detail['access_token']
|
|
|
|
)
|
|
|
|
head :ok
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def fb_object
|
|
|
|
@user_access_token = long_lived_token(params[:omniauth_token])
|
|
|
|
Koala::Facebook::API.new(@user_access_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def long_lived_token(omniauth_token)
|
|
|
|
koala = Koala::Facebook::OAuth.new(ENV['fb_app_id'], ENV['fb_app_secret'])
|
2019-10-20 08:47:26 +00:00
|
|
|
long_lived_token = koala.exchange_access_token_info(omniauth_token)['access_token']
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def mark_already_existing_facebook_pages(data)
|
|
|
|
return [] if data.empty?
|
2019-10-20 08:47:26 +00:00
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
data.inject([]) do |result, page_detail|
|
2019-10-20 08:47:26 +00:00
|
|
|
current_account.facebook_pages.exists?(page_id: page_detail['id']) ? page_detail.merge!(exists: true) : page_detail.merge!(exists: false)
|
2019-08-14 09:48:44 +00:00
|
|
|
result << page_detail
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_avatar(page_id)
|
|
|
|
begin
|
2019-10-20 08:47:26 +00:00
|
|
|
url = 'http://graph.facebook.com/' << page_id << '/picture?type=large'
|
2019-08-14 09:48:44 +00:00
|
|
|
uri = URI.parse(url)
|
|
|
|
tries = 3
|
|
|
|
begin
|
|
|
|
response = uri.open(redirect: false)
|
2019-10-20 08:47:26 +00:00
|
|
|
rescue OpenURI::HTTPRedirect => e
|
|
|
|
uri = e.uri # assigned from the "Location" response header
|
2019-08-14 09:48:44 +00:00
|
|
|
retry if (tries -= 1) > 0
|
|
|
|
raise
|
|
|
|
end
|
|
|
|
pic_url = response.base_uri.to_s
|
|
|
|
Rails.logger.info(pic_url)
|
2019-10-20 08:47:26 +00:00
|
|
|
rescue StandardError => e
|
2019-08-14 09:48:44 +00:00
|
|
|
pic_url = nil
|
|
|
|
end
|
|
|
|
pic_url
|
|
|
|
end
|
|
|
|
end
|