createMediaMessage
Send direct messages on Twitter with media attachments, including images or videos, by specifying recipient ID, text, media ID, and alt text for accessibility.
Instructions
Send a direct message with media attachments
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| altText | No | Alternative text for the media (accessibility) | |
| mediaId | Yes | The media ID of the uploaded media to attach | |
| mediaType | No | MIME type of the media file | |
| recipientId | Yes | The ID of the user to send the message to | |
| text | Yes | The text content of the direct message |
Implementation Reference
- The core handler function implementing the logic for the 'createMediaMessage' tool. Sends a direct message with media attachment using Twitter API v1.export const handleCreateMediaMessage: TwitterHandler<CreateMediaMessageArgs> = async ( client: TwitterClient | null, { recipientId, text, mediaId, altText }: CreateMediaMessageArgs ): Promise<HandlerResponse> => { if (!client) { return createMissingTwitterApiKeyResponse('createMediaMessage'); } try { // Using v1 API for sending DMs with media const dmParams: any = { recipient_id: recipientId, text, media_id: mediaId }; const result = await client.v1.sendDm(dmParams); return createResponse(`Direct message with media sent successfully to user ${recipientId}. Response: ${JSON.stringify(result, null, 2)}`); } catch (error) { if (error instanceof Error) { if (error.message.includes('404')) { throw new Error(`Failed to send media message: User ${recipientId} not found or media ${mediaId} not found.`); } else if (error.message.includes('413')) { throw new Error(`Failed to send media message: Media file too large. Check Twitter's media size limits.`); } else if (error.message.includes('415')) { throw new Error(`Failed to send media message: Unsupported media type. Check Twitter's supported media formats.`); } throw new Error(formatTwitterError(error, 'sending media message')); } throw error; } };
- src/tools.ts:582-611 (schema)JSON Schema definition for the 'createMediaMessage' tool input parameters, used for MCP tool registration.createMediaMessage: { description: 'Send a direct message with media attachments', inputSchema: { type: 'object', properties: { recipientId: { type: 'string', description: 'The ID of the user to send the message to' }, text: { type: 'string', description: 'The text content of the direct message' }, mediaId: { type: 'string', description: 'The media ID of the uploaded media to attach' }, mediaType: { type: 'string', enum: ['image/jpeg', 'image/png', 'image/gif', 'video/mp4'], description: 'MIME type of the media file' }, altText: { type: 'string', description: 'Alternative text for the media (accessibility)' } }, required: ['recipientId', 'text', 'mediaId'] } },
- src/types/handlers.ts:106-112 (schema)TypeScript interface defining the arguments for the createMediaMessage handler.export interface CreateMediaMessageArgs { recipientId: string; text: string; mediaId: string; mediaType?: string; altText?: string; }
- src/index.ts:376-385 (registration)Registration in the main tool dispatch switch statement, routing 'createMediaMessage' calls to the handler.case 'createMediaMessage': { const { recipientId, text, mediaId, mediaType, altText } = request.params.arguments as { recipientId: string; text: string; mediaId: string; mediaType?: string; altText?: string; }; response = await handleCreateMediaMessage(client, { recipientId, text, mediaId, mediaType, altText }); break;