add formated sending message

This commit is contained in:
Clement 2023-08-11 21:38:00 +02:00
parent 46d3edef1d
commit 0762c38c47
2 changed files with 29 additions and 3 deletions

View File

@ -15,7 +15,24 @@ public:
*/ */
DiscordAPI(String hookUrl); DiscordAPI(String hookUrl);
bool sendMessage(String message); /**
* @brief send a message to the discord webhook
* @param trame trame a envoyer
* @return true la trame est bien envoyée
* @return false la trame n'est pas envoyée
*/
bool sendMessage(String trame);
/**
* @brief envoie un embed discord avec les horaires d'ouverture et de fermeture du lab
*
* @param hStart heure d'ouverture
* @param hEnd heure de fermeture
* @return true la trame est bien envoyée
* @return false la trame n'est pas envoyée
*/
bool sendheure(String hStart, String hEnd);
private: private:
/** /**

View File

@ -6,9 +6,11 @@ DiscordAPI::DiscordAPI(String hookUrl){
this->httpClient = new HTTPClient(); this->httpClient = new HTTPClient();
this->wifiClient = new BearSSL::WiFiClientSecure; this->wifiClient = new BearSSL::WiFiClientSecure;
this->wifiClient->setInsecure(); this->wifiClient->setInsecure();
randomSeed(analogRead(A0));
} }
bool DiscordAPI::sendMessage(String message){ bool DiscordAPI::sendMessage(String trame){
bool sortie = true; bool sortie = true;
if(WiFi.status() != WL_CONNECTED){ if(WiFi.status() != WL_CONNECTED){
@ -18,7 +20,7 @@ bool DiscordAPI::sendMessage(String message){
this->httpClient->begin(*this->wifiClient, this->hookUrl); this->httpClient->begin(*this->wifiClient, this->hookUrl);
this->httpClient->addHeader("Content-Type", "application/json"); this->httpClient->addHeader("Content-Type", "application/json");
int resp = this->httpClient->POST("{\"content\":\""+ message +"\"}"); int resp = this->httpClient->POST(trame);
if(resp != 204){ if(resp != 204){
sortie = false; sortie = false;
Serial.print("sending message error code : "); Serial.print("sending message error code : ");
@ -27,3 +29,10 @@ bool DiscordAPI::sendMessage(String message){
this->httpClient->end(); this->httpClient->end();
return sortie; return sortie;
} }
bool DiscordAPI::sendheure(String hStart, String hEnd){
int color = random(16777215);
String trame = "{\"embeds\": [{\"title\": \"Le Lab est ouvert !\",\"description\": \"Le Lab est ouvert de **"+ hStart +"** à **"+ hEnd +"**\",\"color\": \"" + color + "\"}]}";
return this->sendMessage(trame);
}