Learn how to send and manage direct messages with Twikit
This guide covers everything you need to know about direct messaging, including sending messages, managing conversations, group DMs, and message reactions.
Attach images, videos, or GIFs to your direct messages:
# Upload media firstmedia_id = await client.upload_media('vacation_photo.jpg')# Send DM with mediamessage = await client.send_dm( user_id='123456789', text='Check out this photo!', media_id=media_id)
Unlike tweets, direct messages can only include one media attachment at a time.
# Get conversation historyhistory = await client.get_dm_history('123456789')last_message = history[0]# Reply to the last messagereply = await client.send_dm( user_id='123456789', text='Thanks for your message!', reply_to=last_message.id)
# Get DM history with a specific userhistory = await client.get_dm_history('123456789')for message in history: print(f'{message.time}: {message.text}') # Check for attachments if message.attachment: print(f' Has attachment: {message.attachment}')# Load more messages (older)more_messages = await history.next()
group = await client.get_group('123456789')print(f'Group name: {group.name}')print(f'Created at: {group.created_at}')print(f'Type: {group.type}')# Get group participantsfor participant in group.participants: print(f'Member: {participant.screen_name}')
import asyncioasync def auto_reply_bot(): # Store last checked message ID to avoid duplicates last_message_id = None while True: try: # Get your DM history me = await client.user() # You would need to get list of conversations first # This is a simplified example # Check each conversation for new messages # and send auto-reply await asyncio.sleep(60) # Check every minute except Exception as e: print(f'Error: {e}') await asyncio.sleep(60)# Run the botawait auto_reply_bot()
See the examples/dm_auto_reply.py file in the Twikit repository for a complete auto-reply bot implementation.
import asynciouser_ids = ['123456', '789012', '345678']message_text = 'Thanks for following!'for user_id in user_ids: try: await client.send_dm(user_id, message_text) print(f'Sent DM to {user_id}') # Add delay to respect rate limits await asyncio.sleep(2) except Exception as e: print(f'Failed to send to {user_id}: {e}')
Be careful with bulk DMs. Sending too many messages too quickly may result in rate limiting or account restrictions. Always add delays between messages and ensure you have permission to contact users.
import asyncioasync def check_new_messages(): known_messages = set() while True: try: # Check DMs from a specific user history = await client.get_dm_history('123456789') for message in history: if message.id not in known_messages: print(f'New message: {message.text}') # You could send a notification here known_messages.add(message.id) await asyncio.sleep(30) # Check every 30 seconds except Exception as e: print(f'Error: {e}') await asyncio.sleep(30)await check_new_messages()
history = await client.get_dm_history('123456789')for message in history: if message.attachment: attachment = message.attachment # Check attachment type if 'photo' in attachment: photo_url = attachment['photo']['url'] print(f'Photo: {photo_url}') elif 'video' in attachment: video_url = attachment['video']['url'] print(f'Video: {video_url}') # Download attachment # You can use client.http to download the file response = await client.http.get(photo_url) with open(f'attachment_{message.id}.jpg', 'wb') as f: f.write(response.content)