Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 53 additions & 28 deletions app/models/pager_tree/integrations/channel/microsoft_teams/v3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Channel::MicrosoftTeams::V3 < Integration
{key: :alert_resolved, type: :boolean, default: false},
{key: :alert_dropped, type: :boolean, default: false},
{key: :outgoing_rules, type: :string, default: nil},
{key: :time_zone, type: :string, default: nil}
{key: :time_zone, type: :string, default: nil},
{key: :compact_message, type: :boolean, default: false}
]
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"

Expand All @@ -22,6 +23,7 @@ class Channel::MicrosoftTeams::V3 < Integration
self.option_alert_dropped ||= false
self.option_outgoing_rules ||= ""
self.option_time_zone ||= "UTC"
self.option_compact_message ||= false
end

def converts_to
Expand Down Expand Up @@ -82,40 +84,21 @@ def _alert
end

def _blocks
facts = if option_compact_message
_compact_facts
else
_full_facts
end

{
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
themeColor: _color,
summary: _title,
sections: [{
activityTitle: _title,
activitySubtitle: _alert.description&.try(:to_plain_text),
facts: [
{
name: "Status",
value: _alert.status.upcase
},
{
name: "Urgency",
value: _alert.urgency.upcase
},
{
name: "Created",
value: _alert.created_at.in_time_zone(option_time_zone).iso8601
},
{
name: "Source",
value: _alert.source&.name
},
{
name: "Destinations",
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
},
{
name: "User",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
],
activitySubtitle: option_compact_message ? nil : _alert.description&.try(:to_plain_text),
facts: facts,
markdown: true
}],
potentialAction: [
Expand All @@ -131,6 +114,48 @@ def _blocks
}
end

def _compact_facts
[
{
name: "Status",
value: _alert.status.upcase
},
{
name: "User",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
]
end

def _full_facts
[
{
name: "Status",
value: _alert.status.upcase
},
{
name: "Urgency",
value: _alert.urgency.upcase
},
{
name: "Created",
value: _alert.created_at.in_time_zone(option_time_zone).iso8601
},
{
name: "Source",
value: _alert.source&.name
},
{
name: "Destinations",
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
},
{
name: "User",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
]
end

def _title
return @_title if @_title.present?

Expand Down
211 changes: 127 additions & 84 deletions app/models/pager_tree/integrations/channel/microsoft_teams/v4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class Channel::MicrosoftTeams::V4 < Integration
{key: :alert_resolved, type: :boolean, default: false},
{key: :alert_dropped, type: :boolean, default: false},
{key: :outgoing_rules, type: :string, default: nil},
{key: :time_zone, type: :string, default: nil}
{key: :time_zone, type: :string, default: nil},
{key: :compact_message, type: :boolean, default: false}
]
store_accessor :options, *OPTIONS.map { |x| x[:key] }.map(&:to_s), prefix: "option"

Expand All @@ -22,6 +23,7 @@ class Channel::MicrosoftTeams::V4 < Integration
self.option_alert_dropped ||= false
self.option_outgoing_rules ||= ""
self.option_time_zone ||= "UTC"
self.option_compact_message ||= false
end

def adapter_supports_incoming?
Expand Down Expand Up @@ -78,6 +80,12 @@ def _alert
end

def _blocks
body = if option_compact_message
_compact_body
else
_full_body
end

{
type: "message",
attachments: [
Expand All @@ -86,105 +94,140 @@ def _blocks
contentUrl: nil,
content: {
type: "AdaptiveCard",
body: [
body: body,
actions: [
{
type: "Container",
backgroundImage: _color,
items: [
{
type: "TextBlock",
size: "Large",
weight: "Bolder",
text: _title
},
{
type: "ColumnSet",
columns: [
{
type: "Column",
items: [
{
type: "TextBlock",
weight: "Bolder",
text: _title,
wrap: true
},
{
type: "TextBlock",
spacing: "None",
text: "Created #{_alert.created_at.in_time_zone(option_time_zone).iso8601}",
wrap: true
}
],
width: "stretch"
}
]
}
]
},
type: "Action.OpenUrl",
title: "View",
url: Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}"),
style: "positive"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
version: "1.2"
}
}
]
}
end

def _compact_body
[
{
type: "Container",
backgroundImage: _color,
items: [
{
type: "TextBlock",
size: "Large",
weight: "Bolder",
text: _title
},
{
type: "FactSet",
facts: [
{
type: "Container",
items: [
{
type: "FactSet",
facts: [
{
title: "Status:",
value: _alert.status&.upcase
}, {
title: "Urgency:",
value: _alert.urgency&.upcase
}, {
title: "Source:",
value: _alert.source&.name
}, {
title: "Destinations:",
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
}, {
title: "User:",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
],
spacing: "None"
}
],
spacing: "Medium"
title: "Status:",
value: _alert.status&.upcase
},
{
type: "Container",
title: "User:",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
],
spacing: "ExtraSmall"
}
]
}
]
end

def _full_body
[
{
type: "Container",
backgroundImage: _color,
items: [
{
type: "TextBlock",
size: "Large",
weight: "Bolder",
text: _title
},
{
type: "ColumnSet",
columns: [
{
type: "Column",
items: [
{
type: "TextBlock",
text: _alert.description&.try(:to_plain_text),
wrap: true,
separator: true,
maxLines: 24
weight: "Bolder",
text: _title,
wrap: true
},
{
type: "FactSet",
facts: _alert.additional_data&.map { |ad| {title: ad["label"], value: Array(ad["value"]).join(", ")} } || [],
spacing: "Medium",
separator: true
type: "TextBlock",
spacing: "None",
text: "Created #{_alert.created_at.in_time_zone(option_time_zone).iso8601}",
wrap: true
}
],
spacing: "Medium",
separator: true
width: "stretch"
}
],
actions: [
]
}
]
},
{
type: "Container",
items: [
{
type: "FactSet",
facts: [
{
type: "Action.OpenUrl",
title: "View",
url: Rails.application.routes.url_helpers.try(:alert_url, _alert, script_name: "/#{_alert.account_id}"),
style: "positive"
title: "Status:",
value: _alert.status&.upcase
}, {
title: "Urgency:",
value: _alert.urgency&.upcase
}, {
title: "Source:",
value: _alert.source&.name
}, {
title: "Destinations:",
value: _alert.alert_destinations&.map { |d| d.destination.name }&.join(", ")
}, {
title: "User:",
value: _alert.alert_responders&.where(role: :incident_commander)&.includes(account_user: :user)&.first&.account_user&.name
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
version: "1.2"
spacing: "None"
}
}
]
}
],
spacing: "Medium"
},
{
type: "Container",
items: [
{
type: "TextBlock",
text: _alert.description&.try(:to_plain_text),
wrap: true,
separator: true,
maxLines: 24
},
{
type: "FactSet",
facts: _alert.additional_data&.map { |ad| {title: ad["label"], value: Array(ad["value"]).join(", ")} } || [],
spacing: "Medium",
separator: true
}
],
spacing: "Medium",
separator: true
}
]
end

def _title
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:alert_acknowledged,
:alert_resolved,
:alert_dropped,
:compact_message,
]
%>
<% opts.each do |opt| %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
:alert_acknowledged,
:alert_resolved,
:alert_dropped,
:compact_message,
]
%>
<% opts.each do |opt| %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:alert_acknowledged,
:alert_resolved,
:alert_dropped,
:compact_message,
]
%>
<% opts.each do |opt| %>
Expand Down
Loading