summaryrefslogtreecommitdiff
path: root/lib/microsoft_teams/notifier.rb
blob: 8e422575636d9cf5939409722c9a0988ff70164a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module MicrosoftTeams
  class Notifier
    def initialize(webhook)
      @webhook = webhook
    end

    def ping(options = {})
      HTTParty.post(
        @webhook.to_str,
        headers: { 'Content-type' => 'application/json' },
        body: body(options)
      )
    end

    private

    def body(options = {})
      result = { 'sections' => [] }

      result['title'] = options[:title] if options[:title]
      result['summary'] = options[:activity][:title]
      result['sections'] << {
        'activityTitle' => options[:activity][:title],
        'activitySubtitle' => options[:activity][:subtitle],
        'activityText' => options[:activity][:text],
        'activityImage' => options[:activity][:image]
      }

      if options[:attachments].present? && !options[:attachments].empty?
        result['sections'] << { 'title' => 'Details', 'facts' => attachments(options[:attachments]) }
      end

      result.to_json
    end

    def attachments(content)
      [{ 'name' => 'Attachments', 'value' => content }]
    end
  end
end