1
1
import { Message } from "@telegraf/types" ;
2
2
import { Context , Telegraf } from "telegraf" ;
3
3
4
- import { composeContext } from "@ai16z/eliza" ;
4
+ import { composeContext , elizaLogger , ServiceType } from "@ai16z/eliza" ;
5
5
import { embeddingZeroVector } from "@ai16z/eliza" ;
6
6
import {
7
7
Content ,
@@ -17,7 +17,6 @@ import { stringToUuid } from "@ai16z/eliza";
17
17
18
18
import { generateMessageResponse , generateShouldRespond } from "@ai16z/eliza" ;
19
19
import { messageCompletionFooter , shouldRespondFooter } from "@ai16z/eliza" ;
20
- import { ImageDescriptionService } from "@ai16z/plugin-node" ;
21
20
22
21
const MAX_MESSAGE_LENGTH = 4096 ; // Telegram's max message length
23
22
@@ -137,57 +136,49 @@ Thread of Tweets You Are Replying To:
137
136
export class MessageManager {
138
137
public bot : Telegraf < Context > ;
139
138
private runtime : IAgentRuntime ;
140
- private imageService : IImageDescriptionService ;
141
139
142
140
constructor ( bot : Telegraf < Context > , runtime : IAgentRuntime ) {
143
141
this . bot = bot ;
144
142
this . runtime = runtime ;
145
- this . imageService = ImageDescriptionService . getInstance ( ) ;
146
143
}
147
144
148
145
// Process image messages and generate descriptions
149
146
private async processImage (
150
147
message : Message
151
148
) : Promise < { description : string } | null > {
152
- // console.log(
153
- // "🖼️ Processing image message:",
154
- // JSON.stringify(message, null, 2)
155
- // );
156
-
157
149
try {
158
150
let imageUrl : string | null = null ;
159
151
160
- // Handle photo messages
161
152
if ( "photo" in message && message . photo ?. length > 0 ) {
162
153
const photo = message . photo [ message . photo . length - 1 ] ;
163
154
const fileLink = await this . bot . telegram . getFileLink (
164
155
photo . file_id
165
156
) ;
166
157
imageUrl = fileLink . toString ( ) ;
167
- }
168
- // Handle image documents
169
- else if (
158
+ } else if (
170
159
"document" in message &&
171
160
message . document ?. mime_type ?. startsWith ( "image/" )
172
161
) {
173
- const doc = message . document ;
174
162
const fileLink = await this . bot . telegram . getFileLink (
175
- doc . file_id
163
+ message . document . file_id
176
164
) ;
177
165
imageUrl = fileLink . toString ( ) ;
178
166
}
179
167
180
168
if ( imageUrl ) {
169
+ const imageDescriptionService =
170
+ this . runtime . getService < IImageDescriptionService > (
171
+ ServiceType . IMAGE_DESCRIPTION
172
+ ) ;
181
173
const { title, description } =
182
- await this . imageService . describeImage ( imageUrl ) ;
183
- const fullDescription = `[Image: ${ title } \n${ description } ]` ;
184
- return { description : fullDescription } ;
174
+ await imageDescriptionService . describeImage ( imageUrl ) ;
175
+ return { description : `[Image: ${ title } \n${ description } ]` } ;
185
176
}
186
177
} catch ( error ) {
187
178
console . error ( "❌ Error processing image:" , error ) ;
188
179
}
189
180
190
- return null ; // No image found
181
+ return null ;
191
182
}
192
183
193
184
// Decide if the bot should respond to the message
@@ -196,7 +187,6 @@ export class MessageManager {
196
187
state : State
197
188
) : Promise < boolean > {
198
189
// Respond if bot is mentioned
199
-
200
190
if (
201
191
"text" in message &&
202
192
message . text ?. includes ( `@${ this . bot . botInfo ?. username } ` )
@@ -209,7 +199,7 @@ export class MessageManager {
209
199
return true ;
210
200
}
211
201
212
- // Respond to images in group chats
202
+ // Don't respond to images in group chats
213
203
if (
214
204
"photo" in message ||
215
205
( "document" in message &&
@@ -238,7 +228,7 @@ export class MessageManager {
238
228
return response === "RESPOND" ;
239
229
}
240
230
241
- return false ; // No criteria met
231
+ return false ;
242
232
}
243
233
244
234
// Send long messages in chunks
@@ -291,7 +281,7 @@ export class MessageManager {
291
281
// Generate a response using AI
292
282
private async _generateResponse (
293
283
message : Memory ,
294
- state : State ,
284
+ _state : State ,
295
285
context : string
296
286
) : Promise < Content > {
297
287
const { userId, roomId } = message ;
@@ -306,9 +296,10 @@ export class MessageManager {
306
296
console . error ( "❌ No response from generateMessageResponse" ) ;
307
297
return null ;
308
298
}
299
+
309
300
await this . runtime . databaseAdapter . log ( {
310
301
body : { message, context, response } ,
311
- userId : userId ,
302
+ userId,
312
303
roomId,
313
304
type : "response" ,
314
305
} ) ;
@@ -342,14 +333,23 @@ export class MessageManager {
342
333
try {
343
334
// Convert IDs to UUIDs
344
335
const userId = stringToUuid ( ctx . from . id . toString ( ) ) as UUID ;
336
+
337
+ // Get user name
345
338
const userName =
346
339
ctx . from . username || ctx . from . first_name || "Unknown User" ;
340
+
341
+ // Get chat ID
347
342
const chatId = stringToUuid (
348
343
ctx . chat ?. id . toString ( ) + "-" + this . runtime . agentId
349
344
) as UUID ;
345
+
346
+ // Get agent ID
350
347
const agentId = this . runtime . agentId ;
348
+
349
+ // Get room ID
351
350
const roomId = chatId ;
352
351
352
+ // Ensure connection
353
353
await this . runtime . ensureConnection (
354
354
userId ,
355
355
roomId ,
@@ -358,6 +358,7 @@ export class MessageManager {
358
358
"telegram"
359
359
) ;
360
360
361
+ // Get message ID
361
362
const messageId = stringToUuid (
362
363
message . message_id . toString ( ) + "-" + this . runtime . agentId
363
364
) as UUID ;
@@ -382,17 +383,18 @@ export class MessageManager {
382
383
return ; // Skip if no content
383
384
}
384
385
386
+ // Create content
385
387
const content : Content = {
386
388
text : fullText ,
387
389
source : "telegram" ,
388
- // inReplyTo:
389
- // "reply_to_message" in message && message.reply_to_message
390
- // ? stringToUuid(
391
- // message.reply_to_message.message_id.toString() +
392
- // "-" +
393
- // this.runtime.agentId
394
- // )
395
- // : undefined,
390
+ inReplyTo :
391
+ "reply_to_message" in message && message . reply_to_message
392
+ ? stringToUuid (
393
+ message . reply_to_message . message_id . toString ( ) +
394
+ "-" +
395
+ this . runtime . agentId
396
+ )
397
+ : undefined ,
396
398
} ;
397
399
398
400
// Create memory for the message
@@ -406,6 +408,7 @@ export class MessageManager {
406
408
embedding : embeddingZeroVector ,
407
409
} ;
408
410
411
+ // Create memory
409
412
await this . runtime . messageManager . createMemory ( memory ) ;
410
413
411
414
// Update state with the new memory
@@ -498,8 +501,8 @@ export class MessageManager {
498
501
499
502
await this . runtime . evaluate ( memory , state , shouldRespond ) ;
500
503
} catch ( error ) {
501
- console . error ( "❌ Error handling message:" , error ) ;
502
- console . error ( "Error sending message:" , error ) ;
504
+ elizaLogger . error ( "❌ Error handling message:" , error ) ;
505
+ elizaLogger . error ( "Error sending message:" , error ) ;
503
506
}
504
507
}
505
508
}
0 commit comments