Skip to content

Commit 17bc1f9

Browse files
committed
Network,Https and sanitizeInput
1 parent 06d0890 commit 17bc1f9

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

src/AIChatbot.cpp

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,34 @@ void AIChatbot::begin(long baudRate) {
88
}
99

1010
bool AIChatbot::connectWiFi(const char* ssid, const char* password, unsigned long timeoutMs) {
11+
Serial.printf("Connecting to WiFi: %s\n", ssid);
1112
WiFi.begin(ssid, password);
12-
unsigned long startAttemptTime = millis();
1313

14+
unsigned long startAttemptTime = millis();
1415
while (WiFi.status() != WL_CONNECTED && millis() - startAttemptTime < timeoutMs) {
15-
delay(1000);
16-
Serial.println("Connecting to WiFi...");
16+
delay(500);
17+
Serial.print(".");
1718
}
1819

1920
if (WiFi.status() == WL_CONNECTED) {
20-
Serial.println("Connected to WiFi");
21+
Serial.println("\nWiFi Connected!");
22+
Serial.printf("IP Address: %s\n", WiFi.localIP().toString().c_str());
2123
return true;
2224
} else {
23-
Serial.println("Failed to connect to WiFi");
25+
Serial.println("\nWiFi Connection Failed.");
26+
return false;
27+
}
28+
}
29+
30+
bool AIChatbot::validateKeys() {
31+
if (selectedAI == "chatgpt" && chatGPTApiKey.isEmpty()) {
32+
Serial.println("Error: ChatGPT API key is not set.");
33+
return false;
34+
} else if (selectedAI == "huggingface" && huggingFaceApiKey.isEmpty()) {
35+
Serial.println("Error: Hugging Face API key is not set.");
2436
return false;
2537
}
38+
return true;
2639
}
2740

2841
void AIChatbot::update() {
@@ -46,6 +59,15 @@ void AIChatbot::setKey(const String& key, const String& aiName) {
4659
void AIChatbot::selectAI(const String& aiName, const String& aiVersion) {
4760
selectedAI = aiName;
4861
selectedAIVersion = (aiVersion != "null" && aiVersion != "none") ? aiVersion : "gpt-3.5-turbo";
62+
63+
if (selectedAI == "chatgpt" && (selectedAIVersion != "gpt-3.5-turbo" && selectedAIVersion != "gpt-4")) {
64+
Serial.println("Warning: Unsupported ChatGPT version. Defaulting to gpt-3.5-turbo.");
65+
selectedAIVersion = "gpt-3.5-turbo";
66+
}
67+
if (selectedAI == "huggingface" && selectedAIVersion.isEmpty()) {
68+
Serial.println("Warning: Hugging Face version not set. Defaulting to the google/flan-t5-small.");
69+
selectedAIVersion = "google/flan-t5-small";
70+
}
4971
}
5072

5173
String AIChatbot::getResponse(const String& message) {
@@ -58,6 +80,24 @@ String AIChatbot::getResponse(const String& message) {
5880
}
5981
}
6082

83+
String AIChatbot::sanitizeInput(const String& input) {
84+
String sanitized = input;
85+
sanitized.replace("\\", "\\\\");
86+
sanitized.replace("\"", "\\\"");
87+
sanitized.replace("\n", "\\n");
88+
sanitized.replace("\r", "\\r");
89+
90+
for (int i = 0; i < sanitized.length(); i++) {
91+
if (sanitized[i] < 32) { // ASCII 32
92+
sanitized.remove(i, 1);
93+
i--;
94+
}
95+
}
96+
97+
return sanitized;
98+
}
99+
100+
61101
String AIChatbot::sendToChatGPT(const String& message) {
62102
if (chatGPTApiKey.isEmpty()) {
63103
return "ChatGPT API key not set.";
@@ -74,30 +114,34 @@ String AIChatbot::sendToHuggingFace(const String& message) {
74114
return "Hugging Face API key not set.";
75115
}
76116

117+
String sanitizedMessage = sanitizeInput(message);
77118
String url = "https://api-inference.huggingface.co/models/" + selectedAIVersion;
78-
String payload = "{\"inputs\": \"" + message + "\"}";
119+
String payload = "{\"inputs\": \"" + sanitizedMessage + "\"}";
79120

80121
return makeHttpRequest(url, payload, huggingFaceApiKey);
81122
}
82123

124+
83125
String AIChatbot::makeHttpRequest(const String& url, const String& payload, const String& apiKey) {
84126
HTTPClient http;
85-
86-
#ifdef ESP32
87127
WiFiClientSecure client;
88128
client.setInsecure();
89-
http.begin(client, url);
90-
#elif defined(ESP8266)
91-
WiFiClientSecure client;
92-
client.setInsecure();
93129
http.begin(client, url);
94-
#endif
95130

96131
http.addHeader("Content-Type", "application/json");
97132
http.addHeader("Authorization", "Bearer " + apiKey);
98133

99134
int httpResponseCode = http.POST(payload);
100-
String response = httpResponseCode > 0 ? http.getString() : "Error on sending POST: " + String(httpResponseCode);
135+
String response = "";
136+
137+
if (httpResponseCode > 0) {
138+
response = http.getString();
139+
Serial.printf("HTTP Response code: %d\nResponse body: %s\n", httpResponseCode, response.c_str());
140+
} else {
141+
response = "HTTP POST failed: " + String(http.errorToString(httpResponseCode).c_str());
142+
Serial.println(response);
143+
Serial.printf("Payload: %s\n", payload.c_str());
144+
}
101145

102146
http.end();
103147
return response;

src/AIChatbot.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33

44
#include <Arduino.h>
55

6-
#ifdef ESP32
6+
#if defined(ESP32)
77
#include <WiFi.h>
88
#include <WiFiClientSecure.h>
99
#include <HTTPClient.h>
10+
#define PLATFORM_NAME "ESP32"
1011
#elif defined(ESP8266)
1112
#include <ESP8266WiFi.h>
1213
#include <ESP8266HTTPClient.h>
14+
#define PLATFORM_NAME "ESP8266"
1315
#endif
1416

17+
1518
class AIChatbot {
1619
public:
1720
AIChatbot();
18-
void begin(long baudRate);
1921
bool connectWiFi(const char* ssid, const char* password, unsigned long timeoutMs = 10000);
22+
bool validateKeys();
23+
void begin(long baudRate);
2024
void update();
2125
void setKey(const String& key, const String& aiName);
2226
void selectAI(const String& aiName, const String& aiVersion = "gpt-3.5-turbo");
27+
String sanitizeInput(const String& input);
2328
String getResponse(const String& message);
2429

2530
private:

0 commit comments

Comments
 (0)