Build an AI-powered Telegram bot with conversation and image generation
Build an AI-powered Telegram bot with conversation and image generation
Create a sophisticated Telegram AI chatbot using n8n that can hold natural conversations with ChatGPT, generate images with DALL-E, support multiple languages, and enhance responses with emojis. This tutorial covers the complete workflow from bot registration to deployment.
A full-featured Telegram bot with AI capabilities that can chat naturally, generate images on command, and provide an engaging user experience.
Use Telegram's BotFather to create and configure your bot.
Configure n8n to receive messages from your Telegram bot.
{
"updates": ["message"],
"additionalFields": {
"download": false
}
}Send a 'typing' action to show users the bot is processing their message.
{
"chatId": "={{$json.message.chat.id}}",
"action": "typing"
}Extract and prepare the user's message for AI processing.
// Extract user message
const message = $json.message.text;
const chatId = $json.message.chat.id;
const userId = $json.message.from.id;
// Determine request type
let requestType = 'chat';
if (message.startsWith('/image')) {
requestType = 'image';
message = message.replace('/image', '').trim();
} else if (message.startsWith('/start')) {
requestType = 'welcome';
}
return {
message,
chatId,
userId,
requestType
};Use a Switch node to route different types of requests to appropriate handlers.
Set up OpenAI node for natural language conversation.
System Message:
You are a helpful and friendly Telegram bot assistant. Provide concise, accurate answers to user questions. Use emojis occasionally to make responses more engaging. Keep responses under 300 words for readability in Telegram. Be conversational and helpful.
User: {{$json.message}}Set up image generation for /image commands.
{
"model": "dall-e-2",
"prompt": "={{$json.message}}",
"size": "512x512",
"quality": "standard",
"n": 1
}Configure Telegram nodes to send text or image responses back to users.
{
"chatId": "={{$json.chatId}}",
"text": "={{$json.response}}",
"additionalFields": {
"parse_mode": "Markdown",
"disable_web_page_preview": false
}
}Enhance your bot with additional capabilities.
Creating an AI-powered Telegram bot with n8n is straightforward and doesn't require complex coding. The combination of Telegram's user-friendly platform, OpenAI's powerful models, and n8n's visual workflow builder makes it accessible to developers of all skill levels. Start with basic conversation, then gradually add features like image generation and advanced interactions.