Add telegram support per @fjriosp request

fixes #439
This commit is contained in:
Luc 2020-04-14 11:31:36 +02:00
parent 248162d960
commit e525bcb6c2
6 changed files with 101 additions and 2 deletions

View File

@ -351,6 +351,8 @@ bool Commands::ESP400(const char* cmd_params, level_authenticate_type auth_type,
output->print (ESP_EMAIL_NOTIFICATION); output->print (ESP_EMAIL_NOTIFICATION);
output->print ("\"},{\"line\":\""); output->print ("\"},{\"line\":\"");
output->print (ESP_LINE_NOTIFICATION); output->print (ESP_LINE_NOTIFICATION);
output->print ("\"},{\"telegram\":\"");
output->print (ESP_TELEGRAM_NOTIFICATION);
output->print ("\"}]}"); output->print ("\"}]}");
//Token 1 //Token 1
output->print (",{\"F\":\"service\",\"F2\":\"notification\",\"P\":\""); output->print (",{\"F\":\"service\",\"F2\":\"notification\",\"P\":\"");

View File

@ -44,7 +44,7 @@ bool Commands::ESP610(const char* cmd_params, level_authenticate_type auth_type,
if (parameter.length() == 0) { if (parameter.length() == 0) {
uint8_t Ntype = Settings_ESP3D::read_byte(ESP_NOTIFICATION_TYPE); uint8_t Ntype = Settings_ESP3D::read_byte(ESP_NOTIFICATION_TYPE);
static String tmp; 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+= " ";
tmp+= Settings_ESP3D::read_string(ESP_NOTIFICATION_SETTINGS); tmp+= Settings_ESP3D::read_string(ESP_NOTIFICATION_SETTINGS);
output->printMSG (tmp.c_str()); 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; Ntype = ESP_EMAIL_NOTIFICATION;
} else if (parameter == "LINE") { } else if (parameter == "LINE") {
Ntype = ESP_LINE_NOTIFICATION; Ntype = ESP_LINE_NOTIFICATION;
} else if (parameter == "TELEGRAM") {
Ntype = ESP_TELEGRAM_NOTIFICATION;
} else { } else {
output->printERROR("Only NONE, PUSHOVER, EMAIL, LINE are supported!"); output->printERROR("Only NONE, PUSHOVER, EMAIL, LINE are supported!");
return false; return false;

View File

@ -85,6 +85,7 @@
#define ESP_PUSHOVER_NOTIFICATION 1 #define ESP_PUSHOVER_NOTIFICATION 1
#define ESP_EMAIL_NOTIFICATION 2 #define ESP_EMAIL_NOTIFICATION 2
#define ESP_LINE_NOTIFICATION 3 #define ESP_LINE_NOTIFICATION 3
#define ESP_TELEGRAM_NOTIFICATION 4
//DHT //DHT
#define NO_DHT_DEVICE 0 #define NO_DHT_DEVICE 0

View File

@ -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.a31" #define FW_VERSION "3.0.0.a32"
#define REPOSITORY "https://github.com/luc-github/ESP3D" #define REPOSITORY "https://github.com/luc-github/ESP3D"
#endif //_VERSION_ESP3D_H #endif //_VERSION_ESP3D_H

View File

@ -27,6 +27,8 @@
//* Email: //* Email:
// - https://github.com/CosmicBoris/ESP8266SMTP // - https://github.com/CosmicBoris/ESP8266SMTP
// - https://www.electronicshub.org/send-an-email-using-esp8266/ // - 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" #include "../../include/esp3d_config.h"
#ifdef NOTIFICATION_FEATURE #ifdef NOTIFICATION_FEATURE
@ -62,10 +64,50 @@ typedef WiFiClientSecure TSecureClient;
#define LINESERVER "notify-api.line.me" #define LINESERVER "notify-api.line.me"
#define LINEPORT 443 #define LINEPORT 443
#define TELEGRAMTIMEOUT 5000
#define TELEGRAMSERVER "api.telegram.org"
#define TELEGRAMPORT 443
#define EMAILTIMEOUT 5000 #define EMAILTIMEOUT 5000
NotificationsService notificationsservice; 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) bool Wait4Answer(TSecureClient & client, const char * linetrigger, const char * expected_answer, uint32_t timeout)
{ {
if(client.connected()) { if(client.connected()) {
@ -143,6 +185,8 @@ const char * NotificationsService::getTypeString()
return "email"; return "email";
case ESP_LINE_NOTIFICATION: case ESP_LINE_NOTIFICATION:
return "line"; return "line";
case ESP_TELEGRAM_NOTIFICATION:
return "telegram";
default: default:
break; break;
} }
@ -152,6 +196,7 @@ const char * NotificationsService::getTypeString()
bool NotificationsService::sendMSG(const char * title, const char * message) bool NotificationsService::sendMSG(const char * title, const char * message)
{ {
if(!_started) { if(!_started) {
log_esp3d("Error notification not started");
return false; return false;
} }
if (!((strlen(title) == 0) && (strlen(message) == 0))) { if (!((strlen(title) == 0) && (strlen(message) == 0))) {
@ -165,6 +210,9 @@ bool NotificationsService::sendMSG(const char * title, const char * message)
case ESP_LINE_NOTIFICATION : case ESP_LINE_NOTIFICATION :
return sendLineMSG(title,message); return sendLineMSG(title,message);
break; break;
case ESP_TELEGRAM_NOTIFICATION :
return sendTelegramMSG(title,message);
break;
default: default:
break; break;
} }
@ -212,6 +260,45 @@ bool NotificationsService::sendPushoverMSG(const char * title, const char * mess
Notificationclient.stop(); Notificationclient.stop();
return res; 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) bool NotificationsService::sendEmailMSG(const char * title, const char * message)
{ {
#pragma GCC diagnostic push #pragma GCC diagnostic push
@ -400,6 +487,12 @@ bool NotificationsService::begin()
_port = PUSHOVERPORT; _port = PUSHOVERPORT;
_serveraddress = PUSHOVERSERVER; _serveraddress = PUSHOVERSERVER;
break; 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: case ESP_LINE_NOTIFICATION:
_token1 = Settings_ESP3D::read_string(ESP_NOTIFICATION_TOKEN1); _token1 = Settings_ESP3D::read_string(ESP_NOTIFICATION_TOKEN1);
_port = LINEPORT; _port = LINEPORT;

View File

@ -56,6 +56,7 @@ private:
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 sendTelegramMSG(const char * title, const char * message);
bool getPortFromSettings(); bool getPortFromSettings();
bool getServerAddressFromSettings(); bool getServerAddressFromSettings();
bool getEmailFromSettings(); bool getEmailFromSettings();