[HowTo] Setting up a Telegram Bot for all your Home Automation messages

Hi to you all!

Just want to share a simple HowTo to setup a Telegram bot for all your messages from, and to your home automation services.

For example, I use three separate Telegram-bots:

  1. Logging-bot for, ehh, logging. No sounds or pop-ups when messages arrive on my phone/tablet/desktop.
  2. Status-bot for state-changes like Home, Away, Armed and so on. Just a ‘beep’ when messages are received.
  3. Alarm-bot for alarm and emergency messages, including pictures from cameras in breached zones. With a lot of noise!

How to setup a Telegram-bot:

  1. Install Telegram Messenger on your mobile device (also available on any computer/laptop/webbrowser when configuration is completed on your mobile!!).
  2. Search for the BotFather-bot afbeelding in your Telegram Messenger app. afbeelding
  3. Create a new bot by sending the command /newbot to the BotFather. Fill in the necessary information and write down/copy the
    Authentication Token <— important! Keep it safe and secret!!
  4. Go to your newly created bot and send a message to the bot. Doesn’t matter what the message contains.
  5. Get your User ID by sending a URL in a webbrowser:
    https://api.telegram.org/bot<token>/getUpdates (without the brackets)
    Where is the Authentication Token obtained in step 3. Check the JSON output and look for the value behind "id": and write down the value. This is your User ID.
    EDIT 20190702: If this does not work, please try this solution to obtain your User ID.
  6. Check if everything went right by sending this URL in your webbrowser:
    https://api.telegram.org/bot<AUTHENTICATION_TOKEN>/sendMessage?chat_id=<USER_ID>&text=testing
    If everything went right your Telegram-bot send you a message with text testing

Be Aware!! Don’t share your Authentication Token with anyone or post it somewhere on the forum/internet. Keep it secret at all times!!

You can send messages from, and to, the bot using the Telegram API.

Using your bot in Node-Red:

In short, you can use Node-Red nodes to send messages, photo’s, build menu structures and even construct conversations with your bot. The best way to do this is to add special nodes to your Node-Red palette called node-red-contrib-chatbot
Check this page for more info and examples!

You have to configure the Telegram-Sender node with your Telegram-bot configuration details:

Be sure to add your User-ID to the users box, otherwise it will be visible for everyone visiting your bot!! You can also add multiple User-ID’s for all your family/relatives/friends who you want to share the messages with.

It is advised to configure a log file. That way you can always see if you had (foreign) visitors and check this file for debugging/logging.

FYI, the node-red-contrib-chatbot can also be used for Facebook, Slack, Viber and Smooch.

For setting up a Telegram structure in Node Red, check this topic for a step by step configuration example. Special thanks to @commandcontrolit !

I hope I got you started. It’s to your imagination how you are gonna use it, the possibilities are endless.
Have fun!! :crazy_face:

PS. Don’t use < or > characters in messages you send to the bot, it will result in a error message in return by Telegram.

22 Likes

Hey, thanks for the great summary.
I was able to create and integrade a bot with Telegram and HA docs.
I can also send a message via an script but i havent figured out how to send a message with node-red.
I started to use node-red a few weeks ago, so I’m still a noob.

Could you share a flow or write a little tutorial how to use the Telegram features of node-red-contrib-chatbot?

Also I’m getting the same error message over and over again in my logs.
ERROR (MainThread) [homeassistant.components.telegram_bot.polling] wrong status 409

I checked this issue and changed the API key but nothing :frowning:

How do you setup different sounds from each bot?
Can your alarm bit force noise if your phone is in silent?

In the Telegram Messenger App you can select the bot (like you want to start a chat) and click on the name of the bot on the top. A menu will appear where you can select the messenger behaviour when a messages arrives. This can be done for all users/bots separately.

I do not use a way to force sound alerts on a silent phone. I imagine this could be done using the Tasker app of some similar kind of phone automation app.

Please check the wiki of the github page of node-red-contrib-chatbot. There is a lot of explaining and examples.
EDIT: I added the wiki link to the first posting.

I don’t use it for HA yet, because I just started exploring HA, so I’m a complete noob in there… :confused:

My goal is to setup it using MQTT. But at the moment I don’t have a working example where HA is involved. For all my other sevices/IoT/Node-Red it is working like a charm.

EDIT: FYI, see my first steps to accomplish this on this topic:
https://community.home-assistant.io/t/my-first-steps-discovering-home-assistant-setting-up-a-ha-client-to-an-existing-mqtt-broker

Thanks for this documentation!

I have Telegram bot integration with HA already running for a while and it’s a great combination.
I see you are using 3 Telegram bots. It’s also possible to use 1 bot and add that bot to 3 (or more) groups.
Then you can send messages to a group using notify.telegram_group1, notify.telegram_group2 etc.

