Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: client for Telegram account #2839

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ FARCASTER_POLL_INTERVAL=120 # How often (in seconds) the bot should check for
# Telegram Configuration
TELEGRAM_BOT_TOKEN=

# Telegram account client Configuration
TELEGRAM_ACCOUNT_PHONE= # Account phone number for authorization
TELEGRAM_ACCOUNT_APP_ID= # Telegram app api_id (get it at me.telegram.org)
TELEGRAM_ACCOUNT_APP_HASH= # Telegram app api_hash (get it at me.telegram.org)
TELEGRAM_ACCOUNT_DEVICE_MODEL= # Device model. Example: Samsung Galaxy S28+
TELEGRAM_ACCOUNT_SYSTEM_VERSION= # Device system version. Example: Android 12 S? (31)

# Twitter/X Configuration
TWITTER_DRY_RUN=false
TWITTER_USERNAME= # Account username
Expand Down
3 changes: 2 additions & 1 deletion agent/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@elizaos/client-farcaster": "workspace:*",
"@elizaos/client-lens": "workspace:*",
"@elizaos/client-telegram": "workspace:*",
"@elizaos/client-telegram-account": "workspace:*",
"@elizaos/client-twitter": "workspace:*",
"@elizaos/client-instagram": "workspace:*",
"@elizaos/client-slack": "workspace:*",
Expand Down Expand Up @@ -158,4 +159,4 @@
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
}
6 changes: 6 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { InstagramClientInterface } from "@elizaos/client-instagram"
import { LensAgentClient } from "@elizaos/client-lens"
import { SlackClientInterface } from "@elizaos/client-slack"
import { TelegramClientInterface } from "@elizaos/client-telegram"
import { TelegramAccountClientInterface } from "@elizaos/client-telegram-account"
import { TwitterClientInterface } from "@elizaos/client-twitter"
import { AlexaClientInterface } from "@elizaos/client-alexa";
import { MongoDBDatabaseAdapter } from "@elizaos/adapter-mongodb"
Expand Down Expand Up @@ -645,6 +646,11 @@ export async function initializeClients(character: Character, runtime: IAgentRun
if (telegramClient) clients.telegram = telegramClient
}

if (clientTypes.includes(Clients.TELEGRAM_ACCOUNT)) {
const telegramAccountClient = await TelegramAccountClientInterface.start(runtime);
if (telegramAccountClient) clients.telegram_account = telegramAccountClient;
}

if (clientTypes.includes(Clients.TWITTER)) {
const twitterClient = await TwitterClientInterface.start(runtime)
if (twitterClient) {
Expand Down
6 changes: 6 additions & 0 deletions packages/client-telegram-account/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*

!dist/**
!package.json
!readme.md
!tsup.config.ts
3 changes: 3 additions & 0 deletions packages/client-telegram-account/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import eslintGlobalConfig from "../../eslint.config.mjs";

export default [...eslintGlobalConfig];
42 changes: 42 additions & 0 deletions packages/client-telegram-account/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@elizaos/client-telegram-account",
"version": "0.1.9-alpha.1",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"import": {
"@elizaos/source": "./src/index.ts",
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
}
}
},
"files": [
"dist"
],
"dependencies": {
"@elizaos/core": "workspace:*",
"glob": "11.0.0",
"input": "^1.0.1",
"telegram": "2.17.4"
},
"devDependencies": {
"tsup": "8.3.5",
"vitest": "1.1.3",
"@vitest/coverage-v8": "1.1.3"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"lint": "eslint --fix --cache .",
"test": "vitest run",
"test:coverage": "vitest run --coverage"
},
"peerDependencies": {
"whatwg-url": "7.1.0"
}
}
64 changes: 64 additions & 0 deletions packages/client-telegram-account/src/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { IAgentRuntime } from "@elizaos/core";
import { z, ZodError } from "zod";

export const telegramAccountEnvSchema = z.object({
TELEGRAM_ACCOUNT_PHONE: z.string(),
TELEGRAM_ACCOUNT_APP_ID: z.number().int(),
TELEGRAM_ACCOUNT_APP_HASH: z.string(),
TELEGRAM_ACCOUNT_DEVICE_MODEL: z.string(),
TELEGRAM_ACCOUNT_SYSTEM_VERSION: z.string(),
});

export type TelegramAccountConfig = z.infer<typeof telegramAccountEnvSchema>;


function safeParseInt(
value: string | undefined | null,
defaultValue: number = null
): number {
if (!value) return defaultValue;
const parsed = parseInt(value, 10);
return isNaN(parsed) ? defaultValue : Math.max(1, parsed);
}


export async function validateTelegramAccountConfig(
runtime: IAgentRuntime
): Promise<TelegramAccountConfig> {
try {
const telegramAccountConfig = {
TELEGRAM_ACCOUNT_PHONE:
runtime.getSetting("TELEGRAM_ACCOUNT_PHONE") ||
process.env.TELEGRAM_ACCOUNT_PHONE,

TELEGRAM_ACCOUNT_APP_ID: safeParseInt(
runtime.getSetting("TELEGRAM_ACCOUNT_APP_ID") ||
process.env.TELEGRAM_ACCOUNT_APP_ID
),

TELEGRAM_ACCOUNT_APP_HASH:
runtime.getSetting("TELEGRAM_ACCOUNT_APP_HASH") ||
process.env.TELEGRAM_ACCOUNT_APP_HASH,

TELEGRAM_ACCOUNT_DEVICE_MODEL:
runtime.getSetting("TELEGRAM_ACCOUNT_DEVICE_MODEL") ||
process.env.TELEGRAM_ACCOUNT_DEVICE_MODEL,

TELEGRAM_ACCOUNT_SYSTEM_VERSION:
runtime.getSetting("TELEGRAM_ACCOUNT_SYSTEM_VERSION") ||
process.env.TELEGRAM_ACCOUNT_SYSTEM_VERSION
};

return telegramAccountEnvSchema.parse(telegramAccountConfig);
} catch (error) {
if (error instanceof ZodError) {
const errorMessages = error.errors
.map((err) => `${err.path.join(".")}: ${err.message}`)
.join("\n");
throw new Error(
`Telegram account configuration validation failed:\n${errorMessages}`
);
}
throw error;
}
}
19 changes: 19 additions & 0 deletions packages/client-telegram-account/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { elizaLogger } from "@elizaos/core";
import { Client, IAgentRuntime } from "@elizaos/core";
import {TelegramAccountConfig, validateTelegramAccountConfig} from "./environment.ts";
import { TelegramAccountClient } from "./telegramAccountClient.ts"

export const TelegramAccountClientInterface: Client = {
start: async (runtime: IAgentRuntime) => {
const telegramAccountConfig: TelegramAccountConfig = await validateTelegramAccountConfig(runtime);
const telegramAccountClient = new TelegramAccountClient(runtime, telegramAccountConfig);
await telegramAccountClient.start();

return telegramAccountClient;
},
stop: async (_runtime: IAgentRuntime) => {
elizaLogger.warn("Telegram client does not support stopping yet");
},
};

export default TelegramAccountClientInterface;
Loading
Loading