Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion app/controllers/webhooks/bot_runtime_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,20 @@ def postback
return
end

message = AgentBots::MessageCreator.new(agent_bot).create_bot_reply(content, conversation)
content_type = params[:content_type].presence || 'text'
raw_items = params[:items]
content_attributes = nil

if content_type == 'input_select' && raw_items.present?
items = raw_items.map { |item| { title: item[:title].to_s, value: item[:value].to_s } }
content_attributes = { items: items }
end

message = AgentBots::MessageCreator.new(agent_bot).create_bot_reply(
content, conversation,
content_type: content_type,
content_attributes: content_attributes
)

if message
Rails.logger.info "[BotRuntime::Postback] Message created: #{message.id} conversation=#{conversation.display_id}"
Expand Down
32 changes: 21 additions & 11 deletions app/services/agent_bots/message_creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def initialize(agent_bot)
@agent_bot = agent_bot
end

def create_bot_reply(content, conversation, force: false)
def create_bot_reply(content, conversation, force: false, content_type: 'text', content_attributes: nil)
return if content.blank?

# If force is true, skip eligibility check (e.g., for final response after transfer)
Expand All @@ -18,7 +18,7 @@ def create_bot_reply(content, conversation, force: false)
end

Rails.logger.info "[AgentBot HTTP] Creating bot reply in conversation #{conversation.id}"
create_message_with_fallback(content, conversation)
create_message_with_fallback(content, conversation, content_type: content_type, content_attributes: content_attributes)
end

private
Expand Down Expand Up @@ -53,33 +53,38 @@ def conversation_eligible_for_bot_reply?(conversation)
eligible
end

def create_message_with_fallback(content, conversation)
create_direct_message(content, conversation)
def create_message_with_fallback(content, conversation, content_type:, content_attributes:)
create_direct_message(content, conversation, content_type: content_type, content_attributes: content_attributes)
rescue StandardError => e
log_creation_error(e)
create_message_with_builder(content, conversation)
create_message_with_builder(content, conversation, content_type: content_type, content_attributes: content_attributes)
end

def create_direct_message(content, conversation)
def create_direct_message(content, conversation, content_type:, content_attributes:)
message_attributes = {
inbox: conversation.inbox,
conversation: conversation,
content: content,
message_type: 'outgoing',
sender: @agent_bot,
content_type: 'text'
content_type: content_type
}

merged_content_attributes = {}
merged_content_attributes.merge!(content_attributes) if content_attributes.present?

# Check if send_as_reply is enabled in bot_config
send_as_reply = @agent_bot.bot_config&.dig('send_as_reply') == true

# For post conversations, always reply to the last incoming message
# OR if send_as_reply is enabled in bot_config
if conversation.post_conversation? || send_as_reply
reply_attributes = build_reply_attributes(conversation)
message_attributes[:content_attributes] = reply_attributes if reply_attributes.present?
merged_content_attributes.merge!(reply_attributes) if reply_attributes.present?
end

message_attributes[:content_attributes] = merged_content_attributes if merged_content_attributes.present?

message = Message.create!(message_attributes)

Rails.logger.info "[AgentBot HTTP] Successfully created message #{message.id}"
Expand All @@ -94,8 +99,11 @@ def log_creation_error(error)
Rails.logger.info '[AgentBot HTTP] Trying with MessageBuilder as fallback'
end

def create_message_with_builder(content, conversation)
message_params = { content: content, message_type: 'outgoing' }
def create_message_with_builder(content, conversation, content_type:, content_attributes:)
message_params = { content: content, message_type: 'outgoing', content_type: content_type }

merged_content_attributes = {}
merged_content_attributes.merge!(content_attributes) if content_attributes.present?

# Check if send_as_reply is enabled in bot_config
send_as_reply = @agent_bot.bot_config&.dig('send_as_reply') == true
Expand All @@ -104,9 +112,11 @@ def create_message_with_builder(content, conversation)
# OR if send_as_reply is enabled in bot_config
if conversation.post_conversation? || send_as_reply
reply_attributes = build_reply_attributes(conversation)
message_params[:content_attributes] = reply_attributes if reply_attributes.present?
merged_content_attributes.merge!(reply_attributes) if reply_attributes.present?
end

message_params[:content_attributes] = merged_content_attributes if merged_content_attributes.present?

message = Messages::MessageBuilder.new(@agent_bot, conversation, message_params).perform
Rails.logger.info "[AgentBot HTTP] MessageBuilder fallback successful: #{message.id}"
Rails.logger.info "[AgentBot HTTP] Reply attributes: #{message.content_attributes.slice(:in_reply_to, :in_reply_to_external_id).inspect}" if conversation.post_conversation? || send_as_reply
Expand Down
39 changes: 30 additions & 9 deletions app/services/agent_bots/response_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,18 @@ def process_bot_response(parsed_response)
artifacts = extract_artifacts(parsed_response)
return unless artifacts

text_content = extract_text_from_artifacts(artifacts)
extracted = extract_content_from_artifacts(artifacts)
text_content = extracted[:text]
return unless text_content

conversation = AgentBots::ConversationFinder.new(@agent_bot, @payload).find_conversation
return unless conversation

select_part = extracted[:select]
select_items = select_part&.dig('items')

# Check if text segmentation is enabled for this agent bot
if @agent_bot.text_segmentation_enabled && ['evo_ai_provider', 'n8n_provider'].include?(@agent_bot.bot_provider)
if select_items.blank? && @agent_bot.text_segmentation_enabled && ['evo_ai_provider', 'n8n_provider'].include?(@agent_bot.bot_provider)
process_segmented_response(text_content, conversation)
else
# Process as a single message with signature
Expand All @@ -59,13 +63,15 @@ def process_bot_response(parsed_response)

# Try to create message normally first
message_creator = AgentBots::MessageCreator.new(@agent_bot)
message = message_creator.create_bot_reply(final_content, conversation)
content_type = select_items.present? ? 'input_select' : 'text'
content_attributes = select_items.present? ? { items: select_items } : nil
message = message_creator.create_bot_reply(final_content, conversation, content_type: content_type, content_attributes: content_attributes)

# If message creation failed (conversation not eligible, e.g., after transfer),
# try to force create it anyway (for final responses after transfer)
unless message
Rails.logger.info "[AgentBot HTTP] Message creation failed (conversation not eligible), attempting force create..."
message = message_creator.create_bot_reply(final_content, conversation, force: true)
message = message_creator.create_bot_reply(final_content, conversation, force: true, content_type: content_type, content_attributes: content_attributes)
end

message
Expand All @@ -79,12 +85,27 @@ def extract_artifacts(parsed_response)
artifacts
end

def extract_text_from_artifacts(artifacts)
artifact = artifacts.first
return unless artifact['parts']&.any?
def extract_content_from_artifacts(artifacts)
text = nil
select = nil

artifacts.each do |artifact|
next unless artifact.is_a?(Hash) && artifact['parts'].is_a?(Array)

artifact['parts'].each do |part|
next unless part.is_a?(Hash)

if text.nil? && part['type'] == 'text' && part['text'].present?
text = part['text']
end

if select.nil? && part['type'] == 'select'
select = part
end
end
end

text_part = artifact['parts'].find { |p| p['type'] == 'text' }
text_part&.dig('text')
{ text: text, select: select }
end

def process_segmented_response(text_content, conversation)
Expand Down
21 changes: 19 additions & 2 deletions app/services/whatsapp/providers/base_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,34 @@ def create_payload_based_on_items(message)
end
end

def interactive_body_text(message)
content = html_to_whatsapp(message.content.to_s)
return content if content.blank?

lines = content.split("\n")
removed_any = false

while lines.any? && lines.last.strip.match?(/^\d+\.\s+\S/)
lines.pop
removed_any = true
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
end

pruned = lines.join("\n").strip
return content unless removed_any
pruned.presence || content
end

def create_button_payload(message)
buttons = create_buttons(message.content_attributes['items'])
json_hash = { 'buttons' => buttons }
create_payload('button', message.content, JSON.generate(json_hash))
create_payload('button', interactive_body_text(message), JSON.generate(json_hash))
end

def create_list_payload(message)
rows = create_rows(message.content_attributes['items'])
section1 = { 'rows' => rows }
sections = [section1]
json_hash = { :button => 'Choose an item', 'sections' => sections }
create_payload('list', message.content, JSON.generate(json_hash))
create_payload('list', interactive_body_text(message), JSON.generate(json_hash))
end
end
21 changes: 19 additions & 2 deletions app/services/whatsapp/providers/evolution_go_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ def instance_name
whatsapp_channel.provider_config['instance_name']
end

def interactive_body_text(message)
content = html_to_whatsapp(message.content.to_s)
return content if content.blank?

lines = content.split("\n")
removed_any = false

while lines.any? && lines.last.strip.match?(/^\d+\.\s+\S/)
lines.pop
removed_any = true
end

pruned = lines.join("\n").strip
return content unless removed_any
pruned.presence || content
end

def send_interactive_message(phone_number, message)
clean_number = phone_number.delete('+')
items = message.content_attributes&.dig('items') || []
Expand All @@ -233,7 +250,7 @@ def send_button_message(clean_number, message, items)
{ type: 'reply', displayText: item['title'].to_s.truncate(20), id: item['value'].to_s }
end

content = html_to_whatsapp(message.content.to_s)
content = interactive_body_text(message)

body = {
number: clean_number,
Expand Down Expand Up @@ -265,7 +282,7 @@ def send_list_message(clean_number, message, items)
{ rowId: item['value'].to_s, title: item['title'].to_s.truncate(24), description: '' }
end

content = html_to_whatsapp(message.content.to_s)
content = interactive_body_text(message)

body = {
number: clean_number,
Expand Down
21 changes: 19 additions & 2 deletions app/services/whatsapp/providers/evolution_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@ def instance_name
whatsapp_channel.provider_config['instance_name']
end

def interactive_body_text(message)
content = html_to_whatsapp(message.content.to_s)
return content if content.blank?

lines = content.split("\n")
removed_any = false

while lines.any? && lines.last.strip.match?(/^\d+\.\s+\S/)
lines.pop
removed_any = true
end

pruned = lines.join("\n").strip
return content unless removed_any
pruned.presence || content
end

def send_interactive_message(phone_number, message)
clean_number = phone_number.delete('+')
items = message.content_attributes&.dig('items') || []
Expand All @@ -320,7 +337,7 @@ def send_button_message(clean_number, message, items)
{ type: 'reply', displayText: item['title'].to_s.truncate(20), id: item['value'].to_s }
end

content = html_to_whatsapp(message.content.to_s)
content = interactive_body_text(message)

body = {
number: clean_number,
Expand Down Expand Up @@ -348,7 +365,7 @@ def send_list_message(clean_number, message, items)
{ rowId: item['value'].to_s, title: item['title'].to_s.truncate(24), description: '' }
end

content = html_to_whatsapp(message.content.to_s)
content = interactive_body_text(message)

body = {
number: clean_number,
Expand Down
Loading