diff --git a/esp3d/src/core/espcmd/ESP400.cpp b/esp3d/src/core/espcmd/ESP400.cpp index 23768125..8787fa8d 100644 --- a/esp3d/src/core/espcmd/ESP400.cpp +++ b/esp3d/src/core/espcmd/ESP400.cpp @@ -351,6 +351,8 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type, output->print (ESP_EMAIL_NOTIFICATION); output->print ("\"},{\"line\":\""); output->print (ESP_LINE_NOTIFICATION); + output->print ("\"},{\"telegram\":\""); + output->print (ESP_TELEGRAM_NOTIFICATION); output->print ("\"}]}"); //Token 1 output->print (",{\"F\":\"service\",\"F2\":\"notification\",\"P\":\""); diff --git a/esp3d/src/core/espcmd/ESP610.cpp b/esp3d/src/core/espcmd/ESP610.cpp index 191b71fc..6152d499 100644 --- a/esp3d/src/core/espcmd/ESP610.cpp +++ b/esp3d/src/core/espcmd/ESP610.cpp @@ -44,7 +44,7 @@ bool Commands::ESP610(const char* cmd_params, level_authenticate_type auth_type, if (parameter.length() == 0) { uint8_t Ntype = Settings_ESP3D::read_byte(ESP_NOTIFICATION_TYPE); static String tmp; - tmp = (Ntype == ESP_PUSHOVER_NOTIFICATION)?"PUSHOVER":(Ntype == ESP_EMAIL_NOTIFICATION)?"EMAIL":(Ntype == ESP_LINE_NOTIFICATION)?"LINE":"NONE"; + tmp = (Ntype == ESP_PUSHOVER_NOTIFICATION)?"PUSHOVER":(Ntype == ESP_EMAIL_NOTIFICATION)?"EMAIL":(Ntype == ESP_LINE_NOTIFICATION)?"LINE":(Ntype == ESP_TELEGRAM_NOTIFICATION)?"TELEGRAM":"NONE"; tmp+= " "; tmp+= Settings_ESP3D::read_string(ESP_NOTIFICATION_SETTINGS); output->printMSG (tmp.c_str()); @@ -63,6 +63,8 @@ bool Commands::ESP610(const char* cmd_params, level_authenticate_type auth_type, Ntype = ESP_EMAIL_NOTIFICATION; } else if (parameter == "LINE") { Ntype = ESP_LINE_NOTIFICATION; + } else if (parameter == "TELEGRAM") { + Ntype = ESP_TELEGRAM_NOTIFICATION; } else { output->printERROR("Only NONE, PUSHOVER, EMAIL, LINE are supported!"); return false; diff --git a/esp3d/src/include/defines.h b/esp3d/src/include/defines.h index b168ccfd..70d45bcb 100644 --- a/esp3d/src/include/defines.h +++ b/esp3d/src/include/defines.h @@ -85,6 +85,7 @@ #define ESP_PUSHOVER_NOTIFICATION 1 #define ESP_EMAIL_NOTIFICATION 2 #define ESP_LINE_NOTIFICATION 3 +#define ESP_TELEGRAM_NOTIFICATION 4 //DHT #define NO_DHT_DEVICE 0 diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index 5f1d47a7..1a9325d2 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a31" +#define FW_VERSION "3.0.0.a32" #define REPOSITORY "https://github.com/luc-github/ESP3D" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/notifications/notifications_service.cpp b/esp3d/src/modules/notifications/notifications_service.cpp index 86bdc149..a2888478 100644 --- a/esp3d/src/modules/notifications/notifications_service.cpp +++ b/esp3d/src/modules/notifications/notifications_service.cpp @@ -27,6 +27,8 @@ //* Email: // - https://github.com/CosmicBoris/ESP8266SMTP // - https://www.electronicshub.org/send-an-email-using-esp8266/ +//* Telegram +// - https://medium.com/@xabaras/sending-a-message-to-a-telegram-channel-the-easy-way-eb0a0b32968 #include "../../include/esp3d_config.h" #ifdef NOTIFICATION_FEATURE @@ -62,10 +64,50 @@ typedef WiFiClientSecure TSecureClient; #define LINESERVER "notify-api.line.me" #define LINEPORT 443 +#define TELEGRAMTIMEOUT 5000 +#define TELEGRAMSERVER "api.telegram.org" +#define TELEGRAMPORT 443 + #define EMAILTIMEOUT 5000 NotificationsService notificationsservice; +//https://circuits4you.com/2019/03/21/esp8266-url-encode-decode-example/ +String urlencode(String str) +{ + String encodedString=""; + char c; + char code0; + char code1; + char code2; + for (int i =0; i < str.length(); i++) { + c=str.charAt(i); + if (c == ' ') { + encodedString+= '+'; + } else if (isalnum(c)) { + encodedString+=c; + } else { + code1=(c & 0xf)+'0'; + if ((c & 0xf) >9) { + code1=(c & 0xf) - 10 + 'A'; + } + c=(c>>4)&0xf; + code0=c+'0'; + if (c > 9) { + code0=c - 10 + 'A'; + } + code2='\0'; + encodedString+='%'; + encodedString+=code0; + encodedString+=code1; + //encodedString+=code2; + } + Hal::wait(0); + } + return encodedString; + +} + bool Wait4Answer(TSecureClient & client, const char * linetrigger, const char * expected_answer, uint32_t timeout) { if(client.connected()) { @@ -143,6 +185,8 @@ const char * NotificationsService::getTypeString() return "email"; case ESP_LINE_NOTIFICATION: return "line"; + case ESP_TELEGRAM_NOTIFICATION: + return "telegram"; default: break; } @@ -152,6 +196,7 @@ const char * NotificationsService::getTypeString() bool NotificationsService::sendMSG(const char * title, const char * message) { if(!_started) { + log_esp3d("Error notification not started"); return false; } if (!((strlen(title) == 0) && (strlen(message) == 0))) { @@ -165,6 +210,9 @@ bool NotificationsService::sendMSG(const char * title, const char * message) case ESP_LINE_NOTIFICATION : return sendLineMSG(title,message); break; + case ESP_TELEGRAM_NOTIFICATION : + return sendTelegramMSG(title,message); + break; default: break; } @@ -212,6 +260,45 @@ bool NotificationsService::sendPushoverMSG(const char * title, const char * mess Notificationclient.stop(); return res; } + +//Telegram +bool NotificationsService::sendTelegramMSG(const char * title, const char * message) +{ + String data; + String getcmd; + bool res; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + TSecureClient Notificationclient; +#pragma GCC diagnostic pop +#if defined(ARDUINO_ARCH_ESP8266) && !defined(USING_AXTLS) + Notificationclient.setInsecure(); +#endif //ARDUINO_ARCH_ESP8266 && !USING_AXTLS + if (!Notificationclient.connect(_serveraddress.c_str(), _port)) { + log_esp3d("Error connecting server %s:%d", _serveraddress.c_str(), _port); + return false; + } + (void)title; + //build url for get + data = "/bot"; + data +=_token1; + data += "/sendMessage?chat_id="; + data += _token2; + data += "&text="; + data += urlencode(message); + + //build post query + getcmd = "GET "; + getcmd += data; + getcmd +=" HTTP/1.1\r\nHost: api.telegram.org\r\nConnection: close\r\n\r\n"; + log_esp3d("Query: %s", getcmd.c_str()); + //send query + Notificationclient.print(getcmd); + res = Wait4Answer(Notificationclient, "{", "\"ok\":true", TELEGRAMTIMEOUT); + Notificationclient.stop(); + return res; +} + bool NotificationsService::sendEmailMSG(const char * title, const char * message) { #pragma GCC diagnostic push @@ -400,6 +487,12 @@ bool NotificationsService::begin() _port = PUSHOVERPORT; _serveraddress = PUSHOVERSERVER; break; + case ESP_TELEGRAM_NOTIFICATION: + _token1 = Settings_ESP3D::read_string(ESP_NOTIFICATION_TOKEN1); + _token2 = Settings_ESP3D::read_string(ESP_NOTIFICATION_TOKEN2); + _port = TELEGRAMPORT; + _serveraddress = TELEGRAMSERVER; + break; case ESP_LINE_NOTIFICATION: _token1 = Settings_ESP3D::read_string(ESP_NOTIFICATION_TOKEN1); _port = LINEPORT; diff --git a/esp3d/src/modules/notifications/notifications_service.h b/esp3d/src/modules/notifications/notifications_service.h index af2a0210..1aa8c7f1 100644 --- a/esp3d/src/modules/notifications/notifications_service.h +++ b/esp3d/src/modules/notifications/notifications_service.h @@ -56,6 +56,7 @@ private: bool sendPushoverMSG(const char * title, const char * message); bool sendEmailMSG(const char * title, const char * message); bool sendLineMSG(const char * title, const char * message); + bool sendTelegramMSG(const char * title, const char * message); bool getPortFromSettings(); bool getServerAddressFromSettings(); bool getEmailFromSettings();