1- import { Telegraf } from 'telegraf' ;
2- import { HumanMessage , SystemMessage } from '@langchain/core/messages' ;
3- import { DynamicStructuredTool , Tool } from '@langchain/core/tools' ;
4- import { ChatModelWithTools } from './models' ;
5-
6-
7-
1+ import { Telegraf } from "telegraf" ;
2+ import { HumanMessage , SystemMessage } from "@langchain/core/messages" ;
3+ import { DynamicStructuredTool , Tool } from "@langchain/core/tools" ;
4+ import { ChatModelWithTools } from "./models" ;
85
96const SYSTEM_PROMPT = `I am a Telegram bot powered by PolkadotAgentKit. I can assist you with:
107- Transferring native tokens on specific chain (e.g., "transfer 1 WND to 5CSox4ZSN4SGLKUG9NYPtfVK9sByXLtxP4hmoF4UgkM4jgDJ on westend_asset_hub")
@@ -30,24 +27,21 @@ export function setupHandlers(
3027 llm : ChatModelWithTools ,
3128 toolsByName : Record < string , DynamicStructuredTool > ,
3229) : void {
33-
3430 bot . start ( ( ctx ) => {
3531 ctx . reply (
36- ' Welcome to Polkadot Bot!\n' +
37- ' I can assist you with:\n' +
38- '- Transferring native tokens (e.g., "transfer 1 token to westend_asset_hub to 5CSox4ZSN4SGLKUG9NYPtfVK9sByXLtxP4hmoF4UgkM4jgDJ")\n' +
39- '- Checking balance (e.g., "check balance on west/polkadot/kusama")\n' +
40- '- Checking proxies (e.g., "check proxies on westend" or "check proxies")\n' +
41- ' Try asking something!' ,
32+ " Welcome to Polkadot Bot!\n" +
33+ " I can assist you with:\n" +
34+ '- Transferring native tokens (e.g., "transfer 1 token to westend_asset_hub to 5CSox4ZSN4SGLKUG9NYPtfVK9sByXLtxP4hmoF4UgkM4jgDJ")\n' +
35+ '- Checking balance (e.g., "check balance on west/polkadot/kusama")\n' +
36+ '- Checking proxies (e.g., "check proxies on westend" or "check proxies")\n' +
37+ " Try asking something!" ,
4238 ) ;
4339 } ) ;
4440
45-
46- bot . on ( 'text' , async ( ctx ) => {
41+ bot . on ( "text" , async ( ctx ) => {
4742 const message = ctx . message . text ;
4843
49-
50- if ( message . startsWith ( '/' ) ) return ;
44+ if ( message . startsWith ( "/" ) ) return ;
5145
5246 try {
5347 const llmWithTools = llm . bindTools ( Object . values ( toolsByName ) ) ;
@@ -58,43 +52,43 @@ export function setupHandlers(
5852 const aiMessage = await llmWithTools . invoke ( messages ) ;
5953 if ( aiMessage . tool_calls && aiMessage . tool_calls . length > 0 ) {
6054 for ( const toolCall of aiMessage . tool_calls ) {
61-
6255 const selectedTool = toolsByName [ toCamelCase ( toolCall . name ) ] ;
6356 if ( selectedTool ) {
6457 const toolMessage = await selectedTool . invoke ( toolCall ) ;
6558 if ( ! toolMessage || ! toolMessage . content ) {
66- await ctx . reply ( ' Tool did not return a response.' ) ;
59+ await ctx . reply ( " Tool did not return a response." ) ;
6760 return ;
6861 }
69- const response = JSON . parse ( toolMessage . content || '{}' ) ;
62+ const response = JSON . parse ( toolMessage . content || "{}" ) ;
7063 if ( response . error ) {
7164 await ctx . reply ( `Error: ${ response . message } ` ) ;
7265 } else {
73- const content = JSON . parse ( response . content || '{}' ) ;
74- await ctx . reply ( content . data || ' No message from tool.' ) ;
66+ const content = JSON . parse ( response . content || "{}" ) ;
67+ await ctx . reply ( content . data || " No message from tool." ) ;
7568 }
7669 } else {
7770 console . warn ( `Tool not found: ${ toolCall . name } ` ) ;
7871 await ctx . reply ( `Tool ${ toolCall . name } not found.` ) ;
7972 }
8073 }
8174 } else {
82- const content = String ( aiMessage . content || ' No response from LLM.' ) ;
75+ const content = String ( aiMessage . content || " No response from LLM." ) ;
8376 await ctx . reply ( content ) ;
8477 }
8578 } catch ( error ) {
86- console . error ( ' Error handling message:' , error ) ;
79+ console . error ( " Error handling message:" , error ) ;
8780 if ( error instanceof Error ) {
88- console . error ( ' Error stack:' , error . stack ) ;
81+ console . error ( " Error stack:" , error . stack ) ;
8982 }
90- await ctx . reply ( `Sorry, an error occurred: ${ error instanceof Error ? error . message : String ( error ) } ` ) ;
83+ await ctx . reply (
84+ `Sorry, an error occurred: ${ error instanceof Error ? error . message : String ( error ) } ` ,
85+ ) ;
9186 }
9287 } ) ;
9388
94-
9589 bot . catch ( ( err , ctx ) => {
9690 console . error ( `Error for ${ ctx . updateType } ` , err ) ;
97- ctx . reply ( ' An error occurred. Please try again.' ) ;
91+ ctx . reply ( " An error occurred. Please try again." ) ;
9892 } ) ;
9993}
10094
0 commit comments