Class: MsteamsNotifier::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/msteams_notifier.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Message

Returns a new instance of Message.



23
24
25
26
27
28
29
# File 'lib/msteams_notifier.rb', line 23

def initialize(options={})
   @enabled = options[:enabled] || (defined?(Rails) ? Rails.application.credentials.dig(:ms_teams, :enabled).to_s == "1" : true)
   @webhook_url = options[:webhook_url] || (defined?(Rails) ? Rails.application.credentials.dig(:ms_teams, :webhook_url) : "")
   @quick_title = options[:quick_title] || (defined?(Rails) ? Rails.application.credentials.dig(:ms_teams, :quick_title) : nil)
   @items = []
   @actions = []
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



10
11
12
# File 'lib/msteams_notifier.rb', line 10

def enabled
  @enabled
end

#quick_titleObject

Returns the value of attribute quick_title.



10
11
12
# File 'lib/msteams_notifier.rb', line 10

def quick_title
  @quick_title
end

#webhook_urlObject

Returns the value of attribute webhook_url.



10
11
12
# File 'lib/msteams_notifier.rb', line 10

def webhook_url
  @webhook_url
end

Class Method Details

.quick_message(message) ⇒ Object

A quick method to send a text notification



13
14
15
16
17
18
19
20
21
# File 'lib/msteams_notifier.rb', line 13

def self.quick_message(message)
  notifier = MsteamsNotifier::Message.new
  if !notifier.quick_title.nil? && !notifier.quick_title != ""
    notifier.add_title notifier.quick_title
  end
  notifier.add_text(message)
  
  notifier.send
end

Instance Method Details

#add_action(title, url) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/msteams_notifier.rb', line 48

def add_action(title, url)
  @actions << {
      "type": "Action.OpenUrl",
      "title": title,
      "url": url
    }
end

#add_facts(facts) ⇒ Object

facts: [‘Label’, value: ‘Thing’]



57
58
59
60
61
62
# File 'lib/msteams_notifier.rb', line 57

def add_facts(facts)
  @items << {
    "type": "FactSet",
    facts: facts
  }
end

#add_text(text) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/msteams_notifier.rb', line 40

def add_text(text)
  @items << {
      "type": "TextBlock",
      "text": text,
      "wrap": true
    }
end

#add_title(title) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/msteams_notifier.rb', line 31

def add_title(title)
  @items << {
      "type": "TextBlock",
      "text": title,
      "weight": "bolder",
      "size": "medium"
    }
end

#json_payloadObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/msteams_notifier.rb', line 87

def json_payload
   {
    "type": "message",
    "attachments":[
       {
          "contentType": "application/vnd.microsoft.card.adaptive",
          "contentUrl": nil,
          "content": {
             "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
             "type": "AdaptiveCard",
             "version": "1.2",
             "body": [
               {
                 "type": "Container",
                 "items": @items
               }
             ],
             "actions": @actions
          }
       }
    ]
  }.to_json
end

#sendObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/msteams_notifier.rb', line 69

def send
   return false unless sending_enabled?
   begin
     uri = URI(@webhook_url.to_s)
     http = Net::HTTP.new(uri.host, uri.port)
     http.use_ssl = true
     request = Net::HTTP::Post.new(uri.request_uri)
     request["Content-Type"] = "application/json"
     request.body = json_payload
     response = http.request(request)
     response.is_a?(Net::HTTPSuccess)
   rescue => e
     puts e.message
     puts e.backtrace
     false
   end
end

#sending_enabled?Boolean

Should we really send the message. Helpful when in development

Returns:

  • (Boolean)


65
66
67
# File 'lib/msteams_notifier.rb', line 65

def sending_enabled?
  @enabled
end