When using this you have to add the chatid of the group (starting with -) to the allowed_chat_ids of the platform.
Maybe this solution is useful for some people as well.

4 Likes

Thank you for your contribution @gerard33. It’s appreciated!

Thank you for your steps! I had a lot of trial and error, but finally got it working

One tip - seems you have to add the Conversation and Text nodes to send a message to the bot. That’s at least the only way I could make it work.

One question, when I try to add the same bot to another flow in another tab, I get an error that only one instance can exist. How can you use a single bot in multiple flows or tabs?

Thanks!

It just tried on different flows, which looks like it is working. An example:

afbeelding

EDIT: Otherwise you could use the link nodes:
afbeelding afbeelding

1 Like

Hmm, so place a node (bot) onto multiple flow/tabs and you don’t receive errors about multie instances?

Also, using that telegram node-red plugin for receiver is where no matter what, all commands the bot receiced, it came inbound. Even when using chatIDs. Meaning, anyone can talk to the flows where this bot is at.

Using the other telegram node-red plugin (blue color) I could limit it to just my chat ID.

Thanks and sorry of these are silly questions.

Using the Authorized-node should do the trick:

afbeelding
afbeelding

Added in the first posting:

For setting up a Telegram structure in Node Red, check this topic for a step by step configuration example. Special thanks to @commandcontrolit !

1 Like

Sorry, i didnt catch that. How can i avoid the error message? My log is getting flooded with this annoying message.

Check this topic. As far as i understood right, it was because of using packages other then node-red-contrib-chatbot or mixing them.

Anyone has experience with if node-red-contrib-chatbot or node-red-contrib-telegram-home would be best suited, if Telegram is the only messenger app in use?

I’m using node-red-contrib-chatbot and its really powerful. I have a few automations which trigger telegram messages but i havent figured out yet how to track an answer with the sender node :frowning:

Does this answer your question? :wink:

If your Telegram Out node (Telegram sender) has the option ‘Track’ enabled, and you wire loops back to previous commands, it should work. To exit the tracking is ending with Out node with ‘Track’ disabled (no node output).

Check my example in my first posting and/or check the configuration topic for more explanation.

I got it so far :grinning: I just don’t have any plans to use another messaging service than Telegram, so I am curious if the Telegram only package made most sense or if there were some added benefits with the chatbot package beyond supporting more messaging services.

I really don’t know, I never used the ‘blue-colored-node-telegram’ package.

If I understood right, the node-red-contrib-chatbot (orange-colored) package is better/easier for authorisation of received messages by using the ‘Authorisation’ node.

Hmm - I’m new to node-red so this is probably a me thing.

I’ve got @frenck’s addon (2.0.2) with the system packages for g++ and make running.

When I try and install node-red-contrib-chatbot I have this (rather long) log output.

If anyone has any ideas let me know please!

2019-04-08T20:28:32.034Z Install : node-red-contrib-chatbot 0.16.6

