Module: Twitter::REST::DirectMessages

Includes:
UploadUtils, Utils, Utils
Included in:
API
Defined in:
lib/twitter/rest/direct_messages.rb,
lib/twitter/rest/direct_messages/welcome_messages.rb

Defined Under Namespace

Modules: WelcomeMessages

Constant Summary

Constants included from Utils

Utils::DEFAULT_CURSOR

Instance Method Summary collapse

Methods included from Utils

flat_pmap, pmap

Instance Method Details

#create_direct_message(user_id, text, options = {}) ⇒ Twitter::DirectMessage Also known as: d, m, dm

Sends a new direct message to the specified user from the authenticating user

Parameters:

  • user_id (Integer)

    A Twitter user ID

  • text (String)

    The text of your direct message, up to 10,000 characters.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



167
168
169
170
# File 'lib/twitter/rest/direct_messages.rb', line 167

def create_direct_message(user_id, text, options = {})
  event = perform_request_with_object(:json_post, "/1.1/direct_messages/events/new.json", format_json_options(user_id, text, options), Twitter::DirectMessageEvent)
  event.direct_message
end

#create_direct_message_event(*args) ⇒ Twitter::DirectMessageEvent

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Create a new direct message event to the specified user from the authenticating user

Parameters:

  • user (Integer, String, Twitter::User)

    A Twitter user ID, screen name, URI, or object.

  • text (String)

    The text of your direct message, up to 10,000 characters.

  • options (Hash)

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



186
187
188
189
190
191
192
# File 'lib/twitter/rest/direct_messages.rb', line 186

def create_direct_message_event(*args)
  arguments = Twitter::Arguments.new(args)
  options = arguments.options.dup
  options[:event] = {type: "message_create", message_create: {target: {recipient_id: extract_id(arguments[0])}, message_data: {text: arguments[1]}}} if arguments.length >= 2
  response = Twitter::REST::Request.new(self, :json_post, "/1.1/direct_messages/events/new.json", options).perform
  Twitter::DirectMessageEvent.new(response[:event])
end

#create_direct_message_event_with_media(user, text, media, options = {}) ⇒ Twitter::DirectMessageEvent

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Create a new direct message event to the specified user from the authenticating user with media

Parameters:

  • user (Integer, String, Twitter::User)

    A Twitter user ID, screen name, URI, or object.

  • text (String)

    The text of your direct message, up to 10,000 characters.

  • media (File)

    A media file (PNG, JPEG, GIF or MP4).

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



207
208
209
210
211
212
213
# File 'lib/twitter/rest/direct_messages.rb', line 207

def create_direct_message_event_with_media(user, text, media, options = {})
  media_id = upload(media, media_category_prefix: "dm")[:media_id]
  options = options.dup
  options[:event] = {type: "message_create", message_create: {target: {recipient_id: extract_id(user)}, message_data: {text:, attachment: {type: "media", media: {id: media_id}}}}}
  response = Twitter::REST::Request.new(self, :json_post, "/1.1/direct_messages/events/new.json", options).perform
  Twitter::DirectMessageEvent.new(response[:event])
end

#destroy_direct_message(*ids) ⇒ nil

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Destroys direct messages

Parameters:

  • ids (Enumerable<Integer>)

    A collection of direct message IDs.

Returns:

  • (nil)

    Response body from Twitter is nil if successful

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



150
151
152
153
154
155
# File 'lib/twitter/rest/direct_messages.rb', line 150

def destroy_direct_message(*ids)
  pmap(ids) do |id|
    perform_requests(:delete, "/1.1/direct_messages/events/destroy.json", id:)
  end
  nil
end

#direct_message(id, options = {}) ⇒ Twitter::DirectMessage

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns a direct message

Parameters:

  • id (Integer)

    A direct message ID.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



85
86
87
# File 'lib/twitter/rest/direct_messages.rb', line 85

def direct_message(id, options = {})
  direct_message_event(id, options).direct_message
