Sending Messages To Telegram Via NodeMCU

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <SoftwareSerial.h>

const char* ssid = "MyWifi";           // Enter your WIFI SSID
const char* password = "12345054321";  // Enter your WIFI Password

#define BOTtoken "XXXXXXXXXXXXXXXXXXXXXXXXX"  // Enter the bottoken you got from botfather
#define CHAT_ID "XXXXXXXXXXXXXXXXXXXXXX"                                      // Enter your CHAT_ID you got from CHAT_ID bot

SoftwareSerial mySerial(5, 4);  // RX, TX
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);

// Function declaration for sendMessage
void sendMessage(String message);

void setup() {
  Serial.begin(115200);
  mySerial.begin(9600);
  configTime(0, 0, "pool.ntp.org");
  client.setTrustAnchors(&cert);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  int a = 0;
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
    a++;
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  sendMessage("Wifi Connected!");
  sendMessage("System has Started!!");
}

void loop() {
  String msg = mySerial.readStringUntil('\r');
  Serial.println(msg);
  sendMessage(msg);
  delay(5000);
}

// Function definition for sendMessage
void sendMessage(String message) {
  int response = bot.sendMessage(CHAT_ID, message, "Markdown");
  if (response == 200) {
    Serial.println("Message sent successfully");
  } else {
    Serial.print("Failed to send message. HTTP error code: ");
    Serial.println(response);
  }
}

This NodeMCU is taking a value from Arduino Uno via Serial Connection and printing properly and also connecting to wifi but is not sending message on telegram group. Can someone tell the mistakes or fix the code.

Interesting, Should it?

Do you have solution ?

For what? THere is no question in your post #1

Message is not going on telegram group. Can you help me fix it?

Remove that "Markdown" in bot.SendMessage. Doublecheck CHAT_ID and BOTtoken. Fire up wireshark and see what is sent/received.

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