@@ -5,6 +5,9 @@ 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 { Media } from "@ai16z/eliza" ;
9
+ import fs from "fs" ;
10
+ import path from "path" ;
8
11
9
12
export const wait = ( minTime : number = 1000 , maxTime : number = 3000 ) => {
10
13
const waitTime =
@@ -162,6 +165,16 @@ export async function buildConversationThread(
162
165
return thread ;
163
166
}
164
167
168
+ export function getMediaType ( attachment : Media ) {
169
+ if ( attachment . contentType ?. startsWith ( "video" ) ) {
170
+ return "video" ;
171
+ } else if ( attachment . contentType ?. startsWith ( "image" ) ) {
172
+ return "image" ;
173
+ } else {
174
+ throw new Error ( `Unsupported media type` ) ;
175
+ }
176
+ }
177
+
165
178
export async function sendTweet (
166
179
client : ClientBase ,
167
180
content : Content ,
@@ -178,11 +191,45 @@ export async function sendTweet(
178
191
let previousTweetId = inReplyTo ;
179
192
180
193
for ( const chunk of tweetChunks ) {
194
+ let mediaData : { data : Buffer ; mediaType : string } [ ] | undefined ;
195
+
196
+ if ( content . attachments && content . attachments . length > 0 ) {
197
+ mediaData = await Promise . all (
198
+ content . attachments . map ( async ( attachment : Media ) => {
199
+ if ( / ^ ( h t t p | h t t p s ) : \/ \/ / . test ( attachment . url ) ) {
200
+ // Handle HTTP URLs
201
+ const response = await fetch ( attachment . url ) ;
202
+ if ( ! response . ok ) {
203
+ throw new Error (
204
+ `Failed to fetch file: ${ attachment . url } `
205
+ ) ;
206
+ }
207
+ const mediaBuffer = Buffer . from (
208
+ await response . arrayBuffer ( )
209
+ ) ;
210
+ const mediaType = getMediaType ( attachment ) ;
211
+ return { data : mediaBuffer , mediaType } ;
212
+ } else if ( fs . existsSync ( attachment . url ) ) {
213
+ // Handle local file paths
214
+ const mediaBuffer = await fs . promises . readFile (
215
+ path . resolve ( attachment . url )
216
+ ) ;
217
+ const mediaType = getMediaType ( attachment ) ;
218
+ return { data : mediaBuffer , mediaType } ;
219
+ } else {
220
+ throw new Error (
221
+ `File not found: ${ attachment . url } . Make sure the path is correct.`
222
+ ) ;
223
+ }
224
+ } )
225
+ ) ;
226
+ }
181
227
const result = await client . requestQueue . add (
182
228
async ( ) =>
183
229
await client . twitterClient . sendTweet (
184
230
chunk . trim ( ) ,
185
- previousTweetId
231
+ previousTweetId ,
232
+ mediaData
186
233
)
187
234
) ;
188
235
const body = await result . json ( ) ;
0 commit comments