Simple IoT Telegram Notifications & Commanding, Node-RED & Telegram Integration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to create an easy way of interacting with your iot server through a telegram bot I wanted to create a way to receive my sensor data remotely and so I created a telegram bot and node-red flow that would receive a command from a user in this case me then present some options and when the user has made a selection it will reply with that sensor information from an influx DB database I also created some alerting functionality to tell me if any of the sensors in my iot system disconnect from the network or for any reason no sensor readings have been logged for a while and then notify me to go and fix it instead of me only finding out when I come to look at an empty grafana dashboard this video is the third in a series based around iot sensor nodes and servers and today we're going to use the server we created in the first video as a baseline but this should work with any instance of node-red that you have running you just might have to adjust the flows slightly to suit your use case so to get started following this video all you will need is as I said some instance of node-red running and a telegram account there is also a written version of this tutorial on our website linked down below should you need to copy and paste any of the code or commands firstly we're going to set up the node-red plugins that we will need in node-red go to the menu in the top right corner and select manage palette select the install Tab and search for node-red DASH contrib telegram bot and install it you may be prompted to install some prerequisite plugins if it does you'll need to install those too if you're using the same Docker configuration we used to showed in our iot server video so this is a Docker configuration generated through iot stack you might get some errors trying to install the node-red plugins and this is because depending on your configuration the docker container might not know where to look for a DNS server so we can't reach the servers where the plugins are hosted this is an easy fix if you're struggling with this simply create a file called damon.json in slash Etc docker in this file we are going to put the IP address of your DNS server which in my case is my pie hole server and I'm going to use Google's DNS server as a failover you'll need to restart your Docker stack after doing this and you should now be able to install the plugins now we can move into Telegram firstly we want to create a bot this bot will be used to interact with our iot server to create this bot we message the botfather account type Slash new bot then it will prompt you for a name for this bot this is what shows up when you message with it so you can choose what you like here then you'll be asked for a username and this has to be unique once done it'll send you a success message which will include a HTTP API key it is important to keep this key to yourself as anyone who has access to it can use it to control your Bots next we need to get our chat ID by messaging a different bot called my ID bot and use the command slash get ID and you should get your ID back to you after you've noted this down we can leave Telegram and move back to node red the current flow that we have here is nice and straightforward basically we simply take a message from an mqtt server format it a little bit and then push it into an influx DB database we're going to use this workflow as a basis for this demonstration but the things we add to this should be easily adaptable to whatever flow you have going on here we're going to start with a message sent from our server to alert us if we haven't heard from our sensor in a while we're going to use the trigger node which is connected to the telegram send node in the trigger node properties we are initially going to send nothing and then wait for about a minute make sure the extend delay checkbox is selected so when the trigger node receives a message it resets this counter until the next message is received if no message is received within a minute it will send a message and this message is going to be a Json message with the message type the chat ID of your account of your personal account this is so that the bot knows who to send a message to and then the message content so these are the three main things that the telegram sender node requires to send a message moving on to the telegram sender node we need to add a new bot in the properties pane we need to put the username of the bot in the name field and then the bot token in the section below you can add some more security focused options for example you can restrict who can interact with the bot so I'm going to put my telegram username in here if you have more than one user you can put an array of usernames here and you can do the same with chat IDs the rest of the settings can be left as default save the Bots and then we can deploy our flow to test it out I simply disconnected my sensor and in a minute I got a telegram message telling me that that sensor had timed out that we know how to send a message to a telegram chat how about receiving a command we can use the command node which listens for a specific command sent to the bot I'm going to create a flow which will listen for a command from the user and then present them with some sort of options of sensor data that they want to receive and then send that data to them upon selection I'm going to start with the command node in the properties of this node I'll select the bot we previously created and then listen to the command slash data then we're going to use a function node which creates this message we have a variable called opt which contains a Json string of four different sensor readings I want to be able to send to the user you can see that each option has a different callback value this is important that when a reply is received from the user the correct data can be sent back to them you can adapt this inline keyboard to whatever you want you can have more or less than four options the rest of the message contains the chat ID and message type just like the message we sent to the telegram sensor node that we previously created we can then root this function into the sensor node and make sure the bot is selected if you deploy and test this now if you send the command to the bot you should see the options presented to you but nothing will happen when you select them in order for anything to happen when we press these buttons we need to listen to a response from the user we do this using the event Block in the properties of this block select your Bot and in the event drop down select callback query then I use a function to move the current message payload to a different property so it isn't overwritten when it goes into our influx DB node in the influxdb node we select the influxdb database and query it with this query format and this pulls the last entry from a table feel free to adapt this to the way you have configured your influx DB database the output of this is rooted into another function node which essentially is a bunch of if statements to check what the response from the inline keyboard was and sets the message content to the corresponding sensor value from the output of the influx DB query the end of this function adds the parts required for the telegram sender node that this node feeds into now after deploying this flow and sending the command to your telegram bot you should get some data back hopefully this video has been broad enough to allow you to tweak this to your specific setup but as always if you need some advice feel free to post it down in the comments and we will take a look if you do end up using this in your iot server then please let us know how how you're doing it down in the comments I'd really like to hear about the projects you guys are making thank you very much for watching please like the video if it's helped you out and consider subscribing thank you very much and as always have a nice day
Info
Channel: Learn Embedded Systems
Views: 14,056
Rating: undefined out of 5
Keywords:
Id: bGzOImDQlo0
Channel Id: undefined
Length: 8min 22sec (502 seconds)
Published: Tue Feb 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.