Senin, Januari 18, 2021
mikroavr.com
No Result
View All Result
  • Login
  • Arduino
    • Sensor
    • program Arduino
  • Project
  • Produk
  • IOT
No Result
View All Result
mikroavr
No Result
View All Result
Home IOT

NodeMCU Telegram Bot – How to Limit the number of User that can access our telegram Bot

Jimmi SitepubyJimmi Sitepu
Maret 15, 2018
0 0
0
limit user telegram bot

limit user telegram bot

1
SHARES
5.3k
VIEWS
Share on FacebookShare on Twitter

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.

READ ALSO

Sensor Tekanan Air Dan Udara Dengan Arduino

Enkripsi Data Lora pada Modul ESP32 Lora

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.

  1. Read before how to add board NodeMCU at Arduino IDE
  2. Download telegram Library

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/#/[email protected]_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/#/[email protected]_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[] = "[email protected]";                              // your network key

const char* ssid = "mikroavr.com";
const char* password = "[email protected]";
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 botnodeMCU 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

Tags: nodemcutelegram bot
ShareTweetShare
Previous Post

Fungsi Rangkaian Optocoupler Sebagai Switch,Cara kerja dan Contoh Aplikasi nya

Next Post

Alat Penambah Daya Listrik PLN Dirumah, Murah dan Berkualitas

Jimmi Sitepu

Jimmi Sitepu

Hobbyist Embedded System, Robotic, IOT and write all about them. So we have nice quote "Learning and Sharing"

Related Posts

Project Sensor Tekanan Air dan Udara dengan Arduino
Arduino

Sensor Tekanan Air Dan Udara Dengan Arduino

Desember 20, 2020
Enkripsi Data Lora pada Modul ESP32 Lora
Arduino

Enkripsi Data Lora pada Modul ESP32 Lora

Maret 1, 2020
ESP32 LoRa Node 923MhZ dengan ADC yang lebih Baik dan Stabil
Arduino

ESP32 LoRa Node 923MhZ dengan ADC yang lebih Baik dan Stabil

Februari 23, 2020
ESP32 Telegram Bot Untuk Relay 8 channel dan Sensor DHT21
Arduino

ESP32 Telegram Bot Untuk Relay 8 channel dan Sensor DHT21

Februari 1, 2020
Cara Termudah Monitoring Listrik 3 Phasa dengan Modul ESP32 dan Sensor PZEM-004T v3
IOT

Cara Termudah Monitoring Listrik 3 Phasa dengan Modul ESP32 dan Sensor PZEM-004T v3

Februari 21, 2020
Macam-Macam Sensor Arus pada Rangkaian Elektronik
Arduino

Membaca Sensor PZEM-004T v3 dengan ESP32

Desember 26, 2019
Next Post
Alat Penambah Daya Listrik PLN Dirumah, Murah dan Berkualitas

Alat Penambah Daya Listrik PLN Dirumah, Murah dan Berkualitas

fungsi millis

Fungsi Millis Pada Arduino, Contoh Program dan Pengertiannya

Please login to join discussion

Tulisan Terbaru

Macam Macam Gerbang Logika Pada Sistem Elektronika Digital

Macam Macam Gerbang Logika Pada Sistem Elektronika Digital

Januari 4, 2021
Project Sensor Tekanan Air dan Udara dengan Arduino

Sensor Tekanan Air Dan Udara Dengan Arduino

Desember 20, 2020
Setting Parameter Dengan ESP32 HTML Form melalui Access Point

Setting Parameter Dengan ESP32 HTML Form melalui Access Point

Desember 10, 2020
Mengirim Data Sensor Suhu dengan Komunikasi LoRa

Mengirim Data Sensor Suhu dengan Komunikasi LoRa

November 26, 2020
Memproteksi Program Esp32 dengan Chip Id

Memproteksi Program Esp32 dengan Chip Id

November 25, 2020
Mengenal Pin Out ESP32 Agar Tidak Salah Dalam Penggunaan nya

Mengenal Pin Out ESP32 Agar Tidak Salah Dalam Penggunaan nya

Oktober 2, 2020

Browse by Category

  • Arduino
  • Ebook
  • ESP32
  • IOT
  • jasa
  • Komponen
  • PCB
  • Produk
  • program Arduino
  • Project
  • Rangkaian
  • Sensor
  • Tips
  • Uncategorized

Recent News

Macam Macam Gerbang Logika Pada Sistem Elektronika Digital

Macam Macam Gerbang Logika Pada Sistem Elektronika Digital

Januari 4, 2021
Project Sensor Tekanan Air dan Udara dengan Arduino

Sensor Tekanan Air Dan Udara Dengan Arduino

Desember 20, 2020
  • About US
  • Blog
  • MIKROAVR

© 2020 mikroavr.com - Learning and sharing.

No Result
View All Result
  • About US
  • Blog
  • MIKROAVR

© 2020 mikroavr.com - Learning and sharing.

Welcome Back!

Sign In with Google
OR

Login to your account below

Forgotten Password?

Create New Account!

Sign Up with Google
OR

Fill the forms below to register

All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In