Learning Timeline
Key Insights
The Importance of Session ID
Make sure to use the 'From ID' from Telegram as the Session ID in the Memory node. Otherwise, the AI will 'forget' the context of the previous conversation every time you send a new message.
Branding Clean-up
Always disable 'Append n8n Attribution' in the Telegram node settings if you want your bot messages to look more professional without the 'Powered by n8n' footer.
Step by Step
Setup Telegram Bot via BotFather
- Open the Telegram app and search for @botfather (ensure the spelling is correct with the @ symbol).
- Send the message '/newbot' to BotFather.
- Enter a name for your bot when prompted.
- Enter a unique 'username' for the bot (this must end with the word 'bot', e.g., MyAIAgent_bot).
- Copy the 'HTTP API access token' provided by BotFather to be used later in n8n.
Configuring the Telegram Trigger in n8n
- In your n8n workflow, search for the 'Telegram' node and select the 'On Message' trigger.
- Click on the 'Credential' section and select 'Create New Credential'.
- Paste the 'Access Token' copied from BotFather into the token field and click 'Save'.
- Click the 'Execute Step' button to start listening mode.
- Open your newly created Telegram bot and press the 'Start' button or send any message to test the connection.
- In n8n, drag the 'text' field from the Telegram output and rename the key to 'message'.
- Click 'Execute Step' again to ensure the message data is successfully pulled in.
Building the AI Agent Workflow
- Add an 'AI Agent' node to your workflow.
- Under the 'Prompt' section, change the setting to 'Define Below'.
- Drag the 'message' field from the Telegram node into the Prompt box.
- Add a 'Chat Model' node (e.g., Anthropic or OpenAI) and connect it to the AI Agent.
- Select your preferred model (e.g., Claude 3.5 Sonnet) and enter your API Key if not already connected.
- Add a 'Window Buffer Memory' or 'Simple Memory' node to the AI Agent to enable back-and-forth conversation.
- Under 'Session ID', select 'Define Below' and drag the 'From ID' field from the Telegram data so the AI recognizes the same user.
Sending the AI Response to Telegram
- Add a 'Telegram' node after the AI Agent and select the 'Send Text Message' action.
- Use the same bot credentials you set up earlier.
- In the 'Chat ID' field, drag the 'From ID' field from the Telegram Trigger input.
- In the 'Text' field, drag the 'response' output (the AI-generated result) from the AI Agent node.
- Open 'Additional Settings', find 'Append n8n Attribution', and switch it to OFF to remove the watermark.
- Click 'Execute Step' and check your Telegram bot to see the response from the AI.