@@ -5,6 +5,7 @@ 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" ;
8
9
import fs from "fs" ;
9
10
import path from "path" ;
10
11
@@ -164,27 +165,15 @@ export async function buildConversationThread(
164
165
return thread ;
165
166
}
166
167
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 } ` ) ;
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` ) ;
178
175
}
179
176
}
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
177
189
178
export async function sendTweet (
190
179
client : ClientBase ,
@@ -206,22 +195,26 @@ export async function sendTweet(
206
195
207
196
if ( content . attachments && content . attachments . length > 0 ) {
208
197
mediaData = await Promise . all (
209
- content . attachments . map ( async ( attachment :Attachment ) => {
198
+ content . attachments . map ( async ( attachment : Media ) => {
210
199
if ( / ^ ( h t t p | h t t p s ) : \/ \/ / . test ( attachment . url ) ) {
211
200
// Handle HTTP URLs
212
201
const response = await fetch ( attachment . url ) ;
213
202
if ( ! response . ok ) {
214
- throw new Error ( `Failed to fetch file: ${ attachment . url } ` ) ;
203
+ throw new Error (
204
+ `Failed to fetch file: ${ attachment . url } `
205
+ ) ;
215
206
}
216
- const mediaBuffer = Buffer . from ( await response . arrayBuffer ( ) ) ;
217
- const mediaType = getMediaType ( attachment . url ) ;
207
+ const mediaBuffer = Buffer . from (
208
+ await response . arrayBuffer ( )
209
+ ) ;
210
+ const mediaType = getMediaType ( attachment ) ;
218
211
return { data : mediaBuffer , mediaType } ;
219
212
} else if ( fs . existsSync ( attachment . url ) ) {
220
213
// Handle local file paths
221
214
const mediaBuffer = await fs . promises . readFile (
222
215
path . resolve ( attachment . url )
223
216
) ;
224
- const mediaType = getMediaType ( attachment . url ) ;
217
+ const mediaType = getMediaType ( attachment ) ;
225
218
return { data : mediaBuffer , mediaType } ;
226
219
} else {
227
220
throw new Error (
0 commit comments