Skip to content

Commit d9f6ea6

Browse files
committed
close #888
1 parent d2c52ac commit d9f6ea6

File tree

10 files changed

+92
-8
lines changed

10 files changed

+92
-8
lines changed

docs/esp3dcnf.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ FTP_Passive_Port = 55600
108108
#Auto notification
109109
AUTONOTIFICATION = Yes
110110

111-
#Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant
111+
#Notification type None / PushOver / Line / Email / Telegram / IFTTT / HomeAssistant / WhatsApp
112112
NOTIF_TYPE = None
113113

114114
#Notification token 1 string of 64 chars max

esp3d/src/core/commands/ESP0.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ const char* help[] = {
143143
#endif // AUTHENTICATION_FEATURE
144144
#if defined(NOTIFICATION_FEATURE)
145145
"[ESP600](message) - send notification",
146-
"[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT/HOMEASSISTANT) "
146+
"[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE/TELEGRAM/IFTTT/HOMEASSISTANT/WHATSAPP) "
147147
"(T1=xxx) (T2=xxx) "
148148
"(TS=xxx) - display/set Notification settings",
149149
"[ESP620]URL=http://XXXXXX - send GET notification",

esp3d/src/core/commands/ESP610.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ void ESP3DCommands::ESP610(int cmd_params_pos, ESP3DMessage* msg) {
4242
String tmpstr;
4343
const char* cmdList[] = {"type=", "T1=", "T2=", "TS="};
4444
uint8_t cmdListSize = sizeof(cmdList) / sizeof(char*);
45-
const char* notificationStr[] = {"NONE", "PUSHOVER", "EMAIL",
46-
"LINE", "TELEGRAM", "IFTTT",
47-
"HOMEASSISTANT"};
45+
const char* notificationStr[] = {"NONE", "PUSHOVER", "EMAIL",
46+
"LINE", "TELEGRAM", "IFTTT",
47+
"HOMEASSISTANT", "WHATSAPP"};
4848
uint8_t notificationStrSize = sizeof(notificationStr) / sizeof(char*);
4949
const ESP3DSettingIndex settingIndex[] = {
5050
ESP_NOTIFICATION_TYPE, ESP_NOTIFICATION_TOKEN1, ESP_NOTIFICATION_TOKEN2,

esp3d/src/core/esp3d_string.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ const char* esp3d_string::encodeString(const char* s) {
169169
return tmp.c_str();
170170
}
171171

172+
// Encode a string to be used in a URL
173+
const char* esp3d_string::urlEncode(const char* s) {
174+
static String encoded;
175+
encoded = "";
176+
char temp[4];
177+
for (int i = 0; i < strlen(s); i++) {
178+
temp[0] =s[i];
179+
if (temp[0] == 32) { //space
180+
encoded.concat('+');
181+
} else if ((temp[0] >= 48 && temp[0] <= 57) /*0-9*/
182+
|| (temp[0] >= 65 && temp[0] <= 90) /*A-Z*/
183+
|| (temp[0] >= 97 && temp[0] <= 122) /*a-z*/
184+
) {
185+
encoded.concat(temp[0]);
186+
} else { //character needs encoding
187+
snprintf(temp, 4, "%%%02X", temp[0]);
188+
encoded.concat(temp);
189+
}
190+
}
191+
return encoded.c_str();
192+
}
193+
172194
// helper to format size to readable string
173195
const char* esp3d_string::formatBytes(uint64_t bytes) {
174196
static String res;

esp3d/src/core/esp3d_string.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const char* encodeString(const char* s);
3030
const char* formatBytes(uint64_t bytes);
3131
bool isPrintableChar(char c);
3232
const char* expandString(const char* s, bool formatspace = false);
33+
const char * urlEncode(const char* s);
3334
} // namespace esp3d_string
3435

3536
#endif //_ESP3D_STRING_H

esp3d/src/include/esp3d_defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ typedef uint ESP3DSettingIndex;
254254
#define ESP_TELEGRAM_NOTIFICATION 4
255255
#define ESP_IFTTT_NOTIFICATION 5
256256
#define ESP_HOMEASSISTANT_NOTIFICATION 6
257+
#define ESP_WHATS_APP_NOTIFICATION 7
257258

258259
// SENSOR
259260
#define NO_SENSOR_DEVICE 0

esp3d/src/include/esp3d_version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#define _VERSION_ESP3D_H
2323

2424
// version and sources location
25-
#define FW_VERSION "3.0.0.a242"
25+
#define FW_VERSION "3.0.0.a243"
2626
#define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0"
2727

2828
#endif //_VERSION_ESP3D_H

esp3d/src/modules/notifications/notifications_service.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ extern "C" {
7474
#define LINESERVER "notify-api.line.me"
7575
#define LINEPORT 443
7676

77+
#define WHATSAPPTIMEOUT 5000
78+
#define WHATSAPPSERVER "api.callmebot.com"
79+
#define WHATSAPPPORT 443
80+
7781
#define TELEGRAMTIMEOUT 5000
7882
#define TELEGRAMSERVER "api.telegram.org"
7983
#define TELEGRAMPORT 443
@@ -166,6 +170,8 @@ const char* NotificationsService::getTypeString() {
166170
return "telegram";
167171
case ESP_IFTTT_NOTIFICATION:
168172
return "IFTTT";
173+
case ESP_WHATS_APP_NOTIFICATION:
174+
return "WhatsApp";
169175
case ESP_HOMEASSISTANT_NOTIFICATION:
170176
return "HomeAssistant";
171177
default:
@@ -209,6 +215,9 @@ bool NotificationsService::sendMSG(const char* title, const char* messagetxt) {
209215
case ESP_IFTTT_NOTIFICATION:
210216
return sendIFTTTMSG(title, message.c_str());
211217
break;
218+
case ESP_WHATS_APP_NOTIFICATION:
219+
return sendWhatsAppMSG(title, message.c_str());
220+
break;
212221
case ESP_HOMEASSISTANT_NOTIFICATION:
213222
return sendHomeAssistantMSG(title, message.c_str());
214223
break;
@@ -267,6 +276,48 @@ bool NotificationsService::sendPushoverMSG(const char* title,
267276
return res;
268277
}
269278

279+
// WhatsApp / CallMeBot
280+
bool NotificationsService::sendWhatsAppMSG(const char* title,
281+
const char* message) {
282+
String data;
283+
String geturl;
284+
bool res;
285+
#pragma GCC diagnostic push
286+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
287+
WiFiClientSecure Notificationclient;
288+
#pragma GCC diagnostic pop
289+
Notificationclient.setInsecure();
290+
#if defined(ARDUINO_ARCH_ESP8266)
291+
BearSSLSetup(Notificationclient);
292+
#endif // ARDUINO_ARCH_ESP8266
293+
if (!Notificationclient.connect(_serveraddress.c_str(), _port)) {
294+
esp3d_log_e("Error connecting server %s:%d", _serveraddress.c_str(),
295+
_port);
296+
return false;
297+
}
298+
// build data for post
299+
data = "/whatsapp.php?phone=";
300+
data += _token1;
301+
data += "&apikey=";
302+
data += _token2;
303+
data += "&text=";
304+
data += esp3d_string::urlEncode(message);
305+
// build get query because it only accept GET
306+
geturl =
307+
"GET https://" + _serveraddress;
308+
geturl+= data;
309+
geturl+=" HTTP/1.0";
310+
Notificationclient.println(geturl.c_str());
311+
Notificationclient.println("Host: api.callmebot.com");
312+
Notificationclient.println("Connection: close");
313+
Notificationclient.println();
314+
esp3d_log("Query: %s", geturl.c_str());
315+
// send query
316+
res = Wait4Answer(Notificationclient, "<b>", "Message queued.", WHATSAPPTIMEOUT);
317+
Notificationclient.stop();
318+
return res;
319+
}
320+
270321
// Telegram
271322
// TODO: put error in variable to allow better error handling
272323
bool NotificationsService::sendTelegramMSG(const char* title,
@@ -637,6 +688,12 @@ bool NotificationsService::begin() {
637688
_port = LINEPORT;
638689
_serveraddress = LINESERVER;
639690
break;
691+
case ESP_WHATS_APP_NOTIFICATION:
692+
_token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1);
693+
_token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2);
694+
_port = WHATSAPPPORT;
695+
_serveraddress = WHATSAPPSERVER;
696+
break;
640697
case ESP_IFTTT_NOTIFICATION:
641698
_token1 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN1);
642699
_token2 = ESP3DSettings::readString(ESP_NOTIFICATION_TOKEN2);

esp3d/src/modules/notifications/notifications_service.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class NotificationsService {
5454
bool sendPushoverMSG(const char* title, const char* message);
5555
bool sendEmailMSG(const char* title, const char* message);
5656
bool sendLineMSG(const char* title, const char* message);
57+
bool sendWhatsAppMSG(const char* title, const char* message);
5758
bool sendTelegramMSG(const char* title, const char* message);
5859
bool sendIFTTTMSG(const char* title, const char* message);
5960
bool sendHomeAssistantMSG(const char* title, const char* message);

esp3d/src/modules/update/update_service.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ bool processingFileFunction(const char* section, const char* key,
330330
}
331331
}
332332
// Notification type None / PushOver / Line / Email / Telegram / IFTTT /
333-
// HomeAssistant
333+
// HomeAssistant / WhatsApp
334334
if (!done) {
335335
if (strcasecmp("NOTIF_TYPE", key) == 0) {
336336
T = 'B';
@@ -348,7 +348,9 @@ bool processingFileFunction(const char* section, const char* key,
348348
b = ESP_TELEGRAM_NOTIFICATION;
349349
} else if (strcasecmp("IFTTT", value) == 0) {
350350
b = ESP_IFTTT_NOTIFICATION;
351-
} else if (strcasecmp("HOMEASSISTANT", value) == 0) {
351+
} else if (strcasecmp("WhatsApp", value) == 0) {
352+
b = ESP_WHATS_APP_NOTIFICATION;
353+
}else if (strcasecmp("HOMEASSISTANT", value) == 0) {
352354
b = ESP_HOMEASSISTANT_NOTIFICATION;
353355
} else {
354356
P = -1; // invalid value

0 commit comments

Comments
 (0)