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

Never tried it, but I guess you can add multiple users in the receiver/send nodes:

*Comma separated list of userId authorized to use the chatBot*

Was someone able to send videos recorded by a cam? Last time I tried it wasn’t possible:
“Error: EFATAL: Unsupported Buffer file-type”

It can be done with telegram_bot and a call service node. Make sure you set the bot up in YAML as “broadcast” instead of “polling” as it will cause conflicts with Redbot if not.

image

Here's how the call service node looks configured that way -

2 Likes

I’m trying to set this up but no matter what I do I end up with errors in NodeRed.
It must be something with its config because I was able to complete all six steps and I have received text with testing

When it comes to my NodeRed config:


I’m not sure where the problem lies as when I hit timestamp it records contents of the message in the log in my shared folder but the message is not forwarded to the telegram.

Also I get tons of polling errors:

error: [polling_error] {“code”:“EFATAL”,“message”:“EFATAL: Error: connect ENOENT /bot8888888:token/getUpdates”}

I do not get any output from Authorized node in debug nodes. Finally here is edited output from chatbot debug node in Nodered log in hassio:

-- OUTBOUND MESSAGE --
type:      message
content:   Hello, testing, testing
chatId:    id_number
messageId: null
inbound:   false
silent:    false
fallback:  null
options: 
Error: EFATAL: Error: connect ENOENT /bot888888888:TOKEN/sendMessage
26 Oct 13:15:40 - [error] [chatbot-telegram-node:a9929f17.22e57] Error: EFATAL: Error: connect ENOENT /bot888888888:TOKEN/sendMessage
--------------------- ChatBot context (id:a60b8141.3a12b8) ---------------------
chatId:                    chatid number
userId:                     null
messageId:                  null
transport:                  telegram
authorized:                 false
pending:                    false
language:                   null
currentConversationNode:    24d49947.fea4ee
currentConversationNode_at: Sat Oct 26 2019 13:15:40 GMT+0100
--------------------- ChatBot message (id:a60b8141.3a12b8) ---------------------
type:      message
content:   Hello, testing, testing
chatId:    id_number
messageId: null
inbound:   false
silent:    false
fallback:  null
options:

Any advice would be really great, thx.

1 Like

Do you have a Telegram bot setup with polling in your YAML config somewhere? If so, change it to broadcast and that will stop the errors. If that is the case, you’re basically running the same bot twice and it will cause issues sending messages.

I don’t think you need that conversation node either. Connect the authorized node directly to the text node.

image

No, I do not. I have set it up only in NodeRed.

Is it possible you have the bot running in both the development environment and the production environment, in different nodes?

Also, according to the error, it looks like its coming from chatbot-telegram-node:a9929f17.22e57. Have you looked at that one specifically? That doesn’t look like one of the send/receive nodes as that would be named chatbot-telegram-send or chatbot-telegram-receive.

I was running both in development and in production. I have changed it to development and there is no difference, I’m still getting polling errors.

That error points towards telegram config node on all flows. It disappears when I put authorized node after text node.

If I remove it then I don’t receive any messages on my debug node. I have changed the flow so it looks like below:

Since you mentioned Telegram bot setup in Yaml config, is it also required for this to work?

Yeah, with an inject node you need the conversation node, didn’t think of that earlier.

Setting it up in YAML is only if you want to use a call service node to send messages, rather than using Redbot like you’re doing. AFAIK, there’s no reason to have it setup in YAML if you are using REDBOT.

Are there any other telegram bot nodes other than Redbot that you’ve set up in testing?

I setup this telegram bot along side REdbot when I was testing and had a similar issue as you, after uninstalling it the issue went away

No, I don’t have any other bot nodes.

Did you check some basics first?

  1. Crucial: Did you send a first message with Telegram Messenger App to your bot for initiation of the user?
  2. Suggestion: In your Telegram-bot setting you can add the logfile option. It will write all Telegram communication to a txt file on a specified location. It can be handy to watch what is happening and it even shows all non-authorized communication.
    Synthax: <path_to_logfile_location>/<name_of_logfile>
  3. Crucial: Make sure, when used in HA notification, the HA platform-settings used should be set to Broadcast and not to Polling or Webhook. [link]

FYI, the Authorized-node should only be used behind a Receiver-node. It should not be used for sending. When sending messages it is only sends messages to the user ID setting of the Sender-node
To be more clear, the Authorization-node is used to manage authorized/unauthorized request from the bot to Node-Red.

