# HowTo send a message with variables

**URL:** <https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841>\
**Category:** General\
**Created:** [16 November 2021 18:29 UTC](https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841 "2021-11-16T18:29:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cavekeeper](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Cavekeeper](https://discourse.nodered.org/u/Cavekeeper)\
**Post date:** [16 November 2021 18:29 UTC](https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841/1 "2021-11-16T18:29:55Z")

</div>

I have a working node which sends a static text (This is a test) as a message. It works very well.  
I would now like to replace the static text with a variable which, for example, was passed to me from the previous node. I think it works with a template block. But how does the code look like? Does anyone have an understandable example here?

 ![Bildschirmfoto 2021-11-16 um 19.19.23](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b58e0c42bf7b15cf6946f010ba39e59b1bbe49cb.png)

```auto
[{"id":"b1c50bd382b75e76","type":"inject","z":"8ed57a821485ba05","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":120,"y":2300,"wires":[["176d336035dfc137"]]},{"id":"176d336035dfc137","type":"change","z":"8ed57a821485ba05","name":"SynoChat","rules":[{"t":"set","p":"headers","pt":"msg","to":"{\"content-type\":\"application/x-www-form-urlencoded\"}","tot":"json"},{"t":"set","p":"payload","pt":"msg","to":"payload={\"text\":\"This is a test\"}","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":2300,"wires":[["2c27a0a20f49d272"]]},{"id":"2c27a0a20f49d272","type":"http request","z":"8ed57a821485ba05","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"http://192.168.xxx.x:5000/webapi/entry.cgi?api=SYNO.Chat.External&method=incoming&version=2&token=<secret_token>","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"credentials":{"user":"","password":""},"x":490,"y":2300,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [16 November 2021 18:55 UTC](https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841/2 "2021-11-16T18:55:23Z")

</div>

Here are two methods, it could also be done with a function node

One uses JSONata expression in a change node  
The other uses mustache template in a template node

```auto
[{"id":"b1c50bd382b75e76","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":210,"y":1060,"wires":[["176d336035dfc137","ff4f5412.95355"]]},{"id":"176d336035dfc137","type":"change","z":"b779de97.b1b46","name":"SynoChat","rules":[{"t":"set","p":"headers","pt":"msg","to":"{\"content-type\":\"application/x-www-form-urlencoded\"}","tot":"json"},{"t":"set","p":"payload","pt":"msg","to":"'payload={\"text\":\"' & $$.payload & '\"}'","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":1060,"wires":[["32bb0d03.017b1a"]]},{"id":"ff4f5412.95355","type":"template","z":"b779de97.b1b46","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"payload={\"text\":\"{{{payload}}}\"}","output":"str","x":440,"y":1120,"wires":[["32bb0d03.017b1a"]]},{"id":"32bb0d03.017b1a","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":660,"y":1080,"wires":[]}]

```

Hope it helps

---

<div class="post-metadata">

**Author:** ![Cavekeeper](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Cavekeeper](https://discourse.nodered.org/u/Cavekeeper)\
**Post date:** [16 November 2021 19:05 UTC](https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841/3 "2021-11-16T19:05:12Z")

</div>

Yes, of course. It helps. Very nice. Full function. Thank you.

---

<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:** [30 November 2021 19:05 UTC](https://discourse.nodered.org/t/howto-send-a-message-with-variables/53841/4 "2021-11-30T19:05:23Z")

</div>

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