Class: RocketChat::Messages::Chat

Inherits:
Object
  • Object
show all
Includes:
RoomSupport
Defined in:
lib/rocket_chat/messages/chat.rb

Overview

Rocket.Chat Chat messages

Instance Method Summary collapse

Methods included from RoomSupport

#room_params, #room_query_params

Constructor Details

#initialize(session) ⇒ Chat

Returns a new instance of Chat.

Parameters:



14
15
16
# File 'lib/rocket_chat/messages/chat.rb', line 14

def initialize(session)
  @session = session
end

Instance Method Details

#delete(room_id: nil, name: nil, msg_id: nil, as_user: nil) ⇒ Boolean

chat.delete REST API

Parameters:

  • room_id (String) (defaults to: nil)

    Rocket.Chat room id

  • name (String) (defaults to: nil)

    Rocket.Chat room name (coming soon)

  • msg_id (String) (defaults to: nil)

    The message id to delete

  • as_user (Boolean) (defaults to: nil)

    Message deleted as user who sent (optional - default: false)

Returns:

  • (Boolean)

Raises:



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rocket_chat/messages/chat.rb', line 27

def delete(room_id: nil, name: nil, msg_id: nil, as_user: nil)
  session.request_json(
    '/api/v1/chat.delete',
    method: :post,
    body:
      room_params(room_id, name).tap do |h|
        h[:msgId] = msg_id
        h[:asUser] = as_user unless as_user.nil?
      end
  )['success']
end

#get_message(msg_id: nil) ⇒ RocketChat::Message

chat.getMessage REST API

Parameters:

  • msg_id (String) (defaults to: nil)

    The message id to return

Returns:

Raises:



45
46
47
48
49
50
51
# File 'lib/rocket_chat/messages/chat.rb', line 45

def get_message(msg_id: nil)
  response = session.request_json(
    '/api/v1/chat.getMessage',
    body: { msgId: msg_id }
  )
  RocketChat::Message.new response['message'] if response['success']
end

#post_message(room_id: nil, name: nil, channel: nil, **params) ⇒ RocketChat::Message

chat.postMessage REST API

Parameters:

  • room_id (String) (defaults to: nil)

    Rocket.Chat room id

  • name (String) (defaults to: nil)

    Rocket.Chat room name (coming soon)

  • channel (String) (defaults to: nil)

    Rocket.Chat channel name

  • params (Hash)

    Optional params (text, alias, emoji, tmid, avatar & attachments)

Returns:

Raises:



62
63
64
65
66
67
68
69
70
71
# File 'lib/rocket_chat/messages/chat.rb', line 62

def post_message(room_id: nil, name: nil, channel: nil, **params)
  response = session.request_json(
    '/api/v1/chat.postMessage',
    method: :post,
    body: room_params(room_id, name)
      .merge(channel: channel)
      .merge(Util.slice_hash(params, :text, :alias, :tmid, :emoji, :avatar, :attachments))
  )
  RocketChat::Message.new response['message'] if response['success']
end

#update(room_id: nil, name: nil, msg_id: nil, text: nil) ⇒ RocketChat::Message

chat.update REST API

Parameters:

  • room_id (String) (defaults to: nil)

    Rocket.Chat room id

  • name (String) (defaults to: nil)

    Rocket.Chat room name (coming soon)

  • msg_id (String) (defaults to: nil)

    The message id to update

  • text (String) (defaults to: nil)

    Updated text for the message

Returns:

Raises:



82
83
84
85
86
87
88
89
# File 'lib/rocket_chat/messages/chat.rb', line 82

def update(room_id: nil, name: nil, msg_id: nil, text: nil)
  response = session.request_json(
    '/api/v1/chat.update',
    method: :post,
    body: room_params(room_id, name).merge(msgId: msg_id, text: text)
  )
  RocketChat::Message.new response['message'] if response['success']
end