Option: If you have multiple users configured in the Sender-node (or none = visible to everyone/complete world!), but you only want to send a message to one specific user you should use a Coversation-node. Also when used as @PickleSlice mentioned.

Something another weird thing I noticed. Sometimes it will happen, by some reason, the conversation routing get stuck in a loop. :thinking:

This can be noticed by setting a debug node connected directly to the Receiver-node and watch the Node-Red debug-tab.

When sending a message with the Telegram Messenger app and no data will be shown, you should check the Node-Red Telegram-bot Context settings:

  1. Set the Context Type settings to Memory
  2. Force Node-Red to deploy settings again (move some node, for example, if needed).
  3. Do a Full-Deploy

Now Node-Red will reset the Telegram-bot Context-memory and sometimes things come to live again. :face_with_raised_eyebrow:

Just something I want to share, maybe it can help you solve some strange problems…

To add-on to what @Domoticon said, starting over is sometimes the way to go, as it undoes anything that you might have missed.

Just a simple question…Am I supposed to have to manually ~regexp away all “underscore” characters in my outgoing messages to telegram users?

Since my telegram notifier chokes on them :frowning:

The log says (when sending the text “test3_”):

 [homeassistant.components.telegram_bot] Error sending message: Can't parse entities: can't find end of the entity starting at byte offset 5.

I know the underscore is a markdown char, but should the ha-logic choke on it?

I can survive it now, by adding “| replace” to all my strings I send, but it feels a bit annoying, so I cant help but wonder if it is working as designed…

Br,
Taisto

OK, I have decided to start again from scratch (third time).

  1. Created new bot
  2. Saved new token.
  3. Sent a message to my new bot.
  4. Since the first method in step five never worked for me I used the alternative way to obtain my user ID
  5. Used this link: https://api.telegram.org/bot<AUTHENTICATION_TOKEN>/sendMessage?chat_id=<USER_ID>&text=testing to send URL in my web browser. Received reply message from my bot with text testing.

So far the above worked for me every time. It is only when I move to NodeRed I start to get errors.

In node red in the sender node I have filled following fields:

  1. Bot-Name with @my_bot_username
  2. Token with my token
  3. Users with my username obtained in step 3.
  4. typed in the path to my telegram log in my hassio shared folder: /share/telegram/telegram.log
  5. changed Parse to html
  6. Made sure my bot configuration is for development mode only and not both.

Here is my flow (without the authorization node):

and here is the output from my telegram.log

My_userID < [TELEGRAM] Sun Oct 27 2019 17:11:54 GMT+0000 - Testing, testing, testing

The error from the flow which points to the: 75b9a33a.4b256c node and when I click on it, it points here:
image

I have no clue why this doesn’t work.

Compare what you’re doing to a write up that I did. Maybe you’re missing a step somewhere?

Weird :thinking:

Did you have the parse setting set to html?
Did you disabled the Track and Pass through setting for your test-nodes-setup?
Did you try to reboot the HA Node-Red integration?
Can you try to set the HA Node-Red integration network port host-port to 1880?

Personally I don’t use Node-Red Telegram in the HA Node-Red integration (separate on an other RasPi). I could try that this evening.

EDIT: FYI, ENOENT means ENOENT: no such file or directory. It could be a package (npm) error. Don’t know what to do with this info (yet), though.

@Domoticon

  1. Yes, parse setting changed to html
  2. Yes, this is also disabled
  3. Already tried that several times, I always end up with ENOENT error
  4. My port is already forwarded to 1880.

I have been wondering if this is specific config issue. I run Hassio on my NUC which runs Proxmox and I can reach HA externally with duckdns addon. Just want to clarify one thing, do I need to have Telegram enabled in Home Assistant or is it only required when I want to use call service node with Telegram?

@PickleSlice I will try this as well but so far I can see only one difference, you have parse set to none I have to HTML

I you want to use it only in Node-Red, you don’t have to enable it in HA. When you want to use a HA call service, you have to configure it in HA (not necessary to install a Node-Red Telegram add-on).

@sloma

I just did a test on my Hassio install on a RasPi 3+.

node-red-contrib-chatbot will not work on a Node-Red Hassio integration because of a failing node-pre-gyp install. Now I remembered what @frenck was saying back in April:

As I mentioned before, I use a separate Node-Red server (docker, nodered/node-red-docker). I see this deprecated and is now available as nodered/node-red.

So I suggest you use the HA Telegram integration and call the HA services to use Telegram (like @PickleSlice does)

I will check if node-red-contrib-telegrambot-home is working for Hassio on my RasPi.