fix: Handle PermissionDeniedError for Dialogflow processor (#8252)
This commit is contained in:
@@ -19,15 +19,12 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
|
||||
Rails.logger.warn "Account: #{hook.try(:account_id)} Hook: #{hook.id} credentials are not present." && return
|
||||
end
|
||||
|
||||
::Google::Cloud::Dialogflow::V2::Sessions::Client.configure do |config|
|
||||
config.timeout = 10.0
|
||||
config.credentials = hook.settings['credentials']
|
||||
end
|
||||
|
||||
client = ::Google::Cloud::Dialogflow::V2::Sessions::Client.new
|
||||
session = "projects/#{hook.settings['project_id']}/agent/sessions/#{session_id}"
|
||||
query_input = { text: { text: message, language_code: 'en-US' } }
|
||||
client.detect_intent session: session, query_input: query_input
|
||||
configure_dialogflow_client_defaults
|
||||
detect_intent(session_id, message)
|
||||
rescue Google::Cloud::PermissionDeniedError => e
|
||||
Rails.logger.warn "DialogFlow Error: (account-#{hook.try(:account_id)}, hook-#{hook.id}) #{e.message}"
|
||||
hook.prompt_reauthorization!
|
||||
hook.disable
|
||||
end
|
||||
|
||||
def process_response(message, response)
|
||||
@@ -53,10 +50,28 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
|
||||
return if content_params.blank?
|
||||
|
||||
conversation = message.conversation
|
||||
conversation.messages.create!(content_params.merge({
|
||||
message_type: :outgoing,
|
||||
account_id: conversation.account_id,
|
||||
inbox_id: conversation.inbox_id
|
||||
}))
|
||||
conversation.messages.create!(
|
||||
content_params.merge(
|
||||
{
|
||||
message_type: :outgoing,
|
||||
account_id: conversation.account_id,
|
||||
inbox_id: conversation.inbox_id
|
||||
}
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def configure_dialogflow_client_defaults
|
||||
::Google::Cloud::Dialogflow::V2::Sessions::Client.configure do |config|
|
||||
config.timeout = 10.0
|
||||
config.credentials = hook.settings['credentials']
|
||||
end
|
||||
end
|
||||
|
||||
def detect_intent(session_id, message)
|
||||
client = ::Google::Cloud::Dialogflow::V2::Sessions::Client.new
|
||||
session = "projects/#{hook.settings['project_id']}/agent/sessions/#{session_id}"
|
||||
query_input = { text: { text: message, language_code: 'en-US' } }
|
||||
client.detect_intent session: session, query_input: query_input
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user