Skip to main content

Overview

The Message class represents a direct message (DM) in Twitter/X and provides methods for replying, reacting, and deleting messages.

Message Class

Attributes

id
str
The ID of the message.
time
str
The timestamp of the message.
text
str
The text content of the message.
attachment
dict | None
Attachment information, if any media is attached to the message.
sender_id
str
The ID of the user who sent the message.
recipient_id
str
The ID of the user who received the message.

Methods

reply()

Replies to the message.
text
str
required
The text content of the direct message.
media_id
str | None
default:"None"
The media ID associated with any media content to be included in the message. Media ID can be received by using the upload_media method.
Returns: Message - Message object containing information about the message sent.
# Reply to a message
await message.reply('Thanks for your message!')

# Reply with media
media_id = await client.upload_media('image.png')
await message.reply('Here is the image you requested', media_id=media_id)
See Also: Client.send_dm

add_reaction()

Adds a reaction (emoji) to the message.
emoji
str
required
The emoji to be added as a reaction.
Returns: httpx.Response
# Add a heart reaction
await message.add_reaction('❤️')

# Add a thumbs up reaction
await message.add_reaction('👍')

remove_reaction()

Removes a reaction (emoji) from the message.
emoji
str
required
The emoji to be removed.
Returns: httpx.Response
# Remove a heart reaction
await message.remove_reaction('❤️')

delete()

Deletes the message. Returns: httpx.Response
await message.delete()
See Also: Client.delete_dm

Examples

Send and Reply to Direct Messages

# Get a user and send them a message
user = await client.get_user_by_screen_name('friend')
message = await user.send_dm('Hey! How are you?')

print(f"Message sent: {message.text}")
print(f"Message ID: {message.id}")

Get DM History and Reply

# Get DM conversation with a user
user = await client.get_user_by_screen_name('friend')
messages = await user.get_dm_history()

# Reply to the most recent message
if messages:
    latest_message = messages[0]
    await latest_message.reply('Got your message!')

React to Messages

# Get messages and add reactions
user = await client.get_user_by_screen_name('friend')
messages = await user.get_dm_history()

for message in messages:
    if 'great' in message.text.lower():
        # React with a heart emoji
        await message.add_reaction('❤️')

Send Message with Media

# Upload media and send in DM
user = await client.get_user_by_screen_name('friend')
media_id = await client.upload_media('vacation_photo.jpg')

message = await user.send_dm(
    'Check out my vacation photo!',
    media_id=media_id
)

print(f"Message with media sent: {message.id}")

Delete Messages

# Get your DM history and delete specific messages
user = await client.get_user_by_screen_name('someone')
messages = await user.get_dm_history()

# Delete messages containing specific text
for message in messages:
    if message.sender_id == await client.user_id():
        if 'delete this' in message.text.lower():
            await message.delete()
            print(f"Deleted message: {message.id}")

Conversation Thread

# Get full conversation and process
user = await client.get_user_by_screen_name('colleague')
messages = await user.get_dm_history()

print(f"Conversation with @{user.screen_name}:")
print("-" * 50)

for message in reversed(messages):  # Show oldest first
    sender = "Me" if message.sender_id == await client.user_id() else user.name
    print(f"{sender}: {message.text}")
    print(f"  ({message.time})")
    print()
Direct messages can only be sent to users who follow you or have their DMs open to everyone. Check the user.can_dm attribute before attempting to send a message.

Build docs developers (and LLMs) love