@@ -5,6 +5,8 @@ import { stringToUuid } from "@ai16z/eliza";
5
5
import { ClientBase } from "./base" ;
6
6
import { elizaLogger } from "@ai16z/eliza" ;
7
7
import { DEFAULT_MAX_TWEET_LENGTH } from "./environment" ;
8
+ import fs from "fs" ;
9
+ import path from "path" ;
8
10
9
11
export const wait = ( minTime : number = 1000 , maxTime : number = 3000 ) => {
10
12
const waitTime =
@@ -162,6 +164,28 @@ export async function buildConversationThread(
162
164
return thread ;
163
165
}
164
166
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
+
165
189
export async function sendTweet (
166
190
client : ClientBase ,
167
191
content : Content ,
@@ -178,11 +202,31 @@ export async function sendTweet(
178
202
let previousTweetId = inReplyTo ;
179
203
180
204
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
+ }
181
224
const result = await client . requestQueue . add (
182
225
async ( ) =>
183
226
await client . twitterClient . sendTweet (
184
227
chunk . trim ( ) ,
185
- previousTweetId
228
+ previousTweetId ,
229
+ mediaData
186
230
)
187
231
) ;
188
232
const body = await result . json ( ) ;
0 commit comments