2019-04-08T20:28:32.635Z npm install --no-audit --no-update-notifier --save --save-prefix="~" --production [email protected]
2019-04-08T20:28:40.802Z [err] npm
2019-04-08T20:28:40.803Z [err]  WARN deprecated @types/[email protected]: This is a stub types definition. p-cancelable provides its own type definitions, so you do not need this installed.
2019-04-08T20:28:40.818Z [err] npm
2019-04-08T20:28:40.819Z [err]  WARN deprecated @types/[email protected]: This is a stub types definition. p-retry provides its own type definitions, so you do not need this installed.
2019-04-08T20:29:07.745Z [err] npm
2019-04-08T20:29:07.746Z [err]  WARN deprecated [email protected]: use node-fetch instead
2019-04-08T20:29:14.270Z [err] npm
2019-04-08T20:29:14.271Z [err]  
2019-04-08T20:29:14.271Z [err] WARN
2019-04-08T20:29:14.272Z [err]  
2019-04-08T20:29:14.272Z [err] deprecated
2019-04-08T20:29:14.272Z [err]  [email protected]: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
2019-04-08T20:30:43.364Z [out] 
2019-04-08T20:30:43.364Z [out] > [email protected] install /config/node-red/node_modules/grpc
2019-04-08T20:30:43.364Z [out] > node-pre-gyp install --fallback-to-build --library=static_library
2019-04-08T20:30:43.364Z [out] 
2019-04-08T20:30:45.694Z [err] node-pre-gyp
2019-04-08T20:30:45.698Z [err]  
2019-04-08T20:30:45.698Z [err] WARN
2019-04-08T20:30:45.698Z [err]  Using request for node-pre-gyp https download 
2019-04-08T20:30:46.588Z [err] node-pre-gyp
2019-04-08T20:30:46.588Z [err]  WARN Tried to download(404): https://node-precompiled-binaries.grpc.io/grpc/v1.19.0/node-v64-linux-arm-musl.tar.gz 
2019-04-08T20:30:46.588Z [err] node-pre-gyp
2019-04-08T20:30:46.589Z [err]  WARN Pre-built binaries not found for [email protected] and [email protected] (node-v64 ABI, musl) (falling back to source compile with node-gyp)
2019-04-08T20:30:46.589Z [err]  
2019-04-08T20:30:53.422Z [out] make: Entering directory '/config/node-red/node_modules/grpc/build'
2019-04-08T20:30:53.425Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/lib/surface/init.o
[more lines here]
Release/obj.target/grpc/deps/grpc/src/core/lib/debug/trace.o
2019-04-08T20:33:51.487Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/server/secure/server_secure_chttp2.o
2019-04-08T20:33:54.574Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/transport/bin_decoder.o
2019-04-08T20:33:55.651Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/transport/bin_encoder.o
2019-04-08T20:33:56.820Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_plugin.o
2019-04-08T20:33:59.065Z [out]   CXX(target) Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.o
2019-04-08T20:34:01.460Z [err] ../deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.cc: In function 'grpc_error* try_http_parsing(grpc_chttp2_transport*)':
2019-04-08T20:34:01.460Z [err] ../deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.cc:2466:40: error: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type 'grpc_http_response' {aka 'struct grpc_http_response'}; use assignment or value-initialization instead [-Werror=class-memaccess]
2019-04-08T20:34:01.460Z [err]    memset(&response, 0, sizeof(response));
2019-04-08T20:34:01.460Z [err]                                         ^
2019-04-08T20:34:01.460Z [err] In file included from ../deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.cc:44:
2019-04-08T20:34:01.460Z [err] ../deps/grpc/src/core/lib/http/parser.h:71:16: note: 'grpc_http_response' {aka 'struct grpc_http_response'} declared here
2019-04-08T20:34:01.460Z [err]  typedef struct grpc_http_response {
2019-04-08T20:34:01.460Z [err]                 ^~~~~~~~~~~~~~~~~~
2019-04-08T20:34:13.038Z [err] cc1plus: all warnings being treated as errors
2019-04-08T20:34:13.154Z [err] make: *** [grpc.target.mk:481: Release/obj.target/grpc/deps/grpc/src/core/ext/transport/chttp2/transport/chttp2_transport.o] Error 1
2019-04-08T20:34:13.154Z [out] make: Leaving directory '/config/node-red/node_modules/grpc/build'
2019-04-08T20:34:13.169Z [err] gyp
2019-04-08T20:34:13.174Z [err]  
2019-04-08T20:34:13.175Z [err] ERR!
2019-04-08T20:34:13.175Z [err]  build error 
2019-04-08T20:34:13.178Z [err] gyp
2019-04-08T20:34:13.178Z [err]  ERR! stack
2019-04-08T20:34:13.178Z [err]  Error: `make` failed with exit code: 2
2019-04-08T20:34:13.178Z [err] gyp
2019-04-08T20:34:13.178Z [err]  ERR! 
2019-04-08T20:34:13.179Z [err] stack     at ChildProcess.onExit (/usr/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23)
2019-04-08T20:34:13.179Z [err] gyp
2019-04-08T20:34:13.179Z [err]  ERR! 
2019-04-08T20:34:13.179Z [err] stack     at ChildProcess.emit (events.js:182:13)
2019-04-08T20:34:13.179Z [err] gyp 
2019-04-08T20:34:13.179Z [err] ERR! stack
2019-04-08T20:34:13.180Z [err]      at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
2019-04-08T20:34:13.181Z [err] gyp
2019-04-08T20:34:13.181Z [err]  ERR! System
2019-04-08T20:34:13.181Z [err]  Linux 4.14.98-v7
2019-04-08T20:34:13.182Z [err] gyp
2019-04-08T20:34:13.182Z [err]  ERR! command
2019-04-08T20:34:13.182Z [err]  "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--library=static_library" "--module=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl/grpc_node.node" "--module_name=grpc_node" "--module_path=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl" "--napi_version=3" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"
2019-04-08T20:34:13.183Z [err] gyp
2019-04-08T20:34:13.183Z [err]  ERR!
2019-04-08T20:34:13.183Z [err]  cwd /config/node-red/node_modules/grpc
2019-04-08T20:34:13.183Z [err] gyp
2019-04-08T20:34:13.183Z [err]  ERR!
2019-04-08T20:34:13.183Z [err]  node -v
2019-04-08T20:34:13.184Z [err]  v10.14.2
2019-04-08T20:34:13.184Z [err] gyp 
2019-04-08T20:34:13.184Z [err] ERR! 
2019-04-08T20:34:13.184Z [err] node-gyp -v v3.8.0
2019-04-08T20:34:13.184Z [err] gyp
2019-04-08T20:34:13.185Z [err]  ERR! 
2019-04-08T20:34:13.185Z [err] not ok 
2019-04-08T20:34:13.194Z [err] node-pre-gyp
2019-04-08T20:34:13.194Z [err]  ERR! build error
2019-04-08T20:34:13.194Z [err]  
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --library=static_library --module=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl/grpc_node.node --module_name=grpc_node --module_path=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/config/node-red/node_modules/grpc/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:962:16)
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! System Linux 4.14.98-v7
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! 
2019-04-08T20:34:13.202Z [err] command "/usr/bin/node" "/config/node-red/node_modules/grpc/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build" "--library=static_library"
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! cwd /config/node-red/node_modules/grpc
2019-04-08T20:34:13.202Z [err] node-pre-gyp ERR! node -v v10.14.2
2019-04-08T20:34:13.203Z [err] node-pre-gyp ERR!
2019-04-08T20:34:13.203Z [err]  node-pre-gyp -v
2019-04-08T20:34:13.203Z [err]  v0.12.0
2019-04-08T20:34:13.203Z [err] node-pre-gyp 
2019-04-08T20:34:13.203Z [err] ERR!
2019-04-08T20:34:13.203Z [err]  not ok
2019-04-08T20:34:13.203Z [err]  
2019-04-08T20:34:13.207Z [out] Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --library=static_library --module=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl/grpc_node.node --module_name=grpc_node --module_path=/config/node-red/node_modules/grpc/src/node/extension_binary/node-v64-linux-arm-musl --napi_version=3 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
2019-04-08T20:34:17.309Z [err] npm
2019-04-08T20:34:17.310Z [err]  WARN [email protected] requires a peer of bufferutil@^3.0.3 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.310Z [err] npm 
2019-04-08T20:34:17.310Z [err] WARN [email protected] requires a peer of erlpack@discordapp/erlpack but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.310Z [err] npm
2019-04-08T20:34:17.311Z [err]  WARN
2019-04-08T20:34:17.311Z [err]  [email protected] requires a peer of node-opus@^0.2.7 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.311Z [err] npm 
2019-04-08T20:34:17.311Z [err] WARN
2019-04-08T20:34:17.311Z [err]  [email protected] requires a peer of opusscript@^0.0.6 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.311Z [err] npm 
2019-04-08T20:34:17.311Z [err] WARN
2019-04-08T20:34:17.311Z [err]  [email protected] requires a peer of sodium@^2.0.3 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.312Z [err] npm WARN
2019-04-08T20:34:17.312Z [err]  [email protected] requires a peer of libsodium-wrappers@^0.7.3 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.312Z [err] npm
2019-04-08T20:34:17.312Z [err]  WARN
2019-04-08T20:34:17.312Z [err]  [email protected] requires a peer of uws@^9.14.0 but none is installed. You must install peer dependencies yourself.
2019-04-08T20:34:17.313Z [err] 
2019-04-08T20:34:17.380Z [err] npm
2019-04-08T20:34:17.381Z [err]  ERR! code ELIFECYCLE
2019-04-08T20:34:17.382Z [err] npm
2019-04-08T20:34:17.382Z [err]  ERR! 
2019-04-08T20:34:17.382Z [err] errno 1
2019-04-08T20:34:17.384Z [err] npm
2019-04-08T20:34:17.384Z [err]  ERR! [email protected] install: `node-pre-gyp install --fallback-to-build --library=static_library`
2019-04-08T20:34:17.384Z [err] npm ERR!
2019-04-08T20:34:17.385Z [err]  Exit status 1
2019-04-08T20:34:17.385Z [err] npm
2019-04-08T20:34:17.386Z [err]  ERR!
2019-04-08T20:34:17.386Z [err]  
2019-04-08T20:34:17.386Z [err] npm
2019-04-08T20:34:17.386Z [err]  ERR!
2019-04-08T20:34:17.387Z [err]  Failed at the [email protected] install script.
2019-04-08T20:34:17.387Z [err] npm 
2019-04-08T20:34:17.387Z [err] ERR!
2019-04-08T20:34:17.387Z [err]  This is probably not a problem with npm. There is likely additional logging output above.
2019-04-08T20:34:17.675Z [err] 
2019-04-08T20:34:17.676Z [err] npm ERR!
2019-04-08T20:34:17.676Z [err]  A complete log of this run can be found in:
2019-04-08T20:34:17.676Z [err] npm ERR!
2019-04-08T20:34:17.680Z [err]      /root/.npm/_logs/2019-04-08T20_34_17_572Z-debug.log
2019-04-08T20:34:17.747Z rc=1