From 6dedbcc1ff1e0b9034a0201d3487a5324755c4d1 Mon Sep 17 00:00:00 2001 From: Nej <2405937+huNt-FMJ@users.noreply.github.com> Date: Thu, 19 Dec 2024 10:17:24 +0100 Subject: [PATCH] Support msteams workflows (#10) * [message.rb]: response.body can be empty on workflow responses * [complex_example.rb]: Use dynamic datetime in text * [message.rb]: use helper to distinguish between webhook responses 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". * [complex_example.rb]: Rubocop says to use '.to_s` * [message.rb]: Add comment and use '.' to call functions * Move funcs to identify response type into class Message as per PR review * Check whether sent msg is too large in dedicated method - Minor formatting change to simplify 'return response' statement * [message.rb] Use '&&' instead of 'and' more ruby-ish? --- complex_example.rb | 2 +- lib/msteams_hermes/message.rb | 36 ++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) 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