Programation and Telegram Node

Hello,

I'm trying to use telegram to advise a high temperature from my sensor and I have two doubts. Firstly I need to do a function to filter out temperatures up to 30. Secondly in Telegram I can't solve the problem about ChatID, I put the firsts numbers of my token and it doesn't work, after it I send a message from Telegram to my Node-Red and I copied the chatID, but not works.

Somebody knows how I can solve that? I attach an image with explanations but the newest Telegram explanation is in this text.

Thank you,
Roger.

Hi,

to send a message you would need something like this in function node

let payload = {
chatId: yourChatID,
type: "message",
content: msg.payload};
return {payload};

You can find your chatID with the telegram-receiver-node. Just connect it to a debug node. Now, go to telegram, find your bot, and start a conversation with f.e. "Hi". In nodered you should see the debug output which includes your chatID

Cheers

1 Like

How can I display the Message? The msg.payload.deveui is the deveui that I'm receiving inside of the MQTT message.

image

In the future please copy/paste the code (using the </> option) instead of a screen shot

  1. You are getting the red X, on line 3, because you haven't defined the variable Message. Since you are referencing msg.Message on line 9, line 3 should be 'msg.Message'
  2. also on line 3, go read this and see if you understand JavaScript Basics: String Concatenation with Variables and Interpolation | by Erica N | Medium

It also looks like you have an extra } (closing curly bracket) on line-10

Sorry for the code zenofmud, next time I did it well. Thanks for your help, now I only need to write well this 3rd line

@dynamicdave good spot, but actually he is missing an open bracket { on line 1

Well seen, thank you dynamicdave

Actually I had this, I only need to search how to wirte the 3rd line well

if (msg.payload.temp_ext >= 20)

msg.Message = "Alert, The sensor with" (msg.payload.deveui) "it's burning";


msg.payload={ 
chatId: 1439100399,
type: "message",
content: msg.Message
}

return msg;

Oops - I hadn't seen that.

So please ignore my comment about line-10 and put an open curly bracket at the end of line-1

You might want to put return msg; inside the { } construct

1 Like

should be:

msg.Message = "Alert, The sensor with" + msg.payload.deveui +  "it's burning";

Assuming msg.payload.deveui is also a string. I am not good enough in js to know what happens if it is not a string...

Edit: Changes & to + so that it is correct for the posterity :slight_smile:

Well finally the correct function in my case is the next. Thanks to all for your help.
@greengolfer I've tried with & but it doesn't works, the Message is incorrect.

if (msg.payload.temp_ext >= 20)

msg.Message = 'Alert, The sensor with ' + msg.payload.deveui + ' its burning';

msg.payload = { 
    chatId: 1439100392,
    type: "message",
    content: msg.Message
}
return msg;

Oups... I messed up :wink: & and +.
My bad.

1 Like

You have missed the { and } from the first and last lines try this...

if (msg.payload.temp_ext >= 20) {

var my_message = 'Alert, The sensor with ' + msg.payload.deveui + ' its burning';

msg.payload = { 
    chatId: 1439100392,
    type: "message",
    content: my_message
   }
return msg;
}

I'm assuming you only want to send a Telegram message if temp_ext is greater than or equal to 20.

Perfect, I'm going to put it.

Yes, that's it.

I do recommend you a basic javascript read-up/training. Doing like you do is to win a Tesla without driver license and trust the autopilot

I know it but for the project tha't I'm doing I need basic functions like this and the other that I had to decode the message.

Yeah, we have all been there, first time. The error you made, I just asked myself if this was the first time you were writing javascript. There are many good sources online for free

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.