1- import { Telegraf } from ' telegraf' ;
2- import { HumanMessage , SystemMessage } from ' @langchain/core/messages' ;
3- import { ChatOpenAI } from ' @langchain/openai' ;
4- import { DynamicStructuredTool , Tool } from '@langchain/core/tools' ;
1+ import { Telegraf } from " telegraf" ;
2+ import { HumanMessage , SystemMessage } from " @langchain/core/messages" ;
3+ import { DynamicStructuredTool } from " @langchain/core/tools" ;
4+ import { ChatModelWithTools } from "./models" ;
55
66const SYSTEM_PROMPT = `I am a Telegram bot powered by PolkadotAgentKit. I can assist you with:
77- Transferring native tokens on specific chain (e.g., "transfer 1 WND to 5CSox4ZSN4SGLKUG9NYPtfVK9sByXLtxP4hmoF4UgkM4jgDJ on westend_asset_hub")
@@ -24,29 +24,26 @@ Please provide instructions, and I will assist you!`;
2424
2525export function setupHandlers (
2626 bot : Telegraf ,
27- llm : ChatOpenAI ,
27+ llm : ChatModelWithTools ,
2828 toolsByName : Record < string , DynamicStructuredTool > ,
2929) : void {
30-
3130 bot . start ( ( ctx ) => {
3231 ctx . reply (
33- ' Welcome to Polkadot Bot!\n' +
34- ' I can assist you with:\n' +
35- '- Transferring native tokens (e.g., "transfer 1 token to westend_asset_hub to 5CSox4ZSN4SGLKUG9NYPtfVK9sByXLtxP4hmoF4UgkM4jgDJ")\n' +
36- '- Checking balance (e.g., "check balance on west/polkadot/kusama")\n' +
37- '- Checking proxies (e.g., "check proxies on westend" or "check proxies")\n' +
38- ' 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!" ,
3938 ) ;
4039 } ) ;
4140
42-
43- bot . on ( 'text' , async ( ctx ) => {
41+ bot . on ( "text" , async ( ctx ) => {
4442 const message = ctx . message . text ;
45-
46- if ( message . startsWith ( '/' ) ) return ;
4743
48- try {
44+ if ( message . startsWith ( "/" ) ) return ;
4945
46+ try {
5047 const llmWithTools = llm . bindTools ( Object . values ( toolsByName ) ) ;
5148 const messages = [
5249 new SystemMessage ( { content : SYSTEM_PROMPT } ) ,
@@ -55,43 +52,43 @@ export function setupHandlers(
5552 const aiMessage = await llmWithTools . invoke ( messages ) ;
5653 if ( aiMessage . tool_calls && aiMessage . tool_calls . length > 0 ) {
5754 for ( const toolCall of aiMessage . tool_calls ) {
58-
5955 const selectedTool = toolsByName [ toCamelCase ( toolCall . name ) ] ;
6056 if ( selectedTool ) {
6157 const toolMessage = await selectedTool . invoke ( toolCall ) ;
6258 if ( ! toolMessage || ! toolMessage . content ) {
63- await ctx . reply ( ' Tool did not return a response.' ) ;
59+ await ctx . reply ( " Tool did not return a response." ) ;
6460 return ;
6561 }
66- const response = JSON . parse ( toolMessage . content || '{}' ) ;
67-
62+ const response = JSON . parse ( toolMessage . content || "{}" ) ;
6863 if ( response . error ) {
6964 await ctx . reply ( `Error: ${ response . message } ` ) ;
7065 } else {
71- await ctx . reply ( response . message || response . content || 'No message from tool.' ) ;
66+ const content = JSON . parse ( response . content || "{}" ) ;
67+ await ctx . reply ( content . data || "No message from tool." ) ;
7268 }
7369 } else {
7470 console . warn ( `Tool not found: ${ toolCall . name } ` ) ;
7571 await ctx . reply ( `Tool ${ toolCall . name } not found.` ) ;
7672 }
7773 }
7874 } else {
79- const content = String ( aiMessage . content || ' No response from LLM.' ) ;
75+ const content = String ( aiMessage . content || " No response from LLM." ) ;
8076 await ctx . reply ( content ) ;
8177 }
8278 } catch ( error ) {
83- console . error ( ' Error handling message:' , error ) ;
79+ console . error ( " Error handling message:" , error ) ;
8480 if ( error instanceof Error ) {
85- console . error ( ' Error stack:' , error . stack ) ;
81+ console . error ( " Error stack:" , error . stack ) ;
8682 }
87- 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+ ) ;
8886 }
8987 } ) ;
9088
91-
9289 bot . catch ( ( err , ctx ) => {
9390 console . error ( `Error for ${ ctx . updateType } ` , err ) ;
94- ctx . reply ( ' An error occurred. Please try again.' ) ;
91+ ctx . reply ( " An error occurred. Please try again." ) ;
9592 } ) ;
9693}
9794
0 commit comments