modified: .env

modified:   bot.db
	modified:   db/db.go
	modified:   go.mod
	modified:   go.sum
	modified:   handlers/dashboard.go
	modified:   main.go
	modified:   services/openrouter.go
	modified:   templates/dashboard.html
This commit is contained in:
2026-03-01 05:39:31 -03:00
parent 7a5f5d86f4
commit 9a341fed76
9 changed files with 270 additions and 46 deletions

View File

@@ -3,18 +3,45 @@ package services
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
)
// The structs to map OpenRouter's response
type OpenRouterResponse struct {
Choices []struct {
Message struct {
Content string `json:"content"`
} `json:"message"`
} `json:"choices"`
}
func GetAIResponse(input string) (string, error) {
apiKey := os.Getenv("OPENROUTER_API_KEY")
payload := map[string]interface{}{
"model": "stepfun/step-3.5-flash:free",
"model": "google/gemini-2.0-flash-001",
"messages": []map[string]string{
{"role": "system", "content": "You are a business assistant in Chile. Book appointments or answer FAQs."},
{"role": "system", "content": "You are a Chilean business assistant. Be brief."},
{"role": "user", "content": input},
},
"tools": []map[string]interface{}{
{
"type": "function",
"function": map[string]interface{}{
"name": "create_appointment",
"description": "Schedules a new appointment in the database",
"parameters": map[string]interface{}{
"type": "object",
"properties": map[string]interface{}{
"customer_phone": map[string]string{"type": "string"},
"date": map[string]string{"type": "string", "description": "ISO format date and time"},
},
"required": []string{"customer_phone", "date"},
},
},
},
},
}
data, _ := json.Marshal(payload)
@@ -28,7 +55,21 @@ func GetAIResponse(input string) (string, error) {
}
defer resp.Body.Close()
// Simplification: You need to parse the JSON response here, seki.
// I assume you know how to decode a nested map. Big assumption, I know.
return "AI Response Placeholder", nil
bodyBytes, _ := io.ReadAll(resp.Body)
// If it's not a 200, return the error body so you can see why it failed
if resp.StatusCode != http.StatusOK {
return "API Error: " + string(bodyBytes), nil
}
var result OpenRouterResponse
if err := json.Unmarshal(bodyBytes, &result); err != nil {
return "", err
}
if len(result.Choices) > 0 {
return result.Choices[0].Message.Content, nil
}
return "No response from AI.", nil
}