mirror of
				https://github.com/luc-github/ESP3D.git
				synced 2025-10-24 11:50:52 -07:00 
			
		
		
		
	close #888
This commit is contained in:
		| @@ -108,7 +108,7 @@ FTP_Passive_Port = 55600 | |||||||
| #Auto notification | #Auto notification | ||||||
| AUTONOTIFICATION = Yes | AUTONOTIFICATION = Yes | ||||||
|  |  | ||||||
| #Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant | #Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant / WhatsApp | ||||||
| NOTIF_TYPE = None  | NOTIF_TYPE = None  | ||||||
|  |  | ||||||
| #Notification token 1 string of 64 chars max | #Notification token 1 string of 64 chars max | ||||||
|   | |||||||
| @@ -143,7 +143,7 @@ const char* help[] = { | |||||||
| #endif  // AUTHENTICATION_FEATURE | #endif  // AUTHENTICATION_FEATURE | ||||||
| #if defined(NOTIFICATION_FEATURE) | #if defined(NOTIFICATION_FEATURE) | ||||||
|     "[ESP600](message) - send notification", |     "[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) " |     "(T1=xxx) (T2=xxx) " | ||||||
|     "(TS=xxx) - display/set Notification settings", |     "(TS=xxx) - display/set Notification settings", | ||||||
|     "[ESP620]URL=http://XXXXXX  - send GET notification", |     "[ESP620]URL=http://XXXXXX  - send GET notification", | ||||||
|   | |||||||
| @@ -42,9 +42,9 @@ void ESP3DCommands::ESP610(int cmd_params_pos, ESP3DMessage* msg) { | |||||||
|   String tmpstr; |   String tmpstr; | ||||||
|   const char* cmdList[] = {"type=", "T1=", "T2=", "TS="}; |   const char* cmdList[] = {"type=", "T1=", "T2=", "TS="}; | ||||||
|   uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*); |   uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*); | ||||||
|   const char* notificationStr[] = {"NONE",         "PUSHOVER", "EMAIL", |   const char* notificationStr[] = {"NONE",          "PUSHOVER", "EMAIL", | ||||||
|                                    "LINE",         "TELEGRAM", "IFTTT", |                                    "LINE",          "TELEGRAM", "IFTTT", | ||||||
|                                    "HOMEASSISTANT"}; |                                    "HOMEASSISTANT", "WHATSAPP"}; | ||||||
|   uint8_t notificationStrSize = sizeof(notificationStr) / sizeof(char*); |   uint8_t notificationStrSize = sizeof(notificationStr) / sizeof(char*); | ||||||
|   const ESP3DSettingIndex settingIndex[] = { |   const ESP3DSettingIndex settingIndex[] = { | ||||||
|       ESP_NOTIFICATION_TYPE, ESP_NOTIFICATION_TOKEN1, ESP_NOTIFICATION_TOKEN2, |       ESP_NOTIFICATION_TYPE, ESP_NOTIFICATION_TOKEN1, ESP_NOTIFICATION_TOKEN2, | ||||||
|   | |||||||
| @@ -169,6 +169,28 @@ const char* esp3d_string::encodeString(const char* s) { | |||||||
|   return tmp.c_str(); |   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 | // helper to format size to readable string | ||||||
| const char* esp3d_string::formatBytes(uint64_t bytes) { | const char* esp3d_string::formatBytes(uint64_t bytes) { | ||||||
|   static String res; |   static String res; | ||||||
|   | |||||||
| @@ -30,6 +30,7 @@ const char* encodeString(const char* s); | |||||||
| const char* formatBytes(uint64_t bytes); | const char* formatBytes(uint64_t bytes); | ||||||
| bool isPrintableChar(char c); | bool isPrintableChar(char c); | ||||||
| const char* expandString(const char* s, bool formatspace = false); | const char* expandString(const char* s, bool formatspace = false); | ||||||
|  | const char * urlEncode(const char* s); | ||||||
| }  // namespace esp3d_string | }  // namespace esp3d_string | ||||||
|  |  | ||||||
| #endif  //_ESP3D_STRING_H | #endif  //_ESP3D_STRING_H | ||||||
|   | |||||||
| @@ -254,6 +254,7 @@ typedef uint ESP3DSettingIndex; | |||||||
| #define ESP_TELEGRAM_NOTIFICATION 4 | #define ESP_TELEGRAM_NOTIFICATION 4 | ||||||
| #define ESP_IFTTT_NOTIFICATION 5 | #define ESP_IFTTT_NOTIFICATION 5 | ||||||
| #define ESP_HOMEASSISTANT_NOTIFICATION 6 | #define ESP_HOMEASSISTANT_NOTIFICATION 6 | ||||||
|  | #define ESP_WHATS_APP_NOTIFICATION 7 | ||||||
|  |  | ||||||
| // SENSOR | // SENSOR | ||||||
| #define NO_SENSOR_DEVICE 0 | #define NO_SENSOR_DEVICE 0 | ||||||
|   | |||||||
| @@ -22,7 +22,7 @@ | |||||||
| #define _VERSION_ESP3D_H | #define _VERSION_ESP3D_H | ||||||
|  |  | ||||||
| // version and sources location | // 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" | #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" | ||||||
|  |  | ||||||
| #endif  //_VERSION_ESP3D_H | #endif  //_VERSION_ESP3D_H | ||||||
|   | |||||||
| @@ -74,6 +74,10 @@ extern "C" { | |||||||
| #define LINESERVER "notify-api.line.me" | #define LINESERVER "notify-api.line.me" | ||||||
| #define LINEPORT 443 | #define LINEPORT 443 | ||||||
|  |  | ||||||
|  | #define WHATSAPPTIMEOUT 5000 | ||||||
|  | #define WHATSAPPSERVER "api.callmebot.com" | ||||||
|  | #define WHATSAPPPORT 443 | ||||||
|  |  | ||||||
| #define TELEGRAMTIMEOUT 5000 | #define TELEGRAMTIMEOUT 5000 | ||||||
| #define TELEGRAMSERVER "api.telegram.org" | #define TELEGRAMSERVER "api.telegram.org" | ||||||
| #define TELEGRAMPORT 443 | #define TELEGRAMPORT 443 | ||||||
| @@ -166,6 +170,8 @@ const char* NotificationsService::getTypeString() { | |||||||
|       return "telegram"; |       return "telegram"; | ||||||
|     case ESP_IFTTT_NOTIFICATION: |     case ESP_IFTTT_NOTIFICATION: | ||||||
|       return "IFTTT"; |       return "IFTTT"; | ||||||
|  |     case ESP_WHATS_APP_NOTIFICATION: | ||||||
|  |       return "WhatsApp"; | ||||||
|     case ESP_HOMEASSISTANT_NOTIFICATION: |     case ESP_HOMEASSISTANT_NOTIFICATION: | ||||||
|       return "HomeAssistant"; |       return "HomeAssistant"; | ||||||
|     default: |     default: | ||||||
| @@ -209,6 +215,9 @@ bool NotificationsService::sendMSG(const char* title, const char* messagetxt) { | |||||||
|       case ESP_IFTTT_NOTIFICATION: |       case ESP_IFTTT_NOTIFICATION: | ||||||
|         return sendIFTTTMSG(title, message.c_str()); |         return sendIFTTTMSG(title, message.c_str()); | ||||||
|         break; |         break; | ||||||
|  |       case ESP_WHATS_APP_NOTIFICATION: | ||||||
|  |         return sendWhatsAppMSG(title, message.c_str()); | ||||||
|  |         break; | ||||||
|       case ESP_HOMEASSISTANT_NOTIFICATION: |       case ESP_HOMEASSISTANT_NOTIFICATION: | ||||||
|         return sendHomeAssistantMSG(title, message.c_str()); |         return sendHomeAssistantMSG(title, message.c_str()); | ||||||
|         break; |         break; | ||||||
| @@ -267,6 +276,48 @@ bool NotificationsService::sendPushoverMSG(const char* title, | |||||||
|   return res; |   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, "<b>", "Message queued.", WHATSAPPTIMEOUT); | ||||||
|  |   Notificationclient.stop(); | ||||||
|  |   return res; | ||||||
|  | } | ||||||
|  |  | ||||||
| // Telegram | // Telegram | ||||||
| // TODO: put error in variable to allow better error handling | // TODO: put error in variable to allow better error handling | ||||||
| bool NotificationsService::sendTelegramMSG(const char* title, | bool NotificationsService::sendTelegramMSG(const char* title, | ||||||
| @@ -637,6 +688,12 @@ bool NotificationsService::begin() { | |||||||
|       _port = LINEPORT; |       _port = LINEPORT; | ||||||
|       _serveraddress = LINESERVER; |       _serveraddress = LINESERVER; | ||||||
|       break; |       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: |     case ESP_IFTTT_NOTIFICATION: | ||||||
|       _token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1); |       _token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1); | ||||||
|       _token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2); |       _token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2); | ||||||
|   | |||||||
| @@ -54,6 +54,7 @@ class NotificationsService { | |||||||
|   bool sendPushoverMSG(const char* title, const char* message); |   bool sendPushoverMSG(const char* title, const char* message); | ||||||
|   bool sendEmailMSG(const char* title, const char* message); |   bool sendEmailMSG(const char* title, const char* message); | ||||||
|   bool sendLineMSG(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 sendTelegramMSG(const char* title, const char* message); | ||||||
|   bool sendIFTTTMSG(const char* title, const char* message); |   bool sendIFTTTMSG(const char* title, const char* message); | ||||||
|   bool sendHomeAssistantMSG(const char* title, const char* message); |   bool sendHomeAssistantMSG(const char* title, const char* message); | ||||||
|   | |||||||
| @@ -330,7 +330,7 @@ bool processingFileFunction(const char* section, const char* key, | |||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     // Notification type None / PushOver / Line / Email / Telegram / IFTTT / |     // Notification type None / PushOver / Line / Email / Telegram / IFTTT / | ||||||
|     // HomeAssistant |     // HomeAssistant / WhatsApp | ||||||
|     if (!done) { |     if (!done) { | ||||||
|       if (strcasecmp("NOTIF_TYPE", key) == 0) { |       if (strcasecmp("NOTIF_TYPE", key) == 0) { | ||||||
|         T = 'B'; |         T = 'B'; | ||||||
| @@ -348,7 +348,9 @@ bool processingFileFunction(const char* section, const char* key, | |||||||
|           b = ESP_TELEGRAM_NOTIFICATION; |           b = ESP_TELEGRAM_NOTIFICATION; | ||||||
|         } else if (strcasecmp("IFTTT", value) == 0) { |         } else if (strcasecmp("IFTTT", value) == 0) { | ||||||
|           b = ESP_IFTTT_NOTIFICATION; |           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; |           b = ESP_HOMEASSISTANT_NOTIFICATION; | ||||||
|         } else { |         } else { | ||||||
|           P = -1;  // invalid value |           P = -1;  // invalid value | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user