class MsteamsNotifier::Message

Public Class Methods

new(options={}) click to toggle source
# File lib/msteams_notifier.rb, line 15
  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) : '')
@items = []
@actions = []
  end
quick_message(message) click to toggle source

A quick method to send a text notification

# File lib/msteams_notifier.rb, line 9
def self.quick_message(message)
  notifier = MsteamsNotifier::Message.new
  notifier.add_text(message)
  notifier.send
end

Public Instance Methods

add_action(title, url) click to toggle source
# File lib/msteams_notifier.rb, line 39
def add_action(title, url)
  @actions << {
      "type": "Action.OpenUrl",
      "title": title,
      "url": url
    }
end
add_facts(facts) click to toggle source

facts: [{title: 'Label', value: 'Thing'}]

# File lib/msteams_notifier.rb, line 48
def add_facts(facts)
  @items << {
    "type": "FactSet",
    facts: facts
  }
end
add_text(text) click to toggle source
# File lib/msteams_notifier.rb, line 31
def add_text(text)
  @items << {
      "type": "TextBlock",
      "text": text,
      "wrap": true
    }
end
add_title(title) click to toggle source
# File lib/msteams_notifier.rb, line 22
def add_title(title)
  @items << {
      "type": "TextBlock",
      "text": title,
      "weight": "bolder",
      "size": "medium"
    }
end
json_payload() click to toggle source
# File lib/msteams_notifier.rb, line 75
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
send() click to toggle source
# File lib/msteams_notifier.rb, line 60
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.body = json_payload
    response = http.request(request)
    response.is_a?(Net::HTTPSuccess)
  rescue
    false
  end
    end
sending_enabled?() click to toggle source

Should we really send the message. Helpful when in development

# File lib/msteams_notifier.rb, line 56
def sending_enabled?
  @enabled
end