Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support to choice buttons sending #21

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 14 deletions app/use_cases/chatwoot/receive_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@ def call!
process_event(event)
end

def valid_event?(event)
event['event'] == 'message_created' && event['message_type'] == 'incoming' && valid_status?(event['conversation']['status'])
end

def valid_status?(status)
if ENV['CHATWOOT_ALLOWED_STATUSES'].present?
allowed_statuses = ENV['CHATWOOT_ALLOWED_STATUSES'].split(',')
else
allowed_statuses = %w[pending]
end
allowed_statuses.include?(status)
end

def process_event(event)
if Chatwoot::ValidEvent.call(event: event).success?
botpress_endpoint = event['botpress_endpoint'] || ENV['BOTPRESS_ENDPOINT']
Expand Down Expand Up @@ -47,4 +34,4 @@ def process_event(event)
Failure result: { message: 'Invalid event' }
end
end
end
end
16 changes: 12 additions & 4 deletions app/use_cases/chatwoot/send_to_botpress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ class Chatwoot::SendToBotpress < Micro::Case

def call!
conversation_id = event['conversation']['id']
message_content = event['content']
if (
event['content_type'] == 'input_select' &&
event['content_attributes']['submitted_values'].respond_to?(:first)
)
message_content = event['content_attributes']['submitted_values'].first['value']
else
message_content = event['content']
end

url = "#{botpress_endpoint}/api/v1/bots/#{botpress_bot_id}/converse/#{conversation_id}"

body = {
'text': "#{message_content}",
'text': message_content,
'type': 'text',
'metadata': {
'event': event
Expand All @@ -24,9 +32,9 @@ def call!
Rails.logger.info("Status code: #{response.status}")
Rails.logger.info("Body: #{response.body}")

if (response.status == 200)
if response.status == 200
Success result: JSON.parse(response.body)
elsif (response.status == 404 && response.body.include?('Invalid Bot ID'))
elsif response.status == 404 && response.body.include?('Invalid Bot ID')
Failure result: { message: 'Invalid Bot ID' }
else
Failure result: { message: 'Invalid botpress endpoint' }
Expand Down
9 changes: 7 additions & 2 deletions app/use_cases/chatwoot/valid_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ class Chatwoot::ValidEvent < Micro::Case
attributes :event

def call!
if (
if (
event['event'] == 'message_created' &&
event['message_type'] == 'incoming' &&
valid_status?(event['conversation']['status'])
) || (
event['event'] == 'message_updated' &&
event['message_type'] == 'outgoing' &&
event['content_type'] == 'input_select' &&
event['content_attributes']['submitted_values'].respond_to?(:first)
)
Success result: {event: event}
Success result: { event: event }
else
Failure result: { message: 'Invalid event' }
end
Expand Down