Skip to content

Commit 0c13bfe

Browse files
committed
enable image tweet on twitter
1 parent 9192179 commit 0c13bfe

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

packages/client-twitter/src/interactions.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ export class TwitterInteractionClient {
448448
await this.runtime.processActions(
449449
message,
450450
responseMessages,
451-
state
451+
state,
452+
callback
452453
);
453454

454455
const responseInfo = `Context:\n\n${context}\n\nSelected Post: ${tweet.id} - ${tweet.username}: ${tweet.text}\nAgent's Output:\n${response.text}`;

packages/client-twitter/src/utils.ts

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { stringToUuid } from "@ai16z/eliza";
55
import { ClientBase } from "./base";
66
import { elizaLogger } from "@ai16z/eliza";
77
import { DEFAULT_MAX_TWEET_LENGTH } from "./environment";
8+
import fs from "fs";
9+
import path from "path";
810

911
export const wait = (minTime: number = 1000, maxTime: number = 3000) => {
1012
const waitTime =
@@ -162,6 +164,28 @@ export async function buildConversationThread(
162164
return thread;
163165
}
164166

167+
export function getMediaType(filePath: string) {
168+
const extension = filePath.split('.').pop().toLowerCase();
169+
switch (extension) {
170+
case 'png':
171+
case 'jpg':
172+
case 'jpeg':
173+
return 'image';
174+
case 'mp4':
175+
return 'video';
176+
default:
177+
throw new Error(`Unsupported media type: ${extension}`);
178+
}
179+
}
180+
type Attachment = {
181+
id: string;
182+
url: string; // Path to the file
183+
title?: string;
184+
source?: string;
185+
description?: string;
186+
text?: string;
187+
};
188+
165189
export async function sendTweet(
166190
client: ClientBase,
167191
content: Content,
@@ -178,11 +202,31 @@ export async function sendTweet(
178202
let previousTweetId = inReplyTo;
179203

180204
for (const chunk of tweetChunks) {
205+
let mediaData: { data: Buffer; mediaType: string }[] | undefined;
206+
207+
if (content.attachments && content.attachments.length > 0) {
208+
mediaData = await Promise.all(
209+
content.attachments.map(async (attachment:Attachment) => {
210+
if (fs.existsSync(attachment.url)) {
211+
const mediaBuffer = await fs.promises.readFile(
212+
path.resolve(attachment.url)
213+
);
214+
const mediaType = getMediaType(attachment.url);
215+
return { data: mediaBuffer, mediaType };
216+
} else {
217+
throw new Error(
218+
`File not found: ${attachment.url}. Make sure the path is correct.`
219+
);
220+
}
221+
})
222+
);
223+
}
181224
const result = await client.requestQueue.add(
182225
async () =>
183226
await client.twitterClient.sendTweet(
184227
chunk.trim(),
185-
previousTweetId
228+
previousTweetId,
229+
mediaData
186230
)
187231
);
188232
const body = await result.json();

0 commit comments

Comments
 (0)