Module: Legion::Extensions::MicrosoftTeams::Runners::ChannelMessages

Extended by:
Definitions
Includes:
Helpers::Lex, Helpers::Client
Included in:
Client
Defined in:
lib/legion/extensions/microsoft_teams/runners/channel_messages.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Client

#bot_connection, #graph_connection, #oauth_connection, #user_path

Class Method Details

.trigger_wordsObject



13
14
15
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 13

def self.trigger_words
  %w[post posts feed thread reply]
end

Instance Method Details

#edit_channel_message(team_id:, channel_id:, message_id:, content:, content_type: 'text') ⇒ Object



179
180
181
182
183
184
185
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 179

def edit_channel_message(team_id:, channel_id:, message_id:, content:, content_type: 'text', **)
  payload = { body: { contentType: content_type, content: content } }
  response = graph_connection(**).patch(
    "teams/#{team_id}/channels/#{channel_id}/messages/#{message_id}", payload
  )
  { result: response.body }
end

#get_channel_message(team_id:, channel_id:, message_id:) ⇒ Object



75
76
77
78
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 75

def get_channel_message(team_id:, channel_id:, message_id:, **)
  response = graph_connection(**).get("teams/#{team_id}/channels/#{channel_id}/messages/#{message_id}")
  { result: response.body }
end

#list_channel_message_replies(team_id:, channel_id:, message_id:, top: 50, max_pages: 1) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 136

def list_channel_message_replies(team_id:, channel_id:, message_id:, top: 50, max_pages: 1, **)
  per_page = [top, 50].min
  params = { '$top' => per_page }
  conn = graph_connection(**)
  response = conn.get(
    "teams/#{team_id}/channels/#{channel_id}/messages/#{message_id}/replies", params
  )
  body = response.body

  return { result: body } if max_pages <= 1

  all_values = Array(body['value'] || body[:value])
  next_link = body['@odata.nextLink'] || body[:'@odata.nextLink']
  pages_fetched = 1

  while next_link && pages_fetched < max_pages
    response = conn.get(next_link)
    page_body = response.body
    items = page_body['value'] || page_body[:value]
    all_values.concat(Array(items)) if items
    next_link = page_body['@odata.nextLink'] || page_body[:'@odata.nextLink']
    pages_fetched += 1
  end

  result = { '@odata.context' => body['@odata.context'] || body[:'@odata.context'],
             'value'          => all_values }
  result['@odata.nextLink'] = next_link if next_link
  { result: result }
end

#list_channel_messages(team_id:, channel_id:, top: 50, max_pages: 1, expand: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 34

def list_channel_messages(team_id:, channel_id:, top: 50, max_pages: 1, expand: nil, **)
  per_page = [top, 50].min
  params = { '$top' => per_page }
  params['$expand'] = expand if expand
  conn = graph_connection(**)
  response = conn.get("teams/#{team_id}/channels/#{channel_id}/messages", params)
  body = response.body

  return { result: body } if max_pages <= 1

  all_values = Array(body['value'] || body[:value])
  next_link = body['@odata.nextLink'] || body[:'@odata.nextLink']
  pages_fetched = 1

  while next_link && pages_fetched < max_pages
    response = conn.get(next_link)
    page_body = response.body
    items = page_body['value'] || page_body[:value]
    all_values.concat(Array(items)) if items
    next_link = page_body['@odata.nextLink'] || page_body[:'@odata.nextLink']
    pages_fetched += 1
  end

  result = { '@odata.context' => body['@odata.context'] || body[:'@odata.context'],
             'value'          => all_values }
  result['@odata.nextLink'] = next_link if next_link
  { result: result }
end

#reply_to_channel_message(team_id:, channel_id:, message_id:, content:, content_type: 'text') ⇒ Object



112
113
114
115
116
117
118
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 112

def reply_to_channel_message(team_id:, channel_id:, message_id:, content:, content_type: 'text', **)
  payload = { body: { contentType: content_type, content: content } }
  response = graph_connection(**).post(
    "teams/#{team_id}/channels/#{channel_id}/messages/#{message_id}/replies", payload
  )
  { result: response.body }
end

#send_channel_message(team_id:, channel_id:, content:, content_type: 'text', attachments: []) ⇒ Object



92
93
94
95
96
97
# File 'lib/legion/extensions/microsoft_teams/runners/channel_messages.rb', line 92

def send_channel_message(team_id:, channel_id:, content:, content_type: 'text', attachments: [], **)
  payload = { body: { contentType: content_type, content: content } }
  payload[:attachments] = attachments unless attachments.empty?
  response = graph_connection(**).post("teams/#{team_id}/channels/#{channel_id}/messages", payload)
  { result: response.body }
end