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.