# Programation and Telegram Node

**URL:** <https://discourse.nodered.org/t/programation-and-telegram-node/38501>\
**Category:** General\
**Created:** [4 January 2021 08:57 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501 "2021-01-04T08:57:40Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 08:57 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/1 "2021-01-04T08:57:40Z")

</div>

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.

 ![Lastnode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/090a70cd98197482267e5460095a2bbcba763c13.png)

Thank you,  
Roger.

---

<div class="post-metadata">

**Author:** ![jodelkoenig](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jodelkoenig/32/44645_2.png) [@jodelkoenig](https://discourse.nodered.org/u/jodelkoenig)\
**Post date:** [4 January 2021 09:36 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/2 "2021-01-04T09:36:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 10:08 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/3 "2021-01-04T10:08:16Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/3787ec11dca4c5cc7ce1f5a8adb0c306165afd2e.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 January 2021 10:22 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/4 "2021-01-04T10:22:38Z")

</div>

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](https://medium.com/@endubueze00/javascript-basics-string-concatenation-with-variables-and-interpolation-deba239debbe)

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 January 2021 10:27 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/5 "2021-01-04T10:27:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 10:30 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/6 "2021-01-04T10:30:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 January 2021 10:30 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/7 "2021-01-04T10:30:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 10:30 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/8 "2021-01-04T10:30:32Z")

</div>

Well seen, thank you dynamicdave

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 10:31 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/9 "2021-01-04T10:31:14Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 January 2021 10:31 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/10 "2021-01-04T10:31:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [4 January 2021 10:43 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/11 "2021-01-04T10:43:12Z")

</div>

> [@Roger](#):
>
> `msg.Message = "Alert, The sensor with" (msg.payload.deveui) "it's burning";`

should be:

```auto
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 🙂

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 10:49 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/12 "2021-01-04T10:49:27Z")

</div>

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.

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [4 January 2021 10:50 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/13 "2021-01-04T10:50:43Z")

</div>

Oups... I messed up 😉 & and +.  
My bad.

---

<div class="post-metadata">

**Author:** ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)\
**Post date:** [4 January 2021 10:53 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/14 "2021-01-04T10:53:29Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 11:40 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/15 "2021-01-04T11:40:35Z")

</div>

Perfect, I'm going to put it.

> [@dynamicdave](#):
>
> I'm assuming you only want to send a Telegram message if `temp_ext` is greater than or equal to 20.

Yes, that's it.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [4 January 2021 11:43 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/16 "2021-01-04T11:43:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Roger](https://avatars.discourse-cdn.com/v4/letter/r/c0e974/32.png) [@Roger](https://discourse.nodered.org/u/Roger)\
**Post date:** [4 January 2021 11:48 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/17 "2021-01-04T11:48:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [4 January 2021 11:50 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/18 "2021-01-04T11:50:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [18 January 2021 11:50 UTC](https://discourse.nodered.org/t/programation-and-telegram-node/38501/19 "2021-01-18T11:50:59Z")

</div>

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