diff options
Diffstat (limited to 'lib/microsoft_teams/notifier.rb')
-rw-r--r-- | lib/microsoft_teams/notifier.rb | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/lib/microsoft_teams/notifier.rb b/lib/microsoft_teams/notifier.rb index a5d2fe15ab6..3bef68a1bcb 100644 --- a/lib/microsoft_teams/notifier.rb +++ b/lib/microsoft_teams/notifier.rb @@ -2,30 +2,43 @@ module MicrosoftTeams class Notifier def initialize(webhook) @webhook = webhook + @header = { 'Content-type' => 'application/json' } end def ping(options = {}) - HTTParty.post( - @webhook.to_str, - headers: { 'Content-type' => 'application/json' }, - body: body(options) - ) + result = false + + begin + response = HTTParty.post( + @webhook.to_str, + headers: @header, + body: body(options) + ) + + result = true if response + rescue HTTParty::Error, StandardError => error + Rails.logger.info("#{self.class.name}: Error while connecting to #{@webhook}: #{error.message}") + end + + result end private def body(options = {}) - attachments = options[:attachments] result = { 'sections' => [] } result['title'] = options[:title] result['summary'] = options[:pretext] - result['sections'] << options[:activity] + result['sections'] << MicrosoftTeams::Activity.new(options[:activity]).prepare - result['sections'] << { - 'title' => 'Details', - 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] - } if attachments.present? && attachments.empty? + attachments = options[:attachments] + unless attachments.blank? + result['sections'] << { + 'title' => 'Details', + 'facts' => [{ 'name' => 'Attachments', 'value' => attachments }] + } + end result.to_json end |