Skip to content

Commit

Permalink
[message.rb]: use helper to distinguish between webhook responses
Browse files Browse the repository at this point in the history
Adds two helper functions that encapsulate the properties of a
successful connector based webhook and a workflow based webhook.

Up to now, workflow based webhooks lead to a response where there
response code is 202 and the response body is empty.
For connector based webhooks, the status code is 200 and the response
body is set to "1".
  • Loading branch information
huNt-FMJ committed Dec 5, 2024
1 parent f655832 commit 99feb09
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/msteams_hermes/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
require "net/http"
require "json"

module MsTeamsWebhookType
def self.mst_workflow_webhook_response?(response)
response.code == "202" and response.body.empty?
end

def self.mst_connector_webhook_response?(response)
# For details see:
# https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL%2Ctext1#send-messages-using-curl-and-powershell
response.code == "200" and response.body == "1"
end
end

module MsTeamsHermes
##
# A class representing Microsoft's webhook message object
Expand Down Expand Up @@ -62,15 +74,11 @@ def deliver

response = http.request(req)

# For details see:
# https://learn.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using?tabs=cURL%2Ctext1#send-messages-using-curl-and-powershell
if not response.body.empty? and response.body != "1"
raise MessageBodyTooLargeError, body_json.bytesize if response.body.include? MSTEAMS_MESSAGE_413_ERROR_TOKEN

raise UnknownError, response.body
end
return response if MsTeamsWebhookType::mst_workflow_webhook_response?(response)
return response if MsTeamsWebhookType::mst_connector_webhook_response?(response)

response
raise MessageBodyTooLargeError, body_json.bytesize if response.body.include? MSTEAMS_MESSAGE_413_ERROR_TOKEN
raise UnknownError, response.body
end
end
# rubocop:enable Metrics/AbcSize
Expand Down

0 comments on commit 99feb09

Please sign in to comment.