Module: X::Resources::DirectMessageConversations

Included in:
DirectMessage
Defined in:
x-resources/lib/x/resources/direct_message_conversations.rb

Overview

Group conversations of direct messages: starting one, sending to one, and reading one, extended by DirectMessage

Internal to x-resources: the methods it gives DirectMessage, such as X::DirectMessage.create_group, are public API, but the module is only how they are shared, and which classes extend or include it can change within 1.x.

Instance Method Summary collapse

Instance Method Details

#create_group(users, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage

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

Examples:

Start a group conversation

X::DirectMessage.create_group([alice, bob], "Hello, both of you!", client: client)

Start a group conversation with an image

X::DirectMessage.create_group([alice, bob], client: client, 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

  • client (Object) —

    the client used to make the request

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

    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

  • params (Hash) —

    additional fields of the message, such as attachments

Returns:

  • (DirectMessage) —

    the sent message, holding only its identifiers, among them the new 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



40
41
42
43
# File 'x-resources/lib/x/resources/direct_message_conversations.rb', line 40

def create_group(users, text = nil, client:, media_ids: nil, **params)
  body = {conversation_type: "Group", participant_ids: users.map { |user| Utils.id_of(user, User) }, message: message(text, params, media_ids)}
  sent(client.post("dm_conversations", body, **Utils::JSON_CLASSES), "dm_conversations", client:)
end

#create_in(conversation, text = nil, client:, media_ids: nil, **params) ⇒ DirectMessage

Send a direct message to a conversation as the authenticated user

Examples:

Reply to the conversation of a message

X::DirectMessage.create_in(message, "Sounds good", client: client)

Reply with an image

X::DirectMessage.create_in(message, client: client, 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

  • client (Object) —

    the client used to make the request

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

    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

  • params (Hash) —

    additional request body fields, such as attachments

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



63
64
65
66
# File 'x-resources/lib/x/resources/direct_message_conversations.rb', line 63

def create_in(conversation, text = nil, client:, media_ids: nil, **params)
  path = "dm_conversations/#{conversation_id_of(conversation)}/messages"
  sent(client.post(path, message(text, params, media_ids), **Utils::JSON_CLASSES), path, client:)
end

#in(conversation, client:, **params) ⇒ Cursor

The direct message events of a conversation, one-to-one or group

Examples:

Print the conversation a message belongs to

X::DirectMessage.in(message, client: client).each { |event| puts event.text }

Parameters:

  • conversation (DirectMessage, String, Integer) —

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

  • client (Object) —

    the client used to make the requests

  • params (Hash) —

    query parameters merged over the default parameters, such as event_types

Returns:

  • (Cursor) —

    a cursor over the events

Raises:

  • (ArgumentError) —

    if the conversation identifier is not one



78
79
80
81
# File 'x-resources/lib/x/resources/direct_message_conversations.rb', line 78

def in(conversation, client:, **params)
  path = "dm_conversations/#{conversation_id_of(conversation)}/dm_events"
  Cursor.__send__(:build, DirectMessage, path, client:, params: {max_results: MAX_RESULTS}.merge(params))
end