From Telegram bot to Assist and back

Hello everybody,

Firstly I want to thank everyone in this great community for helpful information that I read in the past month or so and made my first steps in Home Assistant. Big thanks to developers of this amazing software.

My journey in HA started not long ago, but I found it very addictive and spent almost all of my free time learning and using it. I don’t have a big home nor many smart devices, but I tried to make some of them smart with a DIY approach using ESPHome. My first host for HA was a repurposed Android TV box (arch64 Linux on SD card, Docker, etc.), which was pretty slow and only enough for basic tasks. Then I realised that I could run HAOS headless on an old laptop (Intel N3060 4GB RAM, SSD, etc.) that was collecting dust on a shelf. That was a much better experience, and I was tempted to try Voice Assist, but I was disappointed with the performance of Whisper, Piper… and simply said it was too slow for everyday use. I was too optimistic about hardware requirements… now I know that it was on a shelf for a reson :frowning: Time to reconsider other options till some better hardware comes along.

Back to square one, open NodeRed, set Telegram bot and write commands for all devices that I want to control as I did on previous setup. While doing this, an idea occurred to me: perhaps I could send words from Telegram to Assist and receive a response in Telegram? I’ll save time using this method and get everything from Assist and then I could add custom commands. I searched to see if someone had made something similar but didn’t find any. After some thinking, I made this, and I’m very happy with the results.

[{"id":"43daba42dc1073e0","type":"telegram receiver","z":"c018c05e16a1f1cf","name":"Receiver","bot":"4f4ec733844193d3","saveDataDir":"","filterCommands":true,"x":105,"y":125,"wires":[["0716b1cfd8c37767"],[]]},{"id":"97a5171027419807","type":"api-call-service","z":"c018c05e16a1f1cf","name":"Start conversation","server":"e46d2e3c.94c37","version":5,"debugenabled":false,"domain":"conversation","service":"process","areaId":[],"deviceId":[],"entityId":[],"data":"{\"text\":\"Turn all lights on\",\"language\":\"NL\",\"agent_id\":\"homeassistant\"}","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"},{"property":"payload.chatId","propertyType":"msg","value":"originalMessage.chat.id","valueType":"msg"},{"property":"payload.type","propertyType":"msg","value":"message","valueType":"str"},{"property":"payload.content","propertyType":"msg","value":"payload.response.speech.plain.speech","valueType":"msg"}],"queue":"none","x":580,"y":125,"wires":[["4ca0611f1949a8de"]]},{"id":"0716b1cfd8c37767","type":"change","z":"c018c05e16a1f1cf","name":"Change payload","rules":[{"t":"set","p":"payload.text","pt":"msg","to":"payload.content","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":125,"wires":[["97a5171027419807"]]},{"id":"4a53450f543e0be1","type":"telegram sender","z":"c018c05e16a1f1cf","name":"Sender","bot":"4f4ec733844193d3","haserroroutput":false,"outputs":1,"x":1075,"y":125,"wires":[[]]},{"id":"4ca0611f1949a8de","type":"change","z":"c018c05e16a1f1cf","name":"Clean up message","rules":[{"t":"delete","p":"payload.response","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":840,"y":125,"wires":[["4a53450f543e0be1"]]}]

Set your telegram bot details in the Receiver and Sender nodes, pick your Home Assistant server instance in the Start Conversation node, and you can start chat with your HA Assist (conversation agent) using typical HA Assist sentences.You can even use Telegram’s speech to text; maybe a reply too can be sent as voice, but I didn’t want to make things too complicated as I usually do.

Maybe I reinvented the wheel and someone already came up with the same idea, but I didn’t search hard enough, or maybe this will help someone. I will be very pleased if anyone finds this helpful and I want to give my small contribution to the community.

This is my first post and I hope I didn’t miss category. I left out information on how to set up Node-RED and Telegram in HA because it has been discussed in many other topics.

Any comments are welcome.

1 Like

How would you modify the call_service node to use the extended open ai conversation plugin?

1 Like

Hi, I’m just trying to do the same and I struggle with setting everything up in Node Red. My Telegram receiver works just fine, but I cant figure out how to continue. Would you please tell me how exactly you set everything up in Node Red? Thank you!

EDIT: Never mind, I figured it out. :slight_smile:

But I would also love to know how can I use my ChatGPT to process the request, the same way it is taking care of direct input in my Assist. Do you have any idea?

I was able to figure this out, with great support from HA community on Discord:

{“text”:“{{payload.text}}”,“language”:“EN”,“agent_id”:“conversation.chatgpt”,“conversation_id”:“xxxxxxxxxxxxxxx”}

I changed the call_service “Data” field in this way and it now works. The agent id of course can vary in your case, you need to check your agent id by going to

1.Dev Tools > Actions and select conversation.process
2. check the Agent checkbox
3. select OpenAI from the dropdown
4. click on “Go to YAML mode”

you will see the value for the OpenAI Conversation agent: conversation.openai_conversation