Class: MsteamsWebhook::Message

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

Instance Method Summary collapse

Constructor Details

#initialize(text, title = nil) ⇒ Message

Returns a new instance of Message.



9
10
11
12
# File 'lib/msteams_webhook/message.rb', line 9

def initialize(text, title=nil)
  @text = text
  @title = title
end

Instance Method Details

#add_action(text, url) ⇒ Object



69
70
71
72
73
74
75
76
77
# File 'lib/msteams_webhook/message.rb', line 69

def add_action(text,url)
  @potential_action = [] unless @potential_action
  @potential_action << {
    :@context => 'http://schema.org',
    :@type => 'ViewAction',
    :name => text,
    :target => [url]
  }
end

#add_activity(text, title = nil, image = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/msteams_webhook/message.rb', line 35

def add_activity(text,title=nil,image=nil)
  activity = {}
  activity[:activityTitle] = title if title
  activity[:activityText] = text if text
  activity[:activityImage] = image if image

  @sections = [] unless @sections
  @sections << activity
end

#add_facts(title, array) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/msteams_webhook/message.rb', line 45

def add_facts(title,array)
  section = {}
  section[:title] = title if title
  section[:facts] = []
  array.each { |name,value| section[:facts] << { :name => name, :value => value } }

  @sections = [] unless @sections
  @sections << section
end

#add_image(title, image) ⇒ Object



55
56
57
# File 'lib/msteams_webhook/message.rb', line 55

def add_image(title,image)
  add_images(title,[image])
end

#add_images(title, images) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/msteams_webhook/message.rb', line 59

def add_images(title,images)
  section = {}
  section[:title] = title
  section[:images] = []
  images.each { |image| section[:images] << { :image => image } }

  @sections = [] unless @sections
  @sections << section 
end

#as_jsonObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msteams_webhook/message.rb', line 14

def as_json
  msg = {}
  msg[:summary] = @summary if @summary
  msg[:title] = @title if @title
  msg[:text] = @text if @text
  msg[:themeColor] = @color if @color

  msg[:sections] = @sections if @sections
       msg[:potentialAction] = @potential_action if @potential_action
       
       msg
end

#send(url, async = true) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/msteams_webhook/message.rb', line 79

def send(url, async=true)
       client = HTTPClient.new
       client.ssl_config.cert_store.set_default_paths
       client.ssl_config.ssl_version = :auto
       if async
           client.post_async url, to_json
       else
           client.post url, to_json
       end
end

#set_color(color) ⇒ Object



31
32
33
# File 'lib/msteams_webhook/message.rb', line 31

def set_color(color)
  @color = color
end

#to_jsonObject



27
28
29
# File 'lib/msteams_webhook/message.rb', line 27

def to_json
       as_json.to_json
end