Class: Teams::Api::MessageActivity

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

Constant Summary collapse

TEXT_FORMATS =
%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.



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

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.



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

def attachments
  @attachments
end

#channel_dataObject (readonly)

Returns the value of attribute channel_data.



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

def channel_data
  @channel_data
end

#entitiesObject (readonly)

Returns the value of attribute entities.



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

def entities
  @entities
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#input_hintObject (readonly)

Returns the value of attribute input_hint.



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

def input_hint
  @input_hint
end

#recipientObject (readonly)

Returns the value of attribute recipient.



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

def recipient
  @recipient
end

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

#textObject (readonly)

Returns the value of attribute text.



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

def text
  @text
end

#text_formatObject (readonly)

Returns the value of attribute text_format.



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

def text_format
  @text_format
end

Instance Method Details

#add_ai_generatedObject



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

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



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

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



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

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



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

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



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

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



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

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.



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

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.



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

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



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

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

#get_quoted_messagesObject



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

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

#prepend_quote(message_id) ⇒ Object



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

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



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

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



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

def with_id(id)
  @id = id
  self
end

#with_recipient(account, is_targeted: nil) ⇒ Object



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

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



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

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