Module: X::Objects::Actions::DirectMessages

Included in:
X::Objects::API
Defined in:
x-objects/lib/x/objects/actions/direct_messages.rb

Overview

Send and delete direct messages as the authenticated user

Internal to x-objects: X::Objects::API includes it, and its methods are public API of the client that includes API, but the module is only how they are grouped, and some of them need the methods of another, so include API rather than this module alone.

Instance Method Summary collapse

Instance Method Details

#create_direct_message(user, text = nil, **params) ⇒ DirectMessage Also known as: create_dm

Send a direct message to a user as the authenticated user

Examples:

Send a direct message

client.create_direct_message(user, "Hello!")

Send an image without text

client.create_direct_message(user, media_ids: media)

Parameters:

  • user (User, String, Integer) —

    the recipient or their identifier

  • text (String, nil) (defaults to: nil) —

    the text of the message, or nil for a message of attachments alone

  • params (Hash) —

    additional request body fields, such as media_ids or attachments

Options Hash (**params):

  • :media_ids (Array<String, Integer, #fetch, Media>, String, Integer, #fetch, Media) —

    the identifiers or media keys of uploaded media to attach, what the uploads returned, or media, such as that of a post, one or many

Returns:

  • (DirectMessage) —

    the sent message, holding only its identifiers

Raises:

  • (ArgumentError) —

    if the message has neither text nor any other field, or has both media_ids and attachments

  • (MissingResource) —

    if the API answers without the message



33
34
35
# File 'x-objects/lib/x/objects/actions/direct_messages.rb', line 33

def create_direct_message(user, text = nil, **params) # steep:ignore DifferentMethodParameterKind
  DirectMessage.create(user, text, client: self, **params)
end

#create_direct_message_in(conversation, text = nil, **params) ⇒ DirectMessage Also known as: create_dm_in

Send a direct message to a conversation as the authenticated user

The conversation can be one-to-one or a group.

Examples:

Reply to the conversation of a message

client.create_direct_message_in(message, "Sounds good")

Reply with an image

client.create_direct_message_in(message, media_ids: media)

Parameters:

  • conversation (DirectMessage, String, Integer) —

    a message of the conversation, or the conversation's identifier

  • text (String, nil) (defaults to: nil) —

    the text of the message, or nil for a message of attachments alone

  • params (Hash) —

    additional request body fields, such as media_ids or attachments

Options Hash (**params):

  • :media_ids (Array<String, Integer, #fetch, Media>, String, Integer, #fetch, Media) —

    the identifiers or media keys of uploaded media to attach, what the uploads returned, or media, such as that of a post, one or many

Returns:

  • (DirectMessage) —

    the sent message, holding only its identifiers

Raises:

  • (ArgumentError) —

    if the conversation identifier is not one, the message has neither text nor any other field, or it has both media_ids and attachments

  • (MissingResource) —

    if the API answers without the message



77
78
79
# File 'x-objects/lib/x/objects/actions/direct_messages.rb', line 77

def create_direct_message_in(conversation, text = nil, **params) # steep:ignore DifferentMethodParameterKind
  DirectMessage.create_in(conversation, text, client: self, **params)
end

#create_group_direct_message(users, text = nil, **params) ⇒ DirectMessage Also known as: create_group_dm

Start a group conversation, sending its first message as the authenticated user

Examples:

Start a group conversation

client.create_group_direct_message([alice, bob], "Hello, both of you!")

Start a group conversation with an image

client.create_group_direct_message([alice, bob], media_ids: media)

Parameters:

  • users (Array<User, String, Integer>) —

    the other participants or their identifiers

  • text (String, nil) (defaults to: nil) —

    the text of the first message, or nil for a message of attachments alone

  • params (Hash) —

    additional fields of the message, such as media_ids or attachments

Options Hash (**params):

  • :media_ids (Array<String, Integer, #fetch, Media>, String, Integer, #fetch, Media) —

    the identifiers or media keys of uploaded media to attach, what the uploads returned, or media, such as that of a post, one or many

Returns:

  • (DirectMessage) —

    the sent message, holding only its identifiers, among them the conversation's

Raises:

  • (ArgumentError) —

    if the message has neither text nor any other field, or has both media_ids and attachments

  • (MissingResource) —

    if the API answers without the message



54
55
56
# File 'x-objects/lib/x/objects/actions/direct_messages.rb', line 54

def create_group_direct_message(users, text = nil, **params) # steep:ignore DifferentMethodParameterKind
  DirectMessage.create_group(users, text, client: self, **params)
end

#delete_direct_message(message) ⇒ Boolean Also known as: delete_dm

Delete a direct message event as the authenticated user

Examples:

Delete a direct message

client.delete_direct_message("1234567890")

Parameters:

  • message (DirectMessage, String, Integer) —

    the event or its identifier

Returns:

  • (Boolean) —

    true if the event was deleted



88
89
90
# File 'x-objects/lib/x/objects/actions/direct_messages.rb', line 88

def delete_direct_message(message)
  DirectMessage.delete(message, client: self)
end