Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support msteams workflows #10

Merged
merged 8 commits into from
Dec 19, 2024
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
huNt-FMJ marked this conversation as resolved.
Show resolved Hide resolved
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
Loading