NodeMcu
NodeMCU is open source devices and software. NodeMCU is good platform to create IOT project because has wifi Module, large memory flash, and more I/O. Wifi module on nodeMCU makes us able to access internet connection. this point is avantage to create IOT project. This articel, we will show how to make telegram bot and access it with NodeMCU. How to create it?
NodeMCU Telegram Bot
We will try how to control led or other device with telegram bot. for example try send message “/ledon” to our telegram bot, so NodeMCU will receive our message, and led will on. No metter how far telegram user with NodeMCU device. We can control them.
But the problem is public user. Telegram Bot is public user. Every one can search our telegram bot or some one share our telegram bot to each other. so they can control our device too. is big problem. So how to limit user can access our telegram bot? let try together.
Check account Telegram
Before we limit user who can access our telegram bot, let try check our account direct with telegram bot, code below,
/******************************************************************* * this is a basic example how to program a Telegram Bot * * using TelegramBOT library on ESP8266 * * * * Open a conversation with the bot, you can command via Telegram * * a led from ESP8266 GPIO * * https://web.telegram.org/#/im?p=@FlashledBot_bot * * * * written by Giancarlo Bacchio * *******************************************************************/ #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <ESP8266TelegramBOT.h> const char* ssid = "ssid_name"; const char* password = "pass_wifi"; WiFiServer server(80); // Initialize Telegram BOT #define BOTtoken "your_token_bot" //token of FlashledBOT #define BOTname "botName" #define BOTusername "BotUsername" TelegramBOT bot(BOTtoken, BOTname, BOTusername); int Bot_mtbs = 1000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done bool Start = false; /******************************************** * EchoMessages - function to Echo messages * ********************************************/void Bot_ExecMessages() { for (int i = 1; i < bot.message[0][0].toInt() + 1; i++) { bot.message[i][5]=bot.message[i][5].substring(1,bot.message[i][5].length()); if (bot.message[i][5] == "me") { bot.sendMessage(bot.message[i][4], bot.message[i][0], ""); bot.sendMessage(bot.message[i][4], bot.message[i][1], ""); bot.sendMessage(bot.message[i][4], bot.message[i][2], ""); bot.sendMessage(bot.message[i][4], bot.message[i][3], ""); bot.sendMessage(bot.message[i][4], bot.message[i][4], ""); bot.sendMessage(bot.message[i][4], bot.message[i][5], ""); } } bot.message[0][0] = ""; // All messages have been replied - reset new messages } void setup() { Serial.begin(115200); delay(3000); // attempt to connect to Wifi network: Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.begin(ssid,password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.print("http://"); Serial.print(WiFi.localIP()); bot.begin(); // launch Bot functionalities } void loop() { if (millis() > Bot_lasttime + Bot_mtbs) { bot.getUpdates(bot.message[0][1]); // launch API GetUpdates up to xxx message Bot_ExecMessages(); // reply to message with Echo Bot_lasttime = millis(); } }
after we upload code to nodeMCU, try send message to your telegram bot with “/me”. Your telegram bot will replay with your Telegram ID, name, last name and message. Check video below.
this video will show what is our ID. our ID is 11935626. now lets try how to limit user access the NodeMCU telegram bot.
change code like this
/******************************************************************* * this is a basic example how to program a Telegram Bot * * using TelegramBOT library on ESP8266 * * * * Open a conversation with the bot, you can command via Telegram * * a led from ESP8266 GPIO * * https://web.telegram.org/#/im?p=@FlashledBot_bot * * * * written by Giancarlo Bacchio * *******************************************************************/ #include <ESP8266WiFi.h> #include <WiFiClientSecure.h> #include <ESP8266TelegramBOT.h> // Initialize Wifi connection to the router //char ssid[] = "mikroavr.com"; // your network SSID (name) //char pass[] = "jimmiKS@888"; // your network key const char* ssid = "mikroavr.com"; const char* password = "jimmiKS@888"; WiFiServer server(80); // Initialize Telegram BOT #define BOTtoken "your_token" //token of FlashledBOT #define BOTname "xxxx" #define BOTusername "xxxx_bot" TelegramBOT bot(BOTtoken, BOTname, BOTusername); int Bot_mtbs = 1000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done bool Start = false; /******************************************** * EchoMessages - function to Echo messages * ********************************************/void Bot_ExecMessages() { for (int i = 1; i < bot.message[0][0].toInt() + 1; i++) { bot.message[i][5]=bot.message[i][5].substring(1,bot.message[i][5].length()); if (bot.message[i][5] == "ledon") { if ( bot.message[i][1] == "119353626"){ digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level) bot.sendMessage(bot.message[i][4], "Led is ON by Jimmi", ""); } else{ bot.sendMessage(bot.message[i][4], "Unknown User", ""); } } if (bot.message[i][5] == "ledoff") { if ( bot.message[i][1] == "119353626"){ digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level) bot.sendMessage(bot.message[i][4], "Led is Off by Jimmi", ""); } else { bot.sendMessage(bot.message[i][4], "Unknown User", ""); } } if (bot.message[i][5] == "start") { String wellcome = "Wellcome from mikroavr_bot, your personal Bot on alarm realtime notification"; String wellcome1 = "/ledon : to switch the Led ON"; String wellcome2 = "/ledoff : to switch the Led OFF"; String wellcome3 = "/sensor : status all sensor"; if ( bot.message[i][1] == "119353626"){ bot.sendMessage(bot.message[i][4], wellcome, ""); bot.sendMessage(bot.message[i][4], wellcome1, ""); bot.sendMessage(bot.message[i][4], wellcome2, ""); bot.sendMessage(bot.message[i][4], wellcome3, ""); Start = true; } else { bot.sendMessage(bot.message[i][4], "unkwon user", ""); } } } bot.message[0][0] = ""; // All messages have been replied - reset new messages } void setup() { Serial.begin(115200); (3000); // attempt to connect to Wifi network: Serial.print("Connecting Wifi: "); Serial.println(ssid); WiFi.begin(ssid,password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.print("http://"); Serial.print(WiFi.localIP()); bot.begin(); // launch Bot functionalities pinMode(LED_BUILTIN, OUTPUT); // initialize digital pin 2 as an output. } void loop() { if (millis() > Bot_lasttime + Bot_mtbs) { bot.getUpdates(bot.message[0][1]); // launch API GetUpdates up to xxx message Bot_ExecMessages(); // reply to message with Echo Bot_lasttime = millis(); } }
now we can’t control LED with message “/ledon, because the id is not same. we can’t check below
nodeMCU telegram bot
I hope this articel can help us to create nodeMCU telegram bot with safe user from unknow user
download nodeMCU program here
https://github.com/jimmisitepu88/mikroavr.com/tree/master/NodeMCU%20Telegram%20Bot
Thanks