@@ -7,13 +7,6 @@ function changeTheme() {
77 document . body . className = userPreferences . theme ;
88 localStorage . setItem ( "preferences" , JSON . stringify ( userPreferences ) ) ;
99}
10- // HUGGING FACE API
11- const response = await fetch ( "https://new-codriver.onrender.com" , {
12- method : "POST" ,
13- headers : { "Content-Type" : "application/json" } ,
14- body : JSON . stringify ( { message : input } )
15- } ) ;
16-
1710
1811// DOM Elements
1912const userInput = document . getElementById ( "userInput" ) ;
@@ -23,6 +16,7 @@ const micStatus = document.getElementById("micStatus");
2316// Constants
2417const WEATHER_API_KEY = "5b72dabcdd9d4b67a75170510250304" ;
2518const NYT_API_KEY = "N8j8mb3TSiexCbefuDeT1Ap5MgKPGwmw" ;
19+ const BACKEND_URL = "https://new-codriver.onrender.com/api/chat" ; // your Render backend
2620
2721let gameActive = false ;
2822let secretNumber = null ;
@@ -105,10 +99,7 @@ function getRandomQuote() {
10599
106100function getRandomFunFact ( ) {
107101 return [
108- "Honey never spoils!" ,
109- "Bananas are berries, but strawberries aren't!" ,
110- "Sharks existed before trees." ,
111- "Octopuses have three hearts."
102+ "Honey never spoils!" , "Bananas are berries, but strawberries aren't!" , "Sharks existed before trees." , "Octopuses have three hearts."
112103 ] [ Math . floor ( Math . random ( ) * 4 ) ] ;
113104}
114105
@@ -142,11 +133,15 @@ async function sendMessage() {
142133 const input = userInput . value . trim ( ) ;
143134 if ( ! input ) return ;
144135
136+ // Show user's message
145137 const userMessage = document . createElement ( "p" ) ;
146138 userMessage . textContent = "You: " + input ;
147139 userMessage . classList . add ( "user-message" ) ;
148140 messageArea . appendChild ( userMessage ) ;
149141
142+ let response = "" ;
143+
144+ // Handle games
150145 if ( gameActive ) {
151146 if ( / e x i t g a m e / i. test ( input ) ) {
152147 gameActive = false ;
@@ -168,39 +163,36 @@ async function sendMessage() {
168163 return ;
169164 }
170165
171- let response = "" ;
172-
173- if ( / h e l l o | h i | h e y | g r e e t i n g s / i. test ( input ) ) {
174- response = "Hello! How can I assist you today?" ;
175- } else if ( / q u o t e / i. test ( input ) ) {
176- response = getRandomQuote ( ) ;
177- } else if ( / w e a t h e r / i. test ( input ) ) {
166+ // Local responses
167+ if ( / h e l l o | h i | h e y | g r e e t i n g s / i. test ( input ) ) response = "Hello! How can I assist you today?" ;
168+ else if ( / q u o t e / i. test ( input ) ) response = getRandomQuote ( ) ;
169+ else if ( / w e a t h e r / i. test ( input ) ) {
178170 const city = input . split ( " " ) . pop ( ) ;
179171 response = await fetchWeather ( city ) ;
180- } else if ( / n e w s / i. test ( input ) ) {
181- response = await fetchNews ( ) ;
182- } else if ( / f u n f a c t / i. test ( input ) ) {
183- response = getRandomFunFact ( ) ;
184- } else if ( / j o k e / i. test ( input ) ) {
185- response = getRandomJoke ( ) ;
186- } else if ( / r i d d l e / i. test ( input ) ) {
187- response = getRandomRiddle ( ) ;
188- } else if ( / t i m e / i. test ( input ) ) {
189- response = getCurrentTime ( ) ;
190- } else if ( / g a m e / i. test ( input ) ) {
172+ }
173+ else if ( / n e w s / i. test ( input ) ) response = await fetchNews ( ) ;
174+ else if ( / f u n f a c t / i. test ( input ) ) response = getRandomFunFact ( ) ;
175+ else if ( / j o k e / i. test ( input ) ) response = getRandomJoke ( ) ;
176+ else if ( / r i d d l e / i. test ( input ) ) response = getRandomRiddle ( ) ;
177+ else if ( / t i m e / i. test ( input ) ) response = getCurrentTime ( ) ;
178+ else if ( / g a m e / i. test ( input ) ) {
191179 gameActive = true ;
192180 secretNumber = Math . floor ( Math . random ( ) * 10 ) + 1 ;
193181 response = "Let's play a game! I'm thinking of a number between 1 and 10. What's your guess?" ;
194- } else if ( / c a l c u l a t e | w h a t ' ? s | w h a t i s | ^ [ 0 - 9 + \- * / ( ) . \s ] + $ / i. test ( input ) ) {
182+ }
183+ else {
184+ // Fallback: call backend for AI response
195185 try {
196- const expression = input . replace ( / [ ^ 0 - 9 + \- * / ( ) . ] / g, "" ) ;
197- const result = Function ( "'use strict'; return (" + expression + ")" ) ( ) ;
198- response = `The result is ${ result } ` ;
186+ const apiResponse = await fetch ( BACKEND_URL , {
187+ method : "POST" ,
188+ headers : { "Content-Type" : "application/json" } ,
189+ body : JSON . stringify ( { message : input } )
190+ } ) ;
191+ const data = await apiResponse . json ( ) ;
192+ response = data . reply || "Sorry, I didn't understand that." ;
199193 } catch {
200- response = "I couldn't calculate that. Please check your expression ." ;
194+ response = "Sorry, I couldn't connect to the server ." ;
201195 }
202- } else {
203- response = "Sorry, I didn't understand that." ;
204196 }
205197
206198 addAssistantMessage ( response ) ;
0 commit comments