# ESP8266 Telegram Relay

**URL:** <https://forum.arduino.cc/t/esp8266-telegram-relay/1140011>\
**Category:** Programming\
**Created:** [June 20, 2023, 10:31pm UTC](https://forum.arduino.cc/t/esp8266-telegram-relay/1140011 "2023-06-20T22:31:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![looking4new](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/looking4new/32/973031_2.png) [@looking4new](https://forum.arduino.cc/u/looking4new)\
**Post date:** [June 20, 2023, 10:31pm UTC](https://forum.arduino.cc/t/esp8266-telegram-relay/1140011/1 "2023-06-20T22:31:13Z")

</div>

i have program for activate relay with telegram, its work but not really work 100%, because its doesn't give me reply from as my coding do and the wifi esp had connected, thanks for the time before

```arduino
#include <ESP8266WiFi.h>
#include <UniversalTelegramBot.h>

const char* ssid = " *****"; 
const char* password = " *****";
const char* telegramToken = " *****"; 
const char* chat_id = " *****";

WiFiClientSecure client;
UniversalTelegramBot bot(telegramToken, client);
 

const int relayPin = 5;

void setup() {
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi!");

  Serial.println("Telegram bot connected!");
  
  pinMode(relayPin, OUTPUT);
}

void loop() {
  // Mendapatkan pesan dari Telegram
  int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  
  while (numNewMessages) {
    Serial.println("New message received!");
    
    String command = bot.messages[numNewMessages - 1].text;
    Serial.println("Command: " + command);
    
    if (command == "/on") {
      digitalWrite(relayPin, HIGH); // Menyalakan relay
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Relay turned ON!");
    } else if (command == "/off") {
      digitalWrite(relayPin, LOW); // Mematikan relay
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Relay turned OFF!");
    } else {
      bot.sendMessage(bot.messages[numNewMessages - 1].chat_id, "Invalid command!");
    }
    
    numNewMessages--;
  }

  delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![pylon](https://avatars.discourse-cdn.com/v4/letter/p/59ef9b/32.png) [@pylon](https://forum.arduino.cc/u/pylon)\
**Post date:** [June 21, 2023, 3:22pm UTC](https://forum.arduino.cc/t/esp8266-telegram-relay/1140011/2 "2023-06-21T15:22:03Z")

</div>

> [@looking4new](#):
>
> ```arduino
> Serial.println("Connected to WiFi!");
> 
> Serial.println("Telegram bot connected!");
> 
> ```

I have the impression there's something missing between these two lines.

What is your actual question? It probably doesn't work 100% because it doesn't react on WiFi disconnects (or Telegram disconnects I guess, but that part is missing).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [December 18, 2023, 3:22pm UTC](https://forum.arduino.cc/t/esp8266-telegram-relay/1140011/3 "2023-12-18T15:22:33Z")

</div>

Closed on December 18, 2023, 3:22pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
