Skip to content

Commit

Permalink
Refactor Discord bot logic to split response messages in paragraphs (#…
Browse files Browse the repository at this point in the history
…113)

* Refactor Discord bot logic to split response messages in paragraphs

* Refactor split_paragraphs method to use --DIVISION-- as paragraph separator
  • Loading branch information
LuchoAravena authored Oct 28, 2024
1 parent e10a136 commit 589d3ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
24 changes: 4 additions & 20 deletions lib/bas/bot/write_media_review_in_discord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def read
def process
return { success: { review_added: nil } } if unprocessable_response

response = Utils::Discord::Request.write_media_text(params)
response = Utils::Discord::Request.split_paragraphs(params)

if response.code == 200
if !response.empty?
{ success: { message_id: read_response.data["message_id"], property: read_response.data["property"] } }
else
{ error: { message: response.parsed_response, status_code: response.code } }
{ error: { message: "Response is empty" } }
end
end

Expand All @@ -88,27 +88,11 @@ def conditions

def params
{
body:,
body: read_response.data["review"],
secret_token: process_options[:secret_token],
message_id: read_response.data["message_id"],
channel_id: read_response.data["channel_id"]
}
end

def body
{ content: "#{toggle_title}\n\n#{read_response.data["review"]}\n\n#{mention_content}" }
end

def mention_content
author_name = read_response.data["author"]
"<@#{author_name}>"
end

def toggle_title
case read_response.data["media_type"]
when "images" then "Image review results"
when "paragraph" then "Text review results"
end
end
end
end
17 changes: 15 additions & 2 deletions lib/bas/utils/discord/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,23 @@ def self.get_discord_images(message)
}
end

def self.write_media_text(params)
def self.write_media_text(params, combined_paragraphs)
url_message = URI.parse("#{DISCORD_BASE_URL}/channels/#{params[:channel_id]}/messages")
headers = headers(params[:secret_token])
HTTParty.post(url_message, { body: params[:body].to_json, headers: })
body = { content: combined_paragraphs }.to_json

HTTParty.post(url_message, { body:, headers: })
end

def self.split_paragraphs(params)
paragraphs = params[:body].split("--DIVISION--").map(&:strip).reject(&:empty?)

paragraphs.each_slice(2) do |paragraph|
next if paragraph.empty?

combined_paragraphs = paragraph.join("\n\n")
write_media_text(params, combined_paragraphs)
end
end

def self.headers(secret_token)
Expand Down
8 changes: 3 additions & 5 deletions spec/bas/bot/write_media_review_in_discord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"message_id" => "1285685692772646922", "channel_id" => "1285685692772646933", "media_type" => "images" }
end

let(:error_response) { { "object" => "error", "status" => 404, "message" => "not found" } }
let(:error_response) { { "object" => "error", "message" => "Response is empty" } }

let(:response) { double("https_response") }

Expand All @@ -99,13 +99,11 @@
end

it "returns an error hash with the error message" do
allow(response).to receive(:code).and_return(404)

allow(response).to receive(:parsed_response).and_return(error_response)
allow(Utils::Discord::Request).to receive(:split_paragraphs).and_return([])

processed = @bot.process

expect(processed).to eq({ error: { message: error_response, status_code: 404 } })
expect(processed).to eq({ error: { message: "Response is empty" } })
end
end

Expand Down

0 comments on commit 589d3ab

Please sign in to comment.