Rakha Fatih
Published

Telegram bot with NodeMcu

abc

BeginnerFull instructions provided1 hour534
Telegram bot with NodeMcu

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
LED (generic)
LED (generic)
×1
Female/Female Jumper Wires
Female/Female Jumper Wires
×2
Micro-USB to USB Cable (Generic)
Micro-USB to USB Cable (Generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Actions on Google
Actions on Google

Story

Read more

Code

Untitled file

C/C++
#include "CTBot.h"
CTBot myBot;
String ssid = "NA";
String pass = "234567ER";
String token = "1334762804:AAHEfNE2BTWyDhzxWKF3sX72X_P_Utm8Z2k";
uint8_t redLed = D1;
uint8_t greenLed = D2;

void setup() {
Serial.begin(115200);
Serial.println("Starting TelegramBot...");
myBot.wifiConnect(ssid, pass);
myBot.setTelegramToken(token);
// check if all things are ok
if (myBot.testConnection())
Serial.println("\ntestConnection OK");
else
Serial.println("\ntestConnection NOK");
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
void loop() {
TBMessage msg;
if (myBot.getNewMessage(msg)) {
if (msg.text.equalsIgnoreCase("Red Light on")) {
digitalWrite(redLed, HIGH);
myBot.sendMessage(msg.sender.id, "Red Light is now ON");
}
else if (msg.text.equalsIgnoreCase("Red Light off")) {
digitalWrite(redLed, LOW);

myBot.sendMessage(msg.sender.id, "Red Light is now OFF");
}

if (msg.text.equalsIgnoreCase("Green Light on")) {
digitalWrite(greenLed, HIGH);
myBot.sendMessage(msg.sender.id, "Green Light is now ON");
}
else if (msg.text.equalsIgnoreCase("Green Light off")) {
digitalWrite(greenLed, LOW);
myBot.sendMessage(msg.sender.id, "Green Light is now OFF");
}

else {
String reply;
reply = (String)"Welcome " + msg.sender.username + (String)".Try LIGHT ON or LIGHT OFF.";

myBot.sendMessage(msg.sender.id, reply);
}
}
delay(50);
}

Credits

Rakha Fatih
9 projects • 2 followers

Comments