Skip to content

Commit 11bab58

Browse files
committed
support responses going direct to DM, upgrade metadata, ensureConnection
1 parent 0c80ac4 commit 11bab58

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

src/messageManager.ts

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export class MessageManager {
8484
try {
8585
let imageUrl: string | null = null;
8686

87-
logger.info(`Telegram Message: ${message}`);
87+
logger.info(`Telegram Message: ${JSON.stringify(message, null, 2)}`);
8888

8989
if ('photo' in message && message.photo?.length > 0) {
9090
const photo = message.photo[message.photo.length - 1];
@@ -305,12 +305,10 @@ export class MessageManager {
305305
return;
306306
}
307307
// Generate room ID based on whether this is in a forum topic
308-
const roomId = createUniqueUuid(
309-
this.runtime,
310-
threadId ? `${ctx.chat.id}-${threadId}` : ctx.chat.id.toString()
311-
) as UUID;
308+
const telegramRoomid = threadId ? `${ctx.chat.id}-${threadId}` : ctx.chat.id.toString()
309+
const roomId = createUniqueUuid(this.runtime, telegramRoomid) as UUID;
312310

313-
// Get message ID
311+
// Get message ID (unique to channel)
314312
const messageId = createUniqueUuid(this.runtime, message?.message_id?.toString());
315313

316314
// Handle images
@@ -332,6 +330,19 @@ export class MessageManager {
332330
const chat = message.chat as Chat;
333331
const channelType = getChannelType(chat);
334332

333+
await this.runtime.ensureConnection({
334+
entityId,
335+
roomId,
336+
userName: ctx.from.username,
337+
name: ctx.from.first_name,
338+
source: 'telegram',
339+
channelId: telegramRoomid,
340+
serverId: undefined,
341+
type: channelType,
342+
worldId: createUniqueUuid(this.runtime, roomId) as UUID,
343+
worldName: telegramRoomid,
344+
});
345+
335346
// Create the memory object
336347
const memory: Memory = {
337348
id: messageId,
@@ -340,13 +351,25 @@ export class MessageManager {
340351
roomId,
341352
content: {
342353
text: fullText,
354+
// attachments?
343355
source: 'telegram',
344356
channelType: channelType,
345357
inReplyTo:
346358
'reply_to_message' in message && message.reply_to_message
347359
? createUniqueUuid(this.runtime, message.reply_to_message.message_id.toString())
348360
: undefined,
349361
},
362+
metadata: {
363+
entityName: ctx.from.first_name,
364+
entityUserName: ctx.from.username,
365+
fromBot: ctx.from.is_bot,
366+
// include very technical/exact reference to this user for security reasons
367+
// don't remove or change this, spartan needs this
368+
fromId: chat.id,
369+
// why message? all Memories contain content (which is basically a message)
370+
// what are the other types?
371+
type: 'message',
372+
},
350373
createdAt: message.date * 1000,
351374
};
352375

@@ -356,9 +379,19 @@ export class MessageManager {
356379
// If response is from reasoning do not send it.
357380
if (!content.text) return [];
358381

359-
const sentMessages = await this.sendMessageInChunks(ctx, content, message.message_id);
382+
let sentMessages: boolean | Message.TextMessage[] = false
383+
if (content?.source === 'DM') {
384+
sentMessages = []
385+
if (ctx.from) {
386+
// FIXME split on 4096 chars
387+
const res = await this.bot.telegram.sendMessage(ctx.from.id, content.text);
388+
sentMessages.push(res)
389+
}
390+
} else {
391+
sentMessages = await this.sendMessageInChunks(ctx, content, message.message_id);
392+
}
360393

361-
if (!sentMessages) return [];
394+
if (!Array.isArray(sentMessages)) return [];
362395

363396
const memories: Memory[] = [];
364397
for (let i = 0; i < sentMessages.length; i++) {

0 commit comments

Comments
 (0)