diff --git a/complex_example.rb b/complex_example.rb index 55cd6df..66926df 100755 --- a/complex_example.rb +++ b/complex_example.rb @@ -19,7 +19,7 @@ weight: MsTeamsHermes::Style::FontWeight::BOLDER ), MsTeamsHermes::Components::TextBlock.new( - text: "Saturday, 1 January 2022" + text: Time.now.strftime("%A, %d %B %Y %H:%M:%S").to_s ) ] ) diff --git a/lib/msteams_hermes/message.rb b/lib/msteams_hermes/message.rb index 3de5257..01d779d 100644 --- a/lib/msteams_hermes/message.rb +++ b/lib/msteams_hermes/message.rb @@ -3,6 +3,15 @@ require "net/http" require "json" +## +# Module to encapsulate the logic that decides whether a given response +# is of type workflow or connector type webhook. +# Both types must be considered differently to support both types of +# MSTeams webhooks. +## +module MsTeamsWebhookType +end + module MsTeamsHermes ## # A class representing Microsoft's webhook message object @@ -62,15 +71,12 @@ 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 response.body != "1" - raise MessageBodyTooLargeError, body_json.bytesize if response.body.include? MSTEAMS_MESSAGE_413_ERROR_TOKEN + return response if response_from_mst_workflow_webhook?(response) || + response_from_mst_connector_webhook?(response) - raise UnknownError, response.body - end + raise MessageBodyTooLargeError, body_json.bytesize if message_too_large?(response) - response + raise UnknownError, response.body end end # rubocop:enable Metrics/AbcSize @@ -93,5 +99,21 @@ def body_json ] }.to_json end + + private + + def response_from_mst_workflow_webhook?(response) + response.code == "202" && response.body.empty? + end + + def response_from_mst_connector_webhook?(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" && response.body == "1" + end + + def message_too_large?(response) + response.body.include? MSTEAMS_MESSAGE_413_ERROR_TOKEN + end end end