1
1
import { Context , Telegraf } from "telegraf" ;
2
-
3
- import { IAgentRuntime } from "@ai16z/eliza" ;
2
+ import { IAgentRuntime , elizaLogger } from "@ai16z/eliza" ;
4
3
import { MessageManager } from "./messageManager.ts" ;
5
- import { elizaLogger } from "@ai16z/eliza" ;
6
4
7
5
export class TelegramClient {
8
6
private bot : Telegraf < Context > ;
@@ -14,94 +12,88 @@ export class TelegramClient {
14
12
this . runtime = runtime ;
15
13
this . bot = new Telegraf ( botToken ) ;
16
14
this . messageManager = new MessageManager ( this . bot , this . runtime ) ;
17
-
18
15
elizaLogger . log ( "✅ TelegramClient constructor completed" ) ;
19
16
}
20
17
21
18
public async start ( ) : Promise < void > {
22
19
elizaLogger . log ( "🚀 Starting Telegram bot..." ) ;
23
20
try {
24
- this . bot . launch ( {
25
- dropPendingUpdates : true ,
26
- } ) ;
27
- elizaLogger . log (
28
- "✨ Telegram bot successfully launched and is running!"
29
- ) ;
21
+ await this . initializeBot ( ) ;
22
+ this . setupMessageHandlers ( ) ;
23
+ this . setupShutdownHandlers ( ) ;
24
+ } catch ( error ) {
25
+ elizaLogger . error ( "❌ Failed to launch Telegram bot:" , error ) ;
26
+ throw error ;
27
+ }
28
+ }
30
29
31
- await this . bot . telegram . getMe ( ) . then ( ( botInfo ) => {
32
- this . bot . botInfo = botInfo ;
33
- } ) ;
30
+ private async initializeBot ( ) : Promise < void > {
31
+ this . bot . launch ( { dropPendingUpdates : true } ) ;
32
+ elizaLogger . log (
33
+ "✨ Telegram bot successfully launched and is running!"
34
+ ) ;
34
35
35
- elizaLogger . success ( `Bot username: @${ this . bot . botInfo ?. username } ` ) ;
36
+ const botInfo = await this . bot . telegram . getMe ( ) ;
37
+ this . bot . botInfo = botInfo ;
38
+ elizaLogger . success ( `Bot username: @${ botInfo . username } ` ) ;
36
39
37
- this . messageManager . bot = this . bot ;
40
+ this . messageManager . bot = this . bot ;
41
+ }
38
42
39
- // Include if you want to view message maanger bot info
40
- // console .log(`Message Manager bot info: @${this.messageManager.bot}` );
43
+ private setupMessageHandlers ( ) : void {
44
+ elizaLogger . log ( "Setting up message handler..." ) ;
41
45
42
- elizaLogger . log ( "Setting up message handler..." ) ;
46
+ this . bot . on ( "message" , async ( ctx ) => {
47
+ try {
48
+ await this . messageManager . handleMessage ( ctx ) ;
49
+ } catch ( error ) {
50
+ elizaLogger . error ( "❌ Error handling message:" , error ) ;
51
+ await ctx . reply (
52
+ "An error occurred while processing your message."
53
+ ) ;
54
+ }
55
+ } ) ;
43
56
44
- this . bot . on ( "message" , async ( ctx ) => {
45
- try {
46
- // console.log("📥 Received message:", ctx.message);
47
- await this . messageManager . handleMessage ( ctx ) ;
48
- } catch ( error ) {
49
- elizaLogger . error ( "❌ Error handling message:" , error ) ;
50
- await ctx . reply (
51
- "An error occurred while processing your message."
52
- ) ;
53
- }
54
- } ) ;
57
+ this . bot . on ( "photo" , ( ctx ) => {
58
+ elizaLogger . log (
59
+ "📸 Received photo message with caption:" ,
60
+ ctx . message . caption
61
+ ) ;
62
+ } ) ;
55
63
56
- // Handle specific message types for better logging
57
- this . bot . on ( "photo" , ( ctx ) => {
58
- elizaLogger . log (
59
- "📸 Received photo message with caption:" ,
60
- ctx . message . caption
61
- ) ;
62
- } ) ;
64
+ this . bot . on ( "document" , ( ctx ) => {
65
+ elizaLogger . log (
66
+ "📎 Received document message:" ,
67
+ ctx . message . document . file_name
68
+ ) ;
69
+ } ) ;
63
70
64
- this . bot . on ( "document" , ( ctx ) => {
65
- elizaLogger . log (
66
- "📎 Received document message:" ,
67
- ctx . message . document . file_name
68
- ) ;
69
- } ) ;
71
+ this . bot . catch ( ( err , ctx ) => {
72
+ elizaLogger . error ( `❌ Telegram Error for ${ ctx . updateType } :` , err ) ;
73
+ ctx . reply ( "An unexpected error occurred. Please try again later." ) ;
74
+ } ) ;
75
+ }
70
76
71
- this . bot . catch ( ( err , ctx ) => {
77
+ private setupShutdownHandlers ( ) : void {
78
+ const shutdownHandler = async ( signal : string ) => {
79
+ elizaLogger . log (
80
+ `⚠️ Received ${ signal } . Shutting down Telegram bot gracefully...`
81
+ ) ;
82
+ try {
83
+ await this . stop ( ) ;
84
+ elizaLogger . log ( "🛑 Telegram bot stopped gracefully" ) ;
85
+ } catch ( error ) {
72
86
elizaLogger . error (
73
- `❌ Telegram Error for ${ ctx . updateType } :` ,
74
- err
75
- ) ;
76
- ctx . reply (
77
- "An unexpected error occurred. Please try again later."
87
+ "❌ Error during Telegram bot shutdown:" ,
88
+ error
78
89
) ;
79
- } ) ;
90
+ throw error ;
91
+ }
92
+ } ;
80
93
81
- // Graceful shutdown handlers
82
- const shutdownHandler = async ( signal : string ) => {
83
- elizaLogger . log (
84
- `⚠️ Received ${ signal } . Shutting down Telegram bot gracefully...`
85
- ) ;
86
- try {
87
- await this . stop ( ) ;
88
- elizaLogger . log ( "🛑 Telegram bot stopped gracefully" ) ;
89
- } catch ( error ) {
90
- elizaLogger . error (
91
- "❌ Error during Telegram bot shutdown:" ,
92
- error
93
- ) ;
94
- throw error ;
95
- }
96
- } ;
97
-
98
- process . once ( "SIGINT" , ( ) => shutdownHandler ( "SIGINT" ) ) ;
99
- process . once ( "SIGTERM" , ( ) => shutdownHandler ( "SIGTERM" ) ) ;
100
- process . once ( "SIGHUP" , ( ) => shutdownHandler ( "SIGHUP" ) ) ;
101
- } catch ( error ) {
102
- elizaLogger . error ( "❌ Failed to launch Telegram bot:" , error ) ;
103
- throw error ;
104
- }
94
+ process . once ( "SIGINT" , ( ) => shutdownHandler ( "SIGINT" ) ) ;
95
+ process . once ( "SIGTERM" , ( ) => shutdownHandler ( "SIGTERM" ) ) ;
96
+ process . once ( "SIGHUP" , ( ) => shutdownHandler ( "SIGHUP" ) ) ;
105
97
}
106
98
107
99
public async stop ( ) : Promise < void > {
0 commit comments