Class: Teams::Api::MessageActivity

Inherits:
Object
  • Object
show all
Defined in:
lib/teams/api/message_activity.rb

Constant Summary collapse

TEXT_FORMATS =

"extendedmarkdown" is in public preview and may be subject to change.

%w[plain markdown xml extendedmarkdown].freeze
FEEDBACK_MODES =
%w[default custom].freeze
AI_MESSAGE_ENTITY_TYPE =
"https://schema.org/Message"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, id: nil, attachments: [], text_format: nil, summary: nil, input_hint: nil) ⇒ MessageActivity

Returns a new instance of MessageActivity.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/teams/api/message_activity.rb', line 13

def initialize(text = nil, id: nil, attachments: [], text_format: nil, summary: nil, input_hint: nil)
  @id = id
  @text = text
  @attachments = attachments
  @text_format = normalize_text_format(text_format)
  @summary = summary
  @input_hint = input_hint
  @entities = []
  @channel_data = {}
  @recipient = nil
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def attachments
  @attachments
end

#channel_dataObject (readonly)

Returns the value of attribute channel_data.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def channel_data
  @channel_data
end

#entitiesObject (readonly)

Returns the value of attribute entities.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def entities
  @entities
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def id
  @id
end

#input_hintObject (readonly)

Returns the value of attribute input_hint.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def input_hint
  @input_hint
end

#recipientObject (readonly)

Returns the value of attribute recipient.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def recipient
  @recipient
end

#summaryObject (readonly)

Returns the value of attribute summary.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def summary
  @summary
end

#textObject (readonly)

Returns the value of attribute text.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def text
  @text
end

#text_formatObject (readonly)

Returns the value of attribute text_format.



11
12
13
# File 'lib/teams/api/message_activity.rb', line 11

def text_format
  @text_format
end

Instance Method Details

#add_ai_generatedObject



88
89
90
91
92
93
94
95
# File 'lib/teams/api/message_activity.rb', line 88

def add_ai_generated
  entity = ensure_single_root_level_message_entity
  additional_types = Array(entity["additionalType"])
  return self if additional_types.include?("AIGeneratedContent")

  entity["additionalType"] = additional_types + ["AIGeneratedContent"]
  self
end

#add_card(card) ⇒ Object



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

def add_card(card)
  @attachments << {
    "contentType" => "application/vnd.microsoft.card.adaptive",
    "content" => card.respond_to?(:to_h) ? card.to_h : card
  }
  self
end

#add_citation(position, appearance) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/teams/api/message_activity.rb', line 127

def add_citation(position, appearance)
  entity = ensure_single_root_level_message_entity
  entity["citation"] ||= []
  entity["citation"] << {
    "@type" => "Claim",
    "position" => position,
    "appearance" => citation_appearance(appearance).to_h
  }
  self
end

#add_feedback(mode = "default") ⇒ Object



138
139
140
141
142
# File 'lib/teams/api/message_activity.rb', line 138

def add_feedback(mode = "default")
  @channel_data["feedbackLoop"] = { "type" => normalize_feedback_mode(mode) }
  @channel_data.delete("feedbackLoopEnabled")
  self
end

#add_mention(account, text: nil, add_text: true) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/teams/api/message_activity.rb', line 97

def add_mention(, text: nil, add_text: true)
   = .respond_to?(:to_h) ? .to_h : 
  mention_text = text || ["name"]

  self.add_text("<at>#{mention_text}</at>") if add_text

  @entities << {
    "type" => "mention",
    "mentioned" => ,
    "text" => "<at>#{mention_text}</at>"
  }
  self
end

#add_quote(message_id, text = nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/teams/api/message_activity.rb', line 30

def add_quote(message_id, text = nil)
  @entities << QuotedReplyEntity.new("quotedReply" => { "messageId" => message_id })
  add_text(%(<quoted messageId="#{message_id}"/>))
  add_text(" #{text}") if text
  self
end

#add_sensitivity_label(name, description: nil, pattern: nil) ⇒ Object

Adds a content sensitivity label. The label is carried as usageInfo on the root schema.org/Message entity, the wire shape TypeScript, Python, and the Teams documentation use.



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/teams/api/message_activity.rb', line 114

def add_sensitivity_label(name, description: nil, pattern: nil)
  usage_info = {
    "type" => AI_MESSAGE_ENTITY_TYPE,
    "@type" => "CreativeWork",
    "name" => name
  }
  usage_info["description"] = description if description
  usage_info["pattern"] = pattern if pattern

  ensure_single_root_level_message_entity["usageInfo"] = usage_info
  self
end

#add_targeted_message_info(message_id) ⇒ Object

Adds a targetedMessageInfo entity for prompt preview, once per message. Any quotedReply entities and the matching quote placeholder are removed to avoid collision with the prompt preview.



77
78
79
80
81
82
83
84
85
86
# File 'lib/teams/api/message_activity.rb', line 77

def add_targeted_message_info(message_id)
  @entities.reject! { |entity| quoted_reply_entity?(entity) }
  @text = text.gsub(%(<quoted messageId="#{message_id}"/>), "").strip if text

  unless @entities.any? { |entity| entity_type(entity) == "targetedMessageInfo" }
    @entities << { "type" => "targetedMessageInfo", "messageId" => message_id }
  end

  self
end

#add_text(value) ⇒ Object



25
26
27
28
# File 'lib/teams/api/message_activity.rb', line 25

def add_text(value)
  @text = "#{text}#{value}"
  self
end

#get_quoted_messagesObject



44
45
46
# File 'lib/teams/api/message_activity.rb', line 44

def get_quoted_messages
  entities.select { |entity| quoted_reply_entity?(entity) }
end

#prepend_quote(message_id) ⇒ Object



37
38
39
40
41
42
# File 'lib/teams/api/message_activity.rb', line 37

def prepend_quote(message_id)
  @entities << QuotedReplyEntity.new("quotedReply" => { "messageId" => message_id })
  placeholder = %(<quoted messageId="#{message_id}"/>)
  @text = text.to_s.strip.empty? ? placeholder : "#{placeholder} #{text}"
  self
end

#to_hObject



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/teams/api/message_activity.rb', line 144

def to_h
  body = { "type" => "message" }
  body["id"] = id if id
  body["text"] = text if text
  body["textFormat"] = text_format if text_format
  body["summary"] = summary if summary
  body["inputHint"] = input_hint if input_hint
  body["attachments"] = attachments unless attachments.empty?
  body["entities"] = @entities.map { |entity| entity.respond_to?(:to_h) ? entity.to_h : entity } unless @entities.empty?
  body["channelData"] = channel_data unless channel_data.empty?
  body["recipient"] = recipient if recipient
  body
end

#with_id(id) ⇒ Object



61
62
63
64
# File 'lib/teams/api/message_activity.rb', line 61

def with_id(id)
  @id = id
  self
end

#with_recipient(account, is_targeted: nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/teams/api/message_activity.rb', line 66

def with_recipient(, is_targeted: nil)
  body = .respond_to?(:to_h) ? .to_h : 
  body = body.dup
  body["isTargeted"] = is_targeted unless is_targeted.nil?
  @recipient = body
  self
end

#with_text_format(text_format) ⇒ Object



56
57
58
59
# File 'lib/teams/api/message_activity.rb', line 56

def with_text_format(text_format)
  @text_format = normalize_text_format(text_format)
  self
end