Skip to content

Commit

Permalink
telegram'
Browse files Browse the repository at this point in the history
  • Loading branch information
humanagent committed Dec 24, 2024
1 parent dcb7936 commit 60a7c06
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
6 changes: 3 additions & 3 deletions packages/client-telegram/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@ai16z/client-telegram",
"version": "0.1.6-alpha.4",
"name": "@elizaos/client-telegram",
"version": "0.1.7-alpha.1",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"dependencies": {
"@ai16z/eliza": "workspace:*",
"@elizaos/core": "workspace:*",
"@telegraf/types": "7.1.0",
"telegraf": "4.16.3",
"zod": "3.23.8"
Expand Down
2 changes: 1 addition & 1 deletion packages/client-telegram/src/environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IAgentRuntime } from "@ai16z/eliza";
import { IAgentRuntime } from "@elizaos/core";
import { z } from "zod";

export const telegramEnvSchema = z.object({
Expand Down
4 changes: 2 additions & 2 deletions packages/client-telegram/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { elizaLogger } from "@ai16z/eliza";
import { Client, IAgentRuntime } from "@ai16z/eliza";
import { elizaLogger } from "@elizaos/core";
import { Client, IAgentRuntime } from "@elizaos/core";
import { TelegramClient } from "./telegramClient.ts";
import { validateTelegramConfig } from "./environment.ts";

Expand Down
48 changes: 22 additions & 26 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Message } from "@telegraf/types";
import { Context, Telegraf } from "telegraf";

import { composeContext, elizaLogger, ServiceType } from "@ai16z/eliza";
import { getEmbeddingZeroVector } from "@ai16z/eliza";
import { composeContext, elizaLogger, ServiceType } from "@elizaos/core";
import { getEmbeddingZeroVector } from "@elizaos/core";
import {
Content,
HandlerCallback,
Expand All @@ -13,11 +13,11 @@ import {
State,
UUID,
Media,
} from "@ai16z/eliza";
import { stringToUuid } from "@ai16z/eliza";
} from "@elizaos/core";
import { stringToUuid } from "@elizaos/core";

import { generateMessageResponse, generateShouldRespond } from "@ai16z/eliza";
import { messageCompletionFooter, shouldRespondFooter } from "@ai16z/eliza";
import { generateMessageResponse, generateShouldRespond } from "@elizaos/core";
import { messageCompletionFooter, shouldRespondFooter } from "@elizaos/core";

import { cosineSimilarity } from "./utils";
import {
Expand Down Expand Up @@ -702,7 +702,7 @@ export class MessageManager {
i === 0 && replyToMessageId
? { message_id: replyToMessageId }
: undefined,
parse_mode:'Markdown'
parse_mode: "Markdown",
}
)) as Message.TextMessage;

Expand All @@ -721,21 +721,17 @@ export class MessageManager {
try {
if (/^(http|https):\/\//.test(imagePath)) {
// Handle HTTP URLs
await ctx.telegram.sendPhoto(
ctx.chat.id,
imagePath,
{
caption,
}
);
await ctx.telegram.sendPhoto(ctx.chat.id, imagePath, {
caption,
});
} else {
// Handle local file paths
// Handle local file paths
if (!fs.existsSync(imagePath)) {
throw new Error(`File not found: ${imagePath}`);
}

const fileStream = fs.createReadStream(imagePath);

await ctx.telegram.sendPhoto(
ctx.chat.id,
{
Expand All @@ -746,7 +742,7 @@ export class MessageManager {
}
);
}

elizaLogger.info(`Image sent successfully: ${imagePath}`);
} catch (error) {
elizaLogger.error("Error sending image:", error);
Expand Down Expand Up @@ -1085,7 +1081,7 @@ export class MessageManager {
for (let i = 0; i < sentMessages.length; i++) {
const sentMessage = sentMessages[i];
const isLastMessage = i === sentMessages.length - 1;

const memory: Memory = {
id: stringToUuid(
sentMessage.message_id.toString() +
Expand All @@ -1103,17 +1099,19 @@ export class MessageManager {
createdAt: sentMessage.date * 1000,
embedding: getEmbeddingZeroVector(),
};

// Set action to CONTINUE for all messages except the last one
// For the last message, use the original action from the response content
memory.content.action = !isLastMessage
? "CONTINUE"
: content.action;

await this.runtime.messageManager.createMemory(memory);

await this.runtime.messageManager.createMemory(
memory
);
memories.push(memory);
}

return memories;
}
};
Expand All @@ -1125,14 +1123,12 @@ export class MessageManager {
state = await this.runtime.updateRecentMessageState(state);

// Handle any resulting actions

const actionResult: any = await this.runtime.processActions(
await this.runtime.processActions(
memory,
responseMessages,
state,
callback
);
console.log("actionResult", actionResult);
}

await this.runtime.evaluate(memory, state, shouldRespond);
Expand Down
24 changes: 17 additions & 7 deletions packages/client-telegram/src/telegramClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Context, Telegraf } from "telegraf";
import { message } from 'telegraf/filters';
import { IAgentRuntime, elizaLogger } from "@ai16z/eliza";
import { message } from "telegraf/filters";
import { IAgentRuntime, elizaLogger } from "@elizaos/core";
import { MessageManager } from "./messageManager.ts";
import { getOrCreateRecommenderInBe } from "./getOrCreateRecommenderInBe.ts";

Expand Down Expand Up @@ -67,7 +67,10 @@ export class TelegramClient {
await ctx.reply("Not authorized. Leaving.");
await ctx.leaveChat();
} catch (error) {
elizaLogger.error(`Error leaving unauthorized group ${currentGroupId}:`, error);
elizaLogger.error(
`Error leaving unauthorized group ${currentGroupId}:`,
error
);
}
return false;
}
Expand All @@ -78,10 +81,12 @@ export class TelegramClient {
private setupMessageHandlers(): void {
elizaLogger.log("Setting up message handler...");

this.bot.on(message('new_chat_members'), async (ctx) => {
this.bot.on(message("new_chat_members"), async (ctx) => {
try {
const newMembers = ctx.message.new_chat_members;
const isBotAdded = newMembers.some(member => member.id === ctx.botInfo.id);
const isBotAdded = newMembers.some(
(member) => member.id === ctx.botInfo.id
);

if (isBotAdded && !(await this.isGroupAuthorized(ctx))) {
return;
Expand Down Expand Up @@ -129,9 +134,14 @@ export class TelegramClient {
// Don't try to reply if we've left the group or been kicked
if (error?.response?.error_code !== 403) {
try {
await ctx.reply("An error occurred while processing your message.");
await ctx.reply(
"An error occurred while processing your message."
);
} catch (replyError) {
elizaLogger.error("Failed to send error message:", replyError);
elizaLogger.error(
"Failed to send error message:",
replyError
);
}
}
}
Expand Down

0 comments on commit 60a7c06

Please sign in to comment.