end

#direct_message_event(id, options = {}) ⇒ Twitter::DirectMessageEvent

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns a direct message event

Parameters:

  • id (Integer)

    A direct message ID.

  • options (Hash) (defaults to: {})

    A customizable set of options.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



99
100
101
102
103
# File 'lib/twitter/rest/direct_messages.rb', line 99

def direct_message_event(id, options = {})
  options = options.dup
  options[:id] = id
  perform_get_with_object("/1.1/direct_messages/events/show.json", options, Twitter::DirectMessageEvent)
end

#direct_messages(*ids) ⇒ Object #direct_messages(*ids, options) ⇒ Object

@see https://developer.twitter.com/en/docs/direct-messages/sending-and-receiving/api-reference/list-events @param options [Hash] A customizable set of options. @option options [Integer] :count Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 20 @option options [String] :cursor Specifies the cursor position of results to retrieve.

Overloads:



129
130
131
132
133
134
135
136
137
138
# File 'lib/twitter/rest/direct_messages.rb', line 129

def direct_messages(*args)
  arguments = Twitter::Arguments.new(args)
  if arguments.empty?
    direct_messages_received(arguments.options)
  else
    pmap(arguments) do |id|
      direct_message(id, arguments.options)
    end
  end
end

#direct_messages_events(options = {}) ⇒ Array<Twitter::DirectMessageEvent>

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns all Direct Message events for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20

  • :cursor (String)

    Specifies the cursor position of results to retrieve.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



26
27
28
29
# File 'lib/twitter/rest/direct_messages.rb', line 26

def direct_messages_events(options = {})
  limit = options.fetch(:count, 20)
  perform_get_with_cursor("/1.1/direct_messages/events/list.json", options.merge!(no_default_cursor: true, count: 50, limit:), :events, Twitter::DirectMessageEvent)
end

#direct_messages_list(options = {}) ⇒ Array<Twitter::DirectMessage>

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns all Direct Messages for the authenticated user (both sent and received) within the last 30 days. Sorted in reverse-chronological order.

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records to retrieve. Must be less than or equal to 50. Default is 20

  • :cursor (String)

    Specifies the cursor position of results to retrieve.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



41
42
43
# File 'lib/twitter/rest/direct_messages.rb', line 41

def direct_messages_list(options = {})
  direct_messages_events(options).collect(&:direct_message)
end

#direct_messages_received(options = {}) ⇒ Array<Twitter::DirectMessage>

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns Direct Messages received by the authenticated user within the last 30 days. Sorted in reverse-chronological order.

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 20

  • :cursor (String)

    Specifies the cursor position of results to retrieve.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



55
56
57
58
# File 'lib/twitter/rest/direct_messages.rb', line 55

def direct_messages_received(options = {})
  limit = options.fetch(:count, 20)
  direct_messages_list(options).select { |dm| dm.recipient_id == user_id }.first(limit)
end

#direct_messages_sent(options = {}) ⇒ Array<Twitter::DirectMessage>

Note:

This method requires an access token with RWD (read, write & direct message) permissions. Consult The Application Permission Model for more information.

Returns Direct Messages sent by the authenticated user within the last 30 days. Sorted in reverse-chronological order.

Parameters:

  • options (Hash) (defaults to: {})

    A customizable set of options.

Options Hash (options):

  • :count (Integer)

    Specifies the number of records (sent and received dms) to retrieve. Must be less than or equal to 50. Default is 20

  • :cursor (String)

    Specifies the cursor position of results to retrieve.

Returns:

Raises:

See Also:

Rate Limited?:

  • Yes

Authentication:

  • Requires user context



70
71
72
73
# File 'lib/twitter/rest/direct_messages.rb', line 70

def direct_messages_sent(options = {})
  limit = options.fetch(:count, 20)
  direct_messages_list(options).select { |dm| dm.sender_id == user_id }.first(limit)
end