A Telegram bot for irrigation alerts and remote control
Commands in, one function to rule them
Status built from flow context
Alarms push themselves
A snippet from the implementation
Straight from the example as deployed on the Raspberry Pi (Docker) — copy it freely:
const AUTHORIZED_CHAT = Number(env.get("TELEGRAM_CHAT_ID"));
const chatId = msg.payload.chatId;
const text = (msg.payload.content || "").trim().toLowerCase();
if (chatId !== AUTHORIZED_CHAT) {
// Unknown chat: ignore it and leave a trace in the log
node.warn(`Command rejected from unauthorized chat: ${chatId}`);
return null;
}
const COMMANDS = ["/status", "/irrigate", "/stop", "/alarms"];
msg.cmd = COMMANDS.includes(text.split("@")[0]) ? text.split("@")[0] : "help";
msg.chatId = chatId;
return msg;
// ===========================================================================
// FUNCTION B — "Reply with status"
// Reads from context the latest values published by the other tabs.
// ===========================================================================
/*
const irrigationActive = flow.get("irrigation_active") || false;
const weather = flow.get("last_weather") || {};
const photovoltaic = flow.get("last_pv") || {}; // from the Fronius tab
const lines = [
irrigationActive ? "🟢 Irrigation RUNNING" : "⚪ Irrigation stopped",
`PV: ${photovoltaic.generated_w || "?"} W generated, ` +
`${photovoltaic.surplus_w || "?"} W of surplus`,The full example is a complete program — wiring header, setup and main loop — ready to adapt to your application.
Frequently asked questions
How do I create the bot and get the token?
Talk to @BotFather on Telegram, create a new bot and copy the token into the TELEGRAM_TOKEN environment variable of the Node-RED container. Configure the telegrambot credentials node to read it from the environment instead of pasting it.
How do I find my chat id for the whitelist?
Send any message to your new bot and inspect msg.payload.chatId in a debug node, or query the getUpdates endpoint of the Bot API. Put that number in TELEGRAM_CHAT_ID and the parser will reject every other chat.
Is Telegram reliable enough for critical alarms?
Treat it as a notification channel, not a safety layer. Hardwired protections (thermal relays, pressure switches) still act locally; Telegram tells the human what already happened and lets them stop or start cycles remotely.