Tired of manually sending notifications or constantly checking for updates? Imagine having a personal assistant that sends you tailored messages right to your Telegram chat. With n8n, a powerful workflow automation tool, this isn’t just a dream – it’s incredibly easy to set up! 🤖✨
In this comprehensive tutorial, we’ll walk you through building your very own Telegram bot using n8n to send automated messages. Whether it’s daily weather reports, stock price alerts, or reminders for your tasks, n8n makes it effortless. Let’s dive in! 🚀
What You’ll Learn Today:
- Creating a Telegram Bot via BotFather.
- Obtaining your Chat ID for targeted messages.
- Setting up n8n Credentials for Telegram.
- Building a Workflow to send automated messages using a Cron trigger.
- Exploring Advanced Possibilities for dynamic and conditional messaging.
Prerequisites:
Before we start, make sure you have the following:
- A Telegram Account: You’ll need it to create and interact with your bot.
- An n8n Instance: This can be a self-hosted instance (Docker, npm, etc.) or an n8n Cloud account.
- Basic Understanding of n8n: Familiarity with nodes, workflows, and triggers will be helpful, but we’ll explain everything along the way!
Step 1: Create Your Telegram Bot 🤖
The first step is to create a bot on Telegram itself. The good news is, Telegram provides a dedicated bot for this purpose: BotFather.
- Open Telegram and search for
@BotFather
. Make sure it’s the official one (it will have a blue checkmark). - Start a chat with BotFather by typing
/start
. - Type
/newbot
and hit Enter. - BotFather will then ask you for a name for your bot. This is the display name users will see (e.g., “My Awesome Alert Bot”).
- Next, it will ask for a username for your bot. This must be unique and end with “bot” (e.g.,
MyAwesomeAlertBot_bot
). -
Once you provide a valid username, BotFather will congratulate you and provide you with a HTTP API Token. This token is like your bot’s password – keep it safe and private! It will look something like
1234567890:ABCDEFGHIJ_KLMNOPQRSTUVWXYZabcdefghij
.🔑 Copy your API Token immediately. You’ll need it for n8n.
Example BotFather Output:
Done! Congratulations on your new bot. You will find it at t.me/MyAwesomeAlertBot_bot. You can now add a description, about section and profile picture for your bot. For a list of commands, see /help. Use this token to access the HTTP API: 1234567890:ABCDEFGHIJ_KLMNOPQRSTUVWXYZabcdefghij For a description of the Telegram Bot API, see this page: https://core.telegram.org/bots/api
Step 2: Get Your Telegram Chat ID 🆔
To send a message, Telegram needs to know where to send it. This “where” is identified by a unique chat_id
. There are a couple of easy ways to get your personal chat ID:
Method A: Using a User Info Bot
- Open Telegram and search for
@userinfobot
. - Start a chat with it by typing
/start
. -
It will immediately reply with your
chat_id
. It typically starts with-
if it’s a group ID, or just numbers for a user ID.Example Output:
Your user ID: 123456789 Chat ID: 123456789
Method B: Using Your New Bot and the Telegram API
This method is slightly more technical but ensures you’re getting the ID associated with your interaction with your specific bot.
- Send any message to your newly created bot (the one from Step 1). You can find it by searching its username (e.g.,
@MyAwesomeAlertBot_bot
). A simple “Hi!” will do. - Open your web browser and go to the following URL, replacing
` with the token you got from BotFather:
https://api.telegram.org/bot/getUpdates` -
You’ll see a JSON response. Look for the
chat
object within themessage
object. Theid
field inside thechat
object is yourchat_id
.Example JSON Snippet (look for
id
insidechat
):{ "ok": true, "result": [ { "update_id": 123456789, "message": { "message_id": 1, "from": { "id": 987654321, "is_bot": false, "first_name": "Your Name" }, "chat": { "id": 987654321, // Stock API -> IF (Stock Price > 100) -> Telegram`
5.3. Real-World Automation Examples 🌐
- Daily Weather Updates:
Cron
node (daily at 7 AM) ->Weather API
node (e.g., OpenWeatherMap) ->Telegram
node.- Message: “Good morning! Today’s weather in [City]: {{ $json.weather.0.description }}, {{ $json.main.temp }}°C ☀️”
- New Blog Post Alerts:
RSS Feed Trigger
node (monitoring your blog’s RSS feed) ->Telegram
node.- Message: “New post on our blog: {{ $json.title }} – Read it here: {{ $json.link }} 📚”
- Stock Price Alerts:
Cron
node (every 15 mins) ->Stock Price API
node ->IF
node (e.g.,if(price > 180)
) ->Telegram
node.- Message: “AAPL just hit $180! Time to check your portfolio! 📈”
- Meeting Reminders:
Google Calendar Trigger
node (when an event is 15 mins away) ->Telegram
node.- Message: “Reminder: Your meeting ‘{{ $json.event.summary }}’ starts in 15 mins! 🗓️”
- Website Downtime Notifications:
HTTP Request
node (pinging your website) ->IF
node (checks for 200 status code) ->Telegram
node (if status is not 200).- Message: “URGENT: Your website is down! Status Code: {{ $json.statusCode }} 🚨”
- Daily Standup Prompts:
Cron
node (daily at 9 AM on weekdays) ->Telegram
node.- Message: “Good morning team! Time for your daily standup. What are your updates for today? 🚀”
Your imagination is the only limit! n8n’s vast array of integrations means you can connect almost any service to your Telegram bot for automated notifications.
Conclusion 🎉
Congratulations! You’ve successfully built a Telegram bot with n8n that sends automated messages. You’ve learned how to:
- Create a bot with BotFather.
- Find your personal Chat ID.
- Set up n8n credentials.
- Build a simple yet powerful workflow with a
Cron
trigger andTelegram
node. - Explored the immense potential for dynamic and conditional messaging.
This is just the beginning of what you can achieve with n8n and Telegram. By automating your notifications, you can save time, stay informed, and make your digital life much smoother.
What will you automate next with your new n8n-powered Telegram bot? Share your ideas in the comments below! 🚀
— G