modified: bot.db

modified:   db/db.go
	modified:   handlers/dashboard.go
	modified:   main.go
	modified:   services/openrouter.go
	modified:   templates/dashboard.html
This commit is contained in:
2026-03-01 06:34:23 -03:00
parent 39cc33a2ec
commit d4d395356b
7 changed files with 316 additions and 209 deletions

View File

@@ -1,30 +1,51 @@
package services
import (
"bytes"
"encoding/json"
"io"
"net/http"
"os"
bytes "bytes"
json "encoding/json"
io "io"
http "net/http"
os "os"
"whatsapp-bot/db" // Ensure this matches your module name
)
// The structs to map OpenRouter's response
type Message struct {
Role string `json:"role"`
Content string `json:"content"`
}
type OpenRouterResponse struct {
Choices []struct {
Message struct {
Content string `json:"content"`
Content string `json:"content"`
ToolCalls []struct {
ID string `json:"id"`
Type string `json:"type"`
Function struct {
Name string `json:"name"`
Arguments string `json:"arguments"`
} `json:"function"`
} `json:"tool_calls"`
} `json:"message"`
} `json:"choices"`
}
func GetAIResponse(input string) (string, error) {
func GetAIResponse(chatHistory []Message) (string, error) {
apiKey := os.Getenv("OPENROUTER_API_KEY")
payload := map[string]interface{}{
"model": "google/gemini-2.0-flash-001",
"messages": []map[string]string{
{"role": "system", "content": "You are a Chilean business assistant. Be brief."},
{"role": "user", "content": input},
url := "https://openrouter.ai/api/v1/chat/completions"
// 1. Build the full message list with a System Prompt
fullMessages := append([]Message{
{
Role: "system",
Content: "You are a helpful Chilean business assistant. You can book appointments. If a user wants to schedule something, use the create_appointment tool. Always be concise and polite.",
},
}, chatHistory...)
// 2. Define the tools the AI can use
payload := map[string]interface{}{
"model": "stepfun/step-3.5-flash:free",
"messages": fullMessages,
"tools": []map[string]interface{}{
{
"type": "function",
@@ -34,8 +55,8 @@ func GetAIResponse(input string) (string, error) {
"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"},
"customer_phone": map[string]string{"type": "string", "description": "The user's phone number"},
"date": map[string]string{"type": "string", "description": "The date and time in YYYY-MM-DD HH:MM format"},
},
"required": []string{"customer_phone", "date"},
},
@@ -44,8 +65,8 @@ func GetAIResponse(input string) (string, error) {
},
}
data, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewBuffer(data))
jsonData, _ := json.Marshal(payload)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
@@ -56,10 +77,8 @@ func GetAIResponse(input string) (string, error) {
defer resp.Body.Close()
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
return "Error from OpenRouter: " + string(bodyBytes), nil
}
var result OpenRouterResponse
@@ -68,8 +87,33 @@ func GetAIResponse(input string) (string, error) {
}
if len(result.Choices) > 0 {
return result.Choices[0].Message.Content, nil
aiMsg := result.Choices[0].Message
// 3. Handle Tool Calls (The "Logic" part)
if len(aiMsg.ToolCalls) > 0 {
tc := aiMsg.ToolCalls[0].Function
if tc.Name == "create_appointment" {
var args struct {
Phone string `json:"customer_phone"`
Date string `json:"date"`
}
// Unmarshal the AI-generated arguments
json.Unmarshal([]byte(tc.Arguments), &args)
// Save to DB using your helper
err := db.SaveAppointment(args.Phone, args.Date)
if err != nil {
return "I tried to book it, but the database hates me: " + err.Error(), nil
}
return "✅ [SYSTEM] Appointment automatically booked for " + args.Phone + " at " + args.Date, nil
}
}
// 4. Return plain text if no tool was called
if aiMsg.Content != "" {
return aiMsg.Content, nil
}
}
return "No response from AI.", nil
return "The AI is giving me the silent treatment, seki.", nil
}