From d9f6ea617219f0d0c7f458997eab2b5bb3645e84 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:45:09 +0800 Subject: [PATCH] close #888 --- docs/esp3dcnf.ini | 2 +- esp3d/src/core/commands/ESP0.cpp | 2 +- esp3d/src/core/commands/ESP610.cpp | 6 +- esp3d/src/core/esp3d_string.cpp | 22 +++++++ esp3d/src/core/esp3d_string.h | 1 + esp3d/src/include/esp3d_defines.h | 1 + esp3d/src/include/esp3d_version.h | 2 +- .../notifications/notifications_service.cpp | 57 +++++++++++++++++++ .../notifications/notifications_service.h | 1 + esp3d/src/modules/update/update_service.cpp | 6 +- 10 files changed, 92 insertions(+), 8 deletions(-) diff --git a/docs/esp3dcnf.ini b/docs/esp3dcnf.ini index 9a54a76d..5a68ec10 100644 --- a/docs/esp3dcnf.ini +++ b/docs/esp3dcnf.ini @@ -108,7 +108,7 @@ FTP_Passive_Port = 55600 #Auto notification AUTONOTIFICATION = Yes -#Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant +#Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant / WhatsApp NOTIF_TYPE = None #Notification token 1 string of 64 chars max diff --git a/esp3d/src/core/commands/ESP0.cpp b/esp3d/src/core/commands/ESP0.cpp index 07ee9a6c..47ba3614 100644 --- a/esp3d/src/core/commands/ESP0.cpp +++ b/esp3d/src/core/commands/ESP0.cpp @@ -143,7 +143,7 @@ const char* help[] = { #endif // AUTHENTICATION_FEATURE #if defined(NOTIFICATION_FEATURE) "[ESP600](message) - send notification", - "[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT/HOMEASSISTANT) " + "[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT/HOMEASSISTANT/WHATSAPP) " "(T1=xxx) (T2=xxx) " "(TS=xxx) - display/set Notification settings", "[ESP620]URL=http://XXXXXX - send GET notification", diff --git a/esp3d/src/core/commands/ESP610.cpp b/esp3d/src/core/commands/ESP610.cpp index 84719396..09ab2768 100644 --- a/esp3d/src/core/commands/ESP610.cpp +++ b/esp3d/src/core/commands/ESP610.cpp @@ -42,9 +42,9 @@ void ESP3DCommands::ESP610(int cmd_params_pos, ESP3DMessage* msg) { String tmpstr; const char* cmdList[] = {"type=", "T1=", "T2=", "TS="}; uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*); - const char* notificationStr[] = {"NONE", "PUSHOVER", "EMAIL", - "LINE", "TELEGRAM", "IFTTT", - "HOMEASSISTANT"}; + const char* notificationStr[] = {"NONE", "PUSHOVER", "EMAIL", + "LINE", "TELEGRAM", "IFTTT", + "HOMEASSISTANT", "WHATSAPP"}; uint8_t notificationStrSize = sizeof(notificationStr) / sizeof(char*); const ESP3DSettingIndex settingIndex[] = { ESP_NOTIFICATION_TYPE, ESP_NOTIFICATION_TOKEN1, ESP_NOTIFICATION_TOKEN2, diff --git a/esp3d/src/core/esp3d_string.cpp b/esp3d/src/core/esp3d_string.cpp index 81bb6266..02de1a96 100644 --- a/esp3d/src/core/esp3d_string.cpp +++ b/esp3d/src/core/esp3d_string.cpp @@ -169,6 +169,28 @@ const char* esp3d_string::encodeString(const char* s) { return tmp.c_str(); } +// Encode a string to be used in a URL +const char* esp3d_string::urlEncode(const char* s) { + static String encoded; + encoded = ""; + char temp[4]; + for (int i = 0; i < strlen(s); i++) { + temp[0] =s[i]; + if (temp[0] == 32) { //space + encoded.concat('+'); + } else if ((temp[0] >= 48 && temp[0] <= 57) /*0-9*/ + || (temp[0] >= 65 && temp[0] <= 90) /*A-Z*/ + || (temp[0] >= 97 && temp[0] <= 122) /*a-z*/ + ) { + encoded.concat(temp[0]); + } else { //character needs encoding + snprintf(temp, 4, "%%%02X", temp[0]); + encoded.concat(temp); + } + } + return encoded.c_str(); +} + // helper to format size to readable string const char* esp3d_string::formatBytes(uint64_t bytes) { static String res; diff --git a/esp3d/src/core/esp3d_string.h b/esp3d/src/core/esp3d_string.h index 87e2241e..8f8e8403 100644 --- a/esp3d/src/core/esp3d_string.h +++ b/esp3d/src/core/esp3d_string.h @@ -30,6 +30,7 @@ const char* encodeString(const char* s); const char* formatBytes(uint64_t bytes); bool isPrintableChar(char c); const char* expandString(const char* s, bool formatspace = false); +const char * urlEncode(const char* s); } // namespace esp3d_string #endif //_ESP3D_STRING_H diff --git a/esp3d/src/include/esp3d_defines.h b/esp3d/src/include/esp3d_defines.h index 57ca31eb..612e7578 100644 --- a/esp3d/src/include/esp3d_defines.h +++ b/esp3d/src/include/esp3d_defines.h @@ -254,6 +254,7 @@ typedef uint ESP3DSettingIndex; #define ESP_TELEGRAM_NOTIFICATION 4 #define ESP_IFTTT_NOTIFICATION 5 #define ESP_HOMEASSISTANT_NOTIFICATION 6 +#define ESP_WHATS_APP_NOTIFICATION 7 // SENSOR #define NO_SENSOR_DEVICE 0 diff --git a/esp3d/src/include/esp3d_version.h b/esp3d/src/include/esp3d_version.h index 1a803f2f..a66b0992 100644 --- a/esp3d/src/include/esp3d_version.h +++ b/esp3d/src/include/esp3d_version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H // version and sources location -#define FW_VERSION "3.0.0.a242" +#define FW_VERSION "3.0.0.a243" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/notifications/notifications_service.cpp b/esp3d/src/modules/notifications/notifications_service.cpp index 5607b28c..41056368 100644 --- a/esp3d/src/modules/notifications/notifications_service.cpp +++ b/esp3d/src/modules/notifications/notifications_service.cpp @@ -74,6 +74,10 @@ extern "C" { #define LINESERVER "notify-api.line.me" #define LINEPORT 443 +#define WHATSAPPTIMEOUT 5000 +#define WHATSAPPSERVER "api.callmebot.com" +#define WHATSAPPPORT 443 + #define TELEGRAMTIMEOUT 5000 #define TELEGRAMSERVER "api.telegram.org" #define TELEGRAMPORT 443 @@ -166,6 +170,8 @@ const char* NotificationsService::getTypeString() { return "telegram"; case ESP_IFTTT_NOTIFICATION: return "IFTTT"; + case ESP_WHATS_APP_NOTIFICATION: + return "WhatsApp"; case ESP_HOMEASSISTANT_NOTIFICATION: return "HomeAssistant"; default: @@ -209,6 +215,9 @@ bool NotificationsService::sendMSG(const char* title, const char* messagetxt) { case ESP_IFTTT_NOTIFICATION: return sendIFTTTMSG(title, message.c_str()); break; + case ESP_WHATS_APP_NOTIFICATION: + return sendWhatsAppMSG(title, message.c_str()); + break; case ESP_HOMEASSISTANT_NOTIFICATION: return sendHomeAssistantMSG(title, message.c_str()); break; @@ -267,6 +276,48 @@ bool NotificationsService::sendPushoverMSG(const char* title, return res; } +// WhatsApp / CallMeBot +bool NotificationsService::sendWhatsAppMSG(const char* title, + const char* message) { + String data; + String geturl; + bool res; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" + WiFiClientSecure Notificationclient; +#pragma GCC diagnostic pop + Notificationclient.setInsecure(); +#if defined(ARDUINO_ARCH_ESP8266) + BearSSLSetup(Notificationclient); +#endif // ARDUINO_ARCH_ESP8266 + if (!Notificationclient.connect(_serveraddress.c_str(), _port)) { + esp3d_log_e("Error connecting server %s:%d", _serveraddress.c_str(), + _port); + return false; + } + // build data for post + data = "/whatsapp.php?phone="; + data += _token1; + data += "&apikey="; + data += _token2; + data += "&text="; + data += esp3d_string::urlEncode(message); + // build get query because it only accept GET + geturl = + "GET https://" + _serveraddress; + geturl+= data; + geturl+=" HTTP/1.0"; + Notificationclient.println(geturl.c_str()); + Notificationclient.println("Host: api.callmebot.com"); + Notificationclient.println("Connection: close"); + Notificationclient.println(); + esp3d_log("Query: %s", geturl.c_str()); + // send query + res = Wait4Answer(Notificationclient, "", "Message queued.", WHATSAPPTIMEOUT); + Notificationclient.stop(); + return res; +} + // Telegram // TODO: put error in variable to allow better error handling bool NotificationsService::sendTelegramMSG(const char* title, @@ -637,6 +688,12 @@ bool NotificationsService::begin() { _port = LINEPORT; _serveraddress = LINESERVER; break; + case ESP_WHATS_APP_NOTIFICATION: + _token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1); + _token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2); + _port = WHATSAPPPORT; + _serveraddress = WHATSAPPSERVER; + break; case ESP_IFTTT_NOTIFICATION: _token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1); _token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2); diff --git a/esp3d/src/modules/notifications/notifications_service.h b/esp3d/src/modules/notifications/notifications_service.h index 0e74708e..bbdfb216 100644 --- a/esp3d/src/modules/notifications/notifications_service.h +++ b/esp3d/src/modules/notifications/notifications_service.h @@ -54,6 +54,7 @@ class NotificationsService { 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 sendWhatsAppMSG(const char* title, const char* message); bool sendTelegramMSG(const char* title, const char* message); bool sendIFTTTMSG(const char* title, const char* message); bool sendHomeAssistantMSG(const char* title, const char* message); diff --git a/esp3d/src/modules/update/update_service.cpp b/esp3d/src/modules/update/update_service.cpp index fc6a0e6f..b46d7f07 100644 --- a/esp3d/src/modules/update/update_service.cpp +++ b/esp3d/src/modules/update/update_service.cpp @@ -330,7 +330,7 @@ bool processingFileFunction(const char* section, const char* key, } } // Notification type None / PushOver / Line / Email / Telegram / IFTTT / - // HomeAssistant + // HomeAssistant / WhatsApp if (!done) { if (strcasecmp("NOTIF_TYPE", key) == 0) { T = 'B'; @@ -348,7 +348,9 @@ bool processingFileFunction(const char* section, const char* key, b = ESP_TELEGRAM_NOTIFICATION; } else if (strcasecmp("IFTTT", value) == 0) { b = ESP_IFTTT_NOTIFICATION; - } else if (strcasecmp("HOMEASSISTANT", value) == 0) { + } else if (strcasecmp("WhatsApp", value) == 0) { + b = ESP_WHATS_APP_NOTIFICATION; + }else if (strcasecmp("HOMEASSISTANT", value) == 0) { b = ESP_HOMEASSISTANT_NOTIFICATION; } else { P = -1; // invalid value