cschuhknecht
cschuhknecht

Reputation: 1

Telegram API, ESP8266, sendMessage unable to send Messages

i made myself a Telegram bot to send a message into a group chat when my ESP8266 is powerd, but i dont get the ESP to send Messages using Telegram API. A while ago the code below worked perfectly, but not anymore. And I belive I haven't changed it since then. I searched for a solution a lot, but I wasn't successfull so i hope you can help me.

Here is the code:

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

//------- WiFi Settings -------
char ssid[] = "XXX";       // your network SSID (name)
char password[] = "XXX";  // your network key

// ------- Telegram config --------
#define BOT_TOKEN "XXXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXX"  // your Bot Token (Get from Botfather)

#define CHAT_ID "-XXXXXXXXXXX" // Chat ID of where you want the message to go (You can use MyIdBot to get the chat ID)

// SSL client needed
WiFiClientSecure client;

UniversalTelegramBot bot(BOT_TOKEN, client);

String ipAddress = "";


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

  // Set WiFi to station mode and disconnect from an AP if it was Previously
  // connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  // Attempt to connect to Wifi network:
  Serial.print("Connecting Wifi: ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  IPAddress ip = WiFi.localIP();
  Serial.println(ip);


  Serial.println("START");
}

void loop() {

  Serial.println("ALERT");
  delay(2000);
  Serial.println("STILL ALERT");

  String message = "EINSATZ!";
  if(bot.sendMessage(CHAT_ID, message, "Markdown")){
    Serial.println("TELEGRAM Successfully sent");
  }
  else{
    Serial.println("TELEGRAM NOT Successfully sent");
  }

  delay(300000);

}

Here you can see the serial Output plotted:

Connecting Wifi: XXX
........
WiFi connected
IP address: 
192.168.0.100
START
ALERT
STILL ALERT
TELEGRAM NOT Successfully sent

So the WIFI connection seems to work, but its not sending the message. I checked all the Telegram specific numbers like the Bot Token.

Upvotes: 0

Views: 2306

Answers (1)

TheFoolInTheMirror
TheFoolInTheMirror

Reputation: 11

  1. ensure your Bot-Token is without the prefix "bot" (this was the mistake on my side that prevent it from working)
  2. you need to call "client.setInsecure()" after WiFi is connected (at the end of setup()), because Telegram operates on https. setInsecure() is ... insecure, but the most simple way to handle https-POST on ESP8266

Upvotes: 1

Related Questions