-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from ZaikoXander/feat/support-file-sending
Feat/support file sending
- Loading branch information
Showing
11 changed files
with
216 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'net/http' | ||
require 'openssl' | ||
require 'uri' | ||
require 'open-uri' | ||
|
||
class Chatwoot::SendFileToChatwootRequest < Micro::Case | ||
attributes :account_id | ||
attributes :conversation_id | ||
attribute :botpress_response | ||
|
||
attributes :chatwoot_endpoint | ||
attributes :chatwoot_bot_token | ||
|
||
def call! | ||
file_url = botpress_response['image'] || botpress_response['file'] || botpress_response['video'] | ||
file_name = get_file_name_by_url(file_url) | ||
file = download_file(file_url) | ||
|
||
url = URI("#{chatwoot_endpoint}/api/v1/accounts/#{account_id}/conversations/#{conversation_id}/messages") | ||
|
||
http = Net::HTTP.new(url.host, url.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
|
||
request = Net::HTTP::Post.new(url) | ||
request['api_access_token'] = chatwoot_bot_token | ||
|
||
form_data = [['attachments[]', file, { filename: file_name , content_type: file.content_type }]] | ||
|
||
unless botpress_response['title'].nil? | ||
form_data << ['content', botpress_response['title']] | ||
end | ||
|
||
request.set_form(form_data, 'multipart/form-data') | ||
response = http.request(request) | ||
|
||
Rails.logger.info("Chatwoot response") | ||
Rails.logger.info("Status code: #{response.code}") | ||
Rails.logger.info("Body: #{response.body}") | ||
|
||
if response.code == "200" | ||
Success result: JSON.parse(response.body) | ||
elsif response.code == "404" && response.body.include?('Resource could not be found') | ||
Failure result: { message: 'Chatwoot resource could not be found' } | ||
elsif response.code == "404" | ||
Failure result: { message: 'Invalid chatwoot endpoint' } | ||
elsif response.code == "401" | ||
Failure result: { message: 'Invalid chatwoot access token' } | ||
else | ||
Failure result: { message: 'Chatwoot server error' } | ||
end | ||
end | ||
|
||
def download_file(file_url) | ||
begin | ||
OpenURI::open_uri(file_url) | ||
rescue | ||
raise "File url is invalid" | ||
end | ||
end | ||
|
||
def get_file_name_by_url(file_url) | ||
incoming_file_name = File.basename(file_url) | ||
raise "Invalid file name" unless incoming_file_name.include?('-') | ||
incoming_file_name.sub(/^[^-]+-/, '') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
18 changes: 18 additions & 0 deletions
18
test/use_cases/chatwoot/send_file_to_chatwoot_request_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require "test_helper" | ||
|
||
class SendFileToChatwootRequestTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@service = Chatwoot::SendFileToChatwootRequest.send(:new, {}) | ||
end | ||
|
||
test "should return valid file name" do | ||
result = @service.get_file_name_by_url('https://app.botpress.zimobi.com.br/api/v1/bots/teste-file/media/42rghrwr2zndfjw3fn4q-C%C3%B3pia%20de%20Wo%20(1024%C2%A0%C3%97%C2%A01024%C2%A0px)%20(2).png') | ||
assert_equal result, 'C%C3%B3pia%20de%20Wo%20(1024%C2%A0%C3%97%C2%A01024%C2%A0px)%20(2).png' | ||
end | ||
|
||
test "should throw invalid file name error" do | ||
assert_raises "Invalid file name" do | ||
@service.get_file_name_by_url('https://app.botpress.zimobi.com.br/api/v1/bots/teste-file/media/42rghrwr2zndfjw3fn4q_C%C3%B3pia%20de%20Wo%20(1024%C2%A0%C3%97%C2%A01024%C2%A0px)%20(2).png') | ||
end | ||
end | ||
end |