Skip to content

Commit

Permalink
Support msteams workflows (#10)
Browse files Browse the repository at this point in the history
* [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?
  • Loading branch information
huNt-FMJ authored Dec 19, 2024
1 parent 00b56c0 commit 6dedbcc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion complex_example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
]
)
Expand Down
36 changes: 29 additions & 7 deletions lib/msteams_hermes/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 6dedbcc

Please sign in to comment.