Unable to connect to Telegram using https with esp8266

I am trying to send a Telegram message using https request. I use the following code:

#include <WiFiManager.h>
#include <WiFiClientSecure.h>

char BotToken[47] = "xxxxxxxxxxxxxxxxxxxxxxxxx";
char CHAT_ID[11]="yyyyyyyyyy";
void sendTelegramMessage(const char* TOKEN, const char* CHAT_ID, const char* text1) {
  WiFiClientSecure client;
  if (!client.connect("api.telegram.org", 443)) {
    Serial.println("Connection failed!");
    return;
  }
  String url = "/bot";
  url += TOKEN;
  url += "/sendMessage?chat_id=";
  url += CHAT_ID;
  url += "&text=";
  url += text1;
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: api.telegram.org\r\n" +
               "User-Agent: ArduinoWiFi/1.1\r\n" +
               "Connection: close\r\n\r\n");
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      break;
    }
  }
  String response = client.readStringUntil('\n');
  Serial.println(response);
}
void setup() {
  // put your setup code here, to run once:
   Serial.begin(115200); // Initialize serial communication
   WiFiManager wifiManager;
if ( ! wifiManager.autoConnect()) {
    // reset and try again, or maybe put it to deep sleep
    ESP.reset();
    delay(1000);
  }
  sendTelegramMessage(BotToken, CHAT_ID, "Hello, world!");
}
void loop() {
  // put your main code here, to run repeatedly:
}

I receive the error message "Connection failed!" which corresponds to the failure of the following line in the code: if (!client.connect("api.telegram.org", 443)) {

The http request works fine when I send it via a browser (firefox) like this:
https://api.telegram.org//botxxxxxxxxxxxxxxxxxxxxxx/sendMessage?chat_id=yyyyyyyyyy&text=Hello, world!

any help is appreciated.

The ESP8266 "BasicHttpsClient" example uses
#include <WiFiClientSecureBearSSL.h>
not
#include <WiFiClientSecure.h>

I tried WiFiClientSecureBearSSL as you advised but it didn't solve the issue. However, when I added : client.setInsecure(); to ignore SSL certificate, it worked. Thank you for your suggestion that made think of SSL certificate issue.

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