How to Send and Receive Telegram Messages Using NodeMCU (ESP8266)

 

Here is a basic example of the complete code to send and receive Telegram messages using NodeMCU (ESP8266). Make sure to replace `"YOUR_BOT_TOKEN"` and `"YOUR_CHAT_ID"` with your actual bot token and chat ID.


```cpp

#include <ESP8266WiFi.h>

#include <TelegramBot.h>


// Replace with your network credentials

const char* ssid = "YOUR_SSID"; // Your WiFi SSID

const char* password = "YOUR_PASSWORD"; // Your WiFi Password


// Replace with your bot token and chat ID

#define BOT_TOKEN "YOUR_BOT_TOKEN"

#define CHAT_ID "YOUR_CHAT_ID"


TelegramBot bot(BOT_TOKEN);


void setup() {

  Serial.begin(9600);

  WiFi.begin(ssid, password);

  Serial.println("Connecting to WiFi...");


  while (WiFi.status() != WL_CONNECTED) {

    delay(1000);

    Serial.print(".");

  }

  Serial.println("Connected to WiFi");

}


void loop() {

  // Check for new messages

  if (bot.getUpdates()) {

    String message = bot.getLastMessage();

    Serial.println("Received message: " + message);

    

    // Respond to the message

    if (message == "hello") {

      bot.sendMessage(CHAT_ID, "Hello there!");

    } else if (message == "how are you?") {

      bot.sendMessage(CHAT_ID, "I'm fine, thank you!");

    }

  }

  delay(1000); // Check for new messages every second

}

```


### Instructions:

1. Install the necessary libraries in the Arduino IDE (like `TelegramBot`).

2. Replace the placeholders with your actual WiFi credentials, bot token, and chat ID.

3. Upload the code to your NodeMCU.


This code connects to WiFi, checks for incoming messages, and responds based on the received message.

0 Response to "How to Send and Receive Telegram Messages Using NodeMCU (ESP8266)"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel

Iklan Bawah Artikel