This commit is contained in:
Luc 2024-11-25 15:45:09 +08:00
parent d2c52ac719
commit d9f6ea6172
10 changed files with 92 additions and 8 deletions

View File

@ -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

View File

@ -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",

View File

@ -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,

View File

@ -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;

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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, "<b>", "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);

View File

@ -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);

View File

@ -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