mirror of
https://git.mirrors.martin98.com/https://github.com/luc-github/ESP3D.git
synced 2025-08-06 08:06:03 +08:00
Use POST instead of GET for telegram notification
This commit is contained in:
parent
e525bcb6c2
commit
fd4e385830
@ -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.a32"
|
#define FW_VERSION "3.0.0.a33"
|
||||||
#define REPOSITORY "https://github.com/luc-github/ESP3D"
|
#define REPOSITORY "https://github.com/luc-github/ESP3D"
|
||||||
|
|
||||||
#endif //_VERSION_ESP3D_H
|
#endif //_VERSION_ESP3D_H
|
||||||
|
@ -72,42 +72,6 @@ typedef WiFiClientSecure TSecureClient;
|
|||||||
|
|
||||||
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()) {
|
||||||
@ -265,7 +229,7 @@ bool NotificationsService::sendPushoverMSG(const char * title, const char * mess
|
|||||||
bool NotificationsService::sendTelegramMSG(const char * title, const char * message)
|
bool NotificationsService::sendTelegramMSG(const char * title, const char * message)
|
||||||
{
|
{
|
||||||
String data;
|
String data;
|
||||||
String getcmd;
|
String postcmd;
|
||||||
bool res;
|
bool res;
|
||||||
#pragma GCC diagnostic push
|
#pragma GCC diagnostic push
|
||||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||||
@ -280,20 +244,21 @@ bool NotificationsService::sendTelegramMSG(const char * title, const char * mess
|
|||||||
}
|
}
|
||||||
(void)title;
|
(void)title;
|
||||||
//build url for get
|
//build url for get
|
||||||
data = "/bot";
|
data = "chat_id=";
|
||||||
data +=_token1;
|
|
||||||
data += "/sendMessage?chat_id=";
|
|
||||||
data += _token2;
|
data += _token2;
|
||||||
data += "&text=";
|
data += "&text=";
|
||||||
data += urlencode(message);
|
data += message;
|
||||||
|
|
||||||
//build post query
|
//build post query
|
||||||
getcmd = "GET ";
|
postcmd = "POST /bot";
|
||||||
getcmd += data;
|
postcmd +=_token1;
|
||||||
getcmd +=" HTTP/1.1\r\nHost: api.telegram.org\r\nConnection: close\r\n\r\n";
|
postcmd += "/sendMessage HTTP/1.1\r\nHost: api.telegram.org\r\nConnection: close\r\nContent-Type: application/x-www-form-urlencoded\r\nCache-Control: no-cache\r\nUser-Agent: ESP3D\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nContent-Length: ";
|
||||||
log_esp3d("Query: %s", getcmd.c_str());
|
postcmd += data.length();
|
||||||
|
postcmd +="\r\n\r\n";
|
||||||
|
postcmd +=data;
|
||||||
|
log_esp3d("Query: %s", postcmd.c_str());
|
||||||
//send query
|
//send query
|
||||||
Notificationclient.print(getcmd);
|
Notificationclient.print(postcmd);
|
||||||
res = Wait4Answer(Notificationclient, "{", "\"ok\":true", TELEGRAMTIMEOUT);
|
res = Wait4Answer(Notificationclient, "{", "\"ok\":true", TELEGRAMTIMEOUT);
|
||||||
Notificationclient.stop();
|
Notificationclient.stop();
|
||||||
return res;
|
return res;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user