Module: Legion::Extensions::MicrosoftTeams::Runners::Messages

Extended by:
Definitions
Includes:
Helpers::Lex, Helpers::Client
Included in:
Client
Defined in:
lib/legion/extensions/microsoft_teams/runners/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/messages.rb', line 13

def self.trigger_words
  %w[message messages reply replies thread send]
end

Instance Method Details

#get_chat_message(chat_id:, message_id:) ⇒ Object



78
79
80
81
82
# File 'lib/legion/extensions/microsoft_teams/runners/messages.rb', line 78

def get_chat_message(chat_id:, message_id:, **)
  log.debug "get_chat_message(chat_id: #{chat_id}, message_id: #{message_id})"
  response = graph_connection(**).get("chats/#{chat_id}/messages/#{message_id}")
  { result: response.body }
end

#list_chat_messages(chat_id:, top: 50, max_pages: 1, orderby: nil, filter: nil) ⇒ Object



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
62
63
64
65
# File 'lib/legion/extensions/microsoft_teams/runners/messages.rb', line 36

def list_chat_messages(chat_id:, top: 50, max_pages: 1, orderby: nil, filter: nil, **)
  log.debug "list_chat_messages(chat_id: #{chat_id}, top: #{top}, max_pages: #{max_pages})"
  per_page = [top, 50].min
  params = { '$top' => per_page }
  params['$orderby'] = orderby if orderby
  params['$filter'] = filter if filter
  conn = graph_connection(**)
  response = conn.get("chats/#{chat_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

#list_message_replies(chat_id:, message_id:, top: 50, max_pages: 1) ⇒ Object



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
165
# File 'lib/legion/extensions/microsoft_teams/runners/messages.rb', line 138

def list_message_replies(chat_id:, message_id:, top: 50, max_pages: 1, **)
  log.debug "list_message_replies(chat_id: #{chat_id}, message_id: #{message_id}, top: #{top}, max_pages: #{max_pages})"
  per_page = [top, 50].min
  params = { '$top' => per_page }
  conn = graph_connection(**)
  response = conn.get("chats/#{chat_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

#reply_to_chat_message(chat_id:, message_id:, content:, content_type: 'text') ⇒ Object



116
117
118
119
120
121
# File 'lib/legion/extensions/microsoft_teams/runners/messages.rb', line 116

def reply_to_chat_message(chat_id:, message_id:, content:, content_type: 'text', **)
  log.debug "reply_to_chat_message(chat_id: #{chat_id}, message_id: #{message_id}, content: #{content}, content_type: #{content_type})"
  payload = { body: { contentType: content_type, content: content } }
  response = graph_connection(**).post("chats/#{chat_id}/messages/#{message_id}/replies", payload)
  { result: response.body }
end

#send_chat_message(chat_id:, content:, content_type: 'text', attachments: []) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/legion/extensions/microsoft_teams/runners/messages.rb', line 96

def send_chat_message(chat_id:, content:, content_type: 'text', attachments: [], **)
  log.debug "send_chat_message(chat_id: #{chat_id}, content: #{content}, content_type: #{content_type})"
  payload = { body: { contentType: content_type, content: content } }
  payload[:attachments] = attachments unless attachments.empty?
  response = graph_connection(**).post("chats/#{chat_id}/messages", payload)
  { result: response.body }
end