feat: Integration with Captain (alpha) (#9834)
- Integration with captain (alpha) Co-authored-by: Pranav <pranavrajs@gmail.com>
This commit is contained in:
65
lib/integrations/captain/processor_service.rb
Normal file
65
lib/integrations/captain/processor_service.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
class Integrations::Captain::ProcessorService < Integrations::BotProcessorService
|
||||
pattr_initialize [:event_name!, :hook!, :event_data!]
|
||||
|
||||
private
|
||||
|
||||
def get_response(_session_id, message_content)
|
||||
call_captain(message_content)
|
||||
end
|
||||
|
||||
def process_response(message, response)
|
||||
if response == 'conversation_handoff'
|
||||
message.conversation.bot_handoff!
|
||||
else
|
||||
create_conversation(message, { content: response })
|
||||
end
|
||||
end
|
||||
|
||||
def create_conversation(message, content_params)
|
||||
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
|
||||
}
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
def call_captain(message_content)
|
||||
url = "#{ENV.fetch('CAPTAIN_API_URL', nil)}/accounts/#{hook.settings['account_id']}/assistants/#{hook.settings['assistant_id']}/chat"
|
||||
|
||||
headers = {
|
||||
'X-USER-EMAIL' => hook.settings['account_email'],
|
||||
'X-USER-TOKEN' => hook.settings['access_token'],
|
||||
'Content-Type' => 'application/json'
|
||||
}
|
||||
|
||||
body = {
|
||||
message: message_content,
|
||||
previous_messages: previous_messages
|
||||
}
|
||||
|
||||
response = HTTParty.post(url, headers: headers, body: body.to_json)
|
||||
response.parsed_response['message']
|
||||
end
|
||||
|
||||
def previous_messages
|
||||
previous_messages = []
|
||||
conversation.messages.where(message_type: [:outgoing, :incoming]).where(private: false).offset(1).find_each do |message|
|
||||
next if message.content_type != 'text'
|
||||
|
||||
role = determine_role(message)
|
||||
previous_messages << { message: message.content, type: role }
|
||||
end
|
||||
previous_messages
|
||||
end
|
||||
|
||||
def determine_role(message)
|
||||
message.message_type == 'incoming' ? 'User' : 'Bot'
|
||||
end
|
||||
end
|
||||
@@ -14,13 +14,13 @@ class Integrations::Dialogflow::ProcessorService < Integrations::BotProcessorSer
|
||||
message.content
|
||||
end
|
||||
|
||||
def get_response(session_id, message)
|
||||
def get_response(session_id, message_content)
|
||||
if hook.settings['credentials'].blank?
|
||||
Rails.logger.warn "Account: #{hook.try(:account_id)} Hook: #{hook.id} credentials are not present." && return
|
||||
end
|
||||
|
||||
configure_dialogflow_client_defaults
|
||||
detect_intent(session_id, message)
|
||||
detect_intent(session_id, message_content)
|
||||
rescue Google::Cloud::PermissionDeniedError => e
|
||||
Rails.logger.warn "DialogFlow Error: (account-#{hook.try(:account_id)}, hook-#{hook.id}) #{e.message}"
|
||||
hook.prompt_reauthorization!
|
||||
|
||||
Reference in New Issue
Block a user