Class: ChatSDK::Mattermost::ApiClient

Inherits:
ApiClient::Base
  • Object
show all
Defined in:
lib/chat_sdk/mattermost/api_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, bot_token:) ⇒ ApiClient

Returns a new instance of ApiClient.



6
7
8
9
# File 'lib/chat_sdk/mattermost/api_client.rb', line 6

def initialize(base_url:, bot_token:)
  @base_url_value = base_url.chomp("/")
  @bot_token = bot_token
end

Instance Method Details

#add_reaction(user_id:, post_id:, emoji_name:) ⇒ Object

Reactions



39
40
41
42
# File 'lib/chat_sdk/mattermost/api_client.rb', line 39

def add_reaction(user_id:, post_id:, emoji_name:)
  body = {"user_id" => user_id, "post_id" => post_id, "emoji_name" => emoji_name}
  request(:post, "/api/v4/reactions", body)
end

#create_direct_channel(user_ids) ⇒ Object

Channels



49
50
51
# File 'lib/chat_sdk/mattermost/api_client.rb', line 49

def create_direct_channel(user_ids)
  request(:post, "/api/v4/channels/direct", user_ids)
end

#create_ephemeral_post(user_id:, channel_id:, message:) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/chat_sdk/mattermost/api_client.rb', line 30

def create_ephemeral_post(user_id:, channel_id:, message:)
  body = {
    "user_id" => user_id,
    "post" => {"channel_id" => channel_id, "message" => message}
  }
  request(:post, "/api/v4/posts/ephemeral", body)
end

#create_post(channel_id:, message:, root_id: nil, props: nil, file_ids: nil) ⇒ Object

Posts



12
13
14
15
16
17
18
# File 'lib/chat_sdk/mattermost/api_client.rb', line 12

def create_post(channel_id:, message:, root_id: nil, props: nil, file_ids: nil)
  body = {"channel_id" => channel_id, "message" => message}
  body["root_id"] = root_id if root_id
  body["props"] = props if props
  body["file_ids"] = file_ids if file_ids
  request(:post, "/api/v4/posts", body)
end

#delete_post(post_id:) ⇒ Object



26
27
28
# File 'lib/chat_sdk/mattermost/api_client.rb', line 26

def delete_post(post_id:)
  request(:delete, "/api/v4/posts/#{post_id}")
end

#get_channel_posts(channel_id:, page: 0, per_page: 50) ⇒ Object



53
54
55
# File 'lib/chat_sdk/mattermost/api_client.rb', line 53

def get_channel_posts(channel_id:, page: 0, per_page: 50)
  request(:get, "/api/v4/channels/#{channel_id}/posts?page=#{page}&per_page=#{per_page}")
end

#get_post_thread(post_id:) ⇒ Object

Threads



58
59
60
# File 'lib/chat_sdk/mattermost/api_client.rb', line 58

def get_post_thread(post_id:)
  request(:get, "/api/v4/posts/#{post_id}/thread")
end

#remove_reaction(user_id:, post_id:, emoji_name:) ⇒ Object



44
45
46
# File 'lib/chat_sdk/mattermost/api_client.rb', line 44

def remove_reaction(user_id:, post_id:, emoji_name:)
  request(:delete, "/api/v4/reactions/#{user_id}/#{post_id}/#{emoji_name}")
end

#send_typing(channel_id:) ⇒ Object

Typing indicator



74
75
76
# File 'lib/chat_sdk/mattermost/api_client.rb', line 74

def send_typing(channel_id:)
  request(:post, "/api/v4/users/me/typing", {"channel_id" => channel_id})
end

#update_post(post_id:, message:, props: nil) ⇒ Object



20
21
22
23
24
# File 'lib/chat_sdk/mattermost/api_client.rb', line 20

def update_post(post_id:, message:, props: nil)
  body = {"id" => post_id, "message" => message}
  body["props"] = props if props
  request(:put, "/api/v4/posts/#{post_id}", body)
end

#upload_file(channel_id:, io:, filename:) ⇒ Object

Files



63
64
65
66
67
68
69
70
71
# File 'lib/chat_sdk/mattermost/api_client.rb', line 63

def upload_file(channel_id:, io:, filename:)
  payload = {
    files: Faraday::Multipart::FilePart.new(io, "application/octet-stream", filename),
    channel_id: channel_id
  }

  response = upload_connection.post("/api/v4/files", payload)
  handle_response(response)
end