fix: Handle PermissionDeniedError for Dialogflow processor (#8252)

This commit is contained in:
Pranav Raj S
2023-10-30 13:24:03 -07:00
committed by GitHub
parent 013dab7da6
commit 653e0335c0
8 changed files with 82 additions and 20 deletions

View File

@@ -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