From 6288a10633aad0e3d155db0f253d0f417e93ea4c Mon Sep 17 00:00:00 2001 From: Marcello Costagliola Date: Mon, 5 Oct 2026 02:58:11 +0200 Subject: [PATCH] Post a webhook reply as an attachment only when the bot answered 200 A bot's reply becomes a message when the status is 200 and the type is text, and an attachment otherwise, but the attachment branch never looked at the status. Whenever a bot's endpoint failed, its error page landed in the room as a file: a proxy's 502 page as attachment.html, a 404 as attachment.text. Apply the text branch's 200 check to attachments too. The error reply test answered without a content type, which skipped the attachment branch, so it now answers with an HTML error page. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01JVFo3Lt9T8M5NR7KxVvsZ2 --- app/models/webhook.rb | 2 +- test/models/webhook_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 5c307e7..f386e5f 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -63,7 +63,7 @@ class Webhook < ApplicationRecord end def extract_attachment_from(response) - if response.content_type && mime_type = Mime::Type.lookup(response.content_type) + if response.code == "200" && response.content_type && mime_type = Mime::Type.lookup(response.content_type) ActiveStorage::Blob.create_and_upload! \ io: StringIO.new(response.body), filename: "attachment.#{mime_type.symbol}", content_type: mime_type.to_s end diff --git a/test/models/webhook_test.rb b/test/models/webhook_test.rb index 4f4e3bc..d37192a 100644 --- a/test/models/webhook_test.rb +++ b/test/models/webhook_test.rb @@ -41,7 +41,7 @@ class WebhookTest < ActiveSupport::TestCase test "delivery with error reply" do assert_no_difference -> { Message.count } do - WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 500, body: "Internal Error!", headers: {}) + WebMock.stub_request(:post, webhooks(:bender).url).to_return(status: 500, body: "

Internal Error!

", headers: { "Content-Type" => "text/html" }) response = webhooks(:bender).deliver(messages(:first)) end end