Browse Source

Remove useless @mention notification API

master
Lomanic 4 months ago
parent
commit
4c74d44d02
1 changed files with 6 additions and 75 deletions
  1. 6
    75
      main.ino

+ 6
- 75
main.ino View File

@@ -135,68 +135,6 @@ bool sendMessage(String roomId, String message) {
135 135
   }
136 136
   return success;
137 137
 }
138
-
139
-bool mentionedOnMatrix = false;
140
-bool getMessages(String roomId) {
141
-  bool success = false;
142
-
143
-  String url = "http://corsanywhere.glitch.me/https://" + roomId.substring(roomId.indexOf(":") + 1) + "/_matrix/client/r0/rooms/" + roomId + "/messages?access_token=" + accessToken + "&limit=1";
144
-  if (lastMessageToken == "") {
145
-    url += "&dir=b";
146
-  } else {
147
-    url += "&dir=f&from=" + lastMessageToken;
148
-  }
149
-  Serial.printf("GET %s\n", url.c_str());
150
-
151
-  http.begin(url);
152
-  int rc = http.GET();
153
-  if (rc > 0) {
154
-    Serial.printf("%d\n", rc);
155
-    if (rc == HTTP_CODE_OK) {
156
-      String body = http.getString();
157
-      StaticJsonDocument<1000> jsonBuffer;
158
-      deserializeJson(jsonBuffer, body);
159
-      if (lastMessageToken != "") {
160
-        JsonArray chunks = jsonBuffer["chunk"];
161
-        JsonObject chunk = chunks[0];
162
-        String format = chunk["format"];
163
-        JsonObject content = chunk["content"];
164
-        if (content.containsKey("formatted_body")) {
165
-          String formatted_body = content["formatted_body"];
166
-          Serial.println(formatted_body);
167
-          if (formatted_body.indexOf("<a href=\"https://matrix.to/#/@" + matrixUsername + "\">" + matrixUsername.substring(0, matrixUsername.indexOf(":")) + "</a>") >= 0) {
168
-            mentionedOnMatrix = true;
169
-          }
170
-        }
171
-        if (content.containsKey("body")) {
172
-          String body = content["body"];
173
-          Serial.println(body);
174
-          if (body.indexOf(matrixUsername.substring(0, matrixUsername.indexOf(":")) + ":") == 0 || body.indexOf("@" + matrixUsername) >= 0) {
175
-            mentionedOnMatrix = true;
176
-          }
177
-        }
178
-        //read receipt
179
-        if (chunk.containsKey("event_id")) {
180
-          String event_id = chunk["event_id"];
181
-          String receiptUrl = "http://corsanywhere.glitch.me/https://" + roomId.substring(roomId.indexOf(":") + 1) + "/_matrix/client/r0/rooms/" + roomId + "/receipt/m.read/" + event_id + "?access_token=" + accessToken + "&limit=1";
182
-          http.begin(receiptUrl);
183
-          http.addHeader("Content-Type", "application/json");
184
-          http.POST("");
185
-          String receiptBody = http.getString();
186
-          Serial.println("Receipt " + receiptBody);
187
-        }
188
-      }
189
-      String myLastMessageToken = jsonBuffer["end"];
190
-      lastMessageToken = String(myLastMessageToken.c_str());
191
-      //Serial.println(lastMessageToken);
192
-      success = true;
193
-    }
194
-  } else {
195
-    Serial.printf("Error: %s\n", http.errorToString(rc).c_str());
196
-  }
197
-
198
-  return success;
199
-}
200 138
 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201 139
 
202 140
 ESP8266WebServer httpServer(80); // webserver on port 80 https://github.com/esp8266/Arduino/blob/14262af0d19a9a3b992d5aa310a684d47b6fb876/libraries/ESP8266WebServer/examples/AdvancedWebServer/AdvancedWebServer.ino
@@ -522,25 +460,18 @@ void loop() {
522 460
     ESP.reset();
523 461
   }
524 462
 
525
-  if (!loggedInMatrix) { // send SOS in morse
526
-    morseSOSLED();
527
-    loggedInMatrix = login(matrixUsername, matrixPassword);
528
-    return;
529
-  }
530 463
   unsigned long currentMillis = millis();
531 464
   if (currentMillis - previousMillis > getMatrixMessagesInterval) {
532 465
     previousMillis = currentMillis;
533
-    if (!getMessages(matrixRoom)) {
534
-      morseSOSLED();
535
-      return;
536
-    }
537
-    if (mentionedOnMatrix) {
538
-      digitalWrite(RELAY_PIN, HIGH);
539
-      mentionedOnMatrix = false;
540
-    }
541 466
     notifyFuzIsOpen();
542 467
   }
543 468
 
469
+  if (!loggedInMatrix) { // send SOS in morse
470
+    morseSOSLED();
471
+    loggedInMatrix = login(matrixUsername, matrixPassword);
472
+    return;
473
+  }
474
+
544 475
   bool relayState = digitalRead(RELAY_PIN);
545 476
 
546 477
   if (buttonState == LOW && previousButtonState == HIGH && relayState == HIGH && sendMessage(matrixRoom, matrixMessage)) { // button just pressed while light is up

Loading…
Cancel
Save