-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathadmin_controller.rb
More file actions
63 lines (58 loc) · 2.8 KB
/
admin_controller.rb
File metadata and controls
63 lines (58 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Api::V1::AdminController < Api::V1::BaseApiController
before_action :authenticate_from_token!, except: [:save_messenger_credentials_for_smooch_bot, :save_instagram_credentials_for_smooch_bot]
# GET /api/admin/user/slack?uid=:uid
def slack_user
user = User.find_with_omniauth(params[:uid].to_s, 'slack')
slack_account = user.accounts.where(provider: 'slack').first unless user.nil?
user = { token: slack_account.token } unless slack_account.nil?
render_user user, 'slack_uid'
end
# GET /api/admin/smooch_bot/:bot-installation-id/authorize/messenger?token=:bot-installation-token
def save_messenger_credentials_for_smooch_bot
self.save_facebook_credentials_for_smooch_bot('messenger')
end
# GET /api/admin/smooch_bot/:bot-installation-id/authorize/instagram?token=:bot-installation-token
def save_instagram_credentials_for_smooch_bot
self.save_facebook_credentials_for_smooch_bot('instagram')
end
private
def save_facebook_credentials_for_smooch_bot(platform) # "platform" is either "instagram" or "messenger"
tbi = TeamBotInstallation.find(params[:id])
auth = session['check.facebook.authdata']
status = nil
if auth.blank?
status = 400
@message = I18n.t(:invalid_facebook_authdata)
error_msg = StandardError.new('Could not authenticate Facebook account for tipline Messenger integration.')
CheckSentry.notify(error_msg, team_bot_installation_id: tbi.id, platform: platform)
elsif params[:token].to_s.gsub('#_=_', '') == tbi.get_smooch_authorization_token
q_params = {
client_id: CheckConfig.get('smooch_facebook_app_id'),
client_secret: CheckConfig.get('smooch_facebook_app_secret'),
access_token: auth['token'],
limit: 100,
}
response = Net::HTTP.get_response(URI("https://graph.facebook.com/me/accounts?#{q_params.to_query}"))
pages = JSON.parse(response.body)['data']
if pages.size != 1
Rails.logger.info("[Facebook Messenger Integration] API scoped token: #{auth['token']} API response: #{response.body}")
CheckSentry.notify(StandardError.new('Unexpected list of Facebook pages returned for tipline Messenger integration'), team_bot_installation_id: tbi.id, response: response.body)
@message = I18n.t(:must_select_exactly_one_facebook_page)
status = 400
else
params = {
'appId' => CheckConfig.get('smooch_facebook_app_id'),
'appSecret' => CheckConfig.get('smooch_facebook_app_secret'),
'pageAccessToken' => pages[0]['access_token']
}
tbi.smooch_add_integration(platform, params)
@message = I18n.t(:smooch_facebook_success)
status = 200
end
else
@message = I18n.t(:invalid_token)
status = 401
end
render template: 'message', formats: :html, status: status
end
end