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

1
.env
View File

@@ -1 +1,2 @@
OPENROUTER_API_KEY=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

BIN
bot.db

Binary file not shown.

View File

@@ -31,3 +31,11 @@ func Init() {
);`
Conn.Exec(schema)
}
func SaveAppointment(phone string, date string) error {
_, err := Conn.Exec(
"INSERT INTO appointments (customer_phone, appointment_date, status) VALUES (?, ?, ?)",
phone, date, "confirmed",
)
return err
}

1
go.mod
View File

@@ -21,6 +21,7 @@ require (
github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect

2
go.sum
View File

@@ -38,6 +38,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=

View File

@@ -49,6 +49,26 @@ func TestAIHandler(c *gin.Context) {
c.JSON(200, gin.H{"response": response})
}
// Add this to handlers/dashboard.go
func CreateAppointmentHandler(c *gin.Context) {
var body struct {
Phone string `json:"phone"`
Date string `json:"date"`
}
if err := c.BindJSON(&body); err != nil {
c.Status(400)
return
}
// Use the helper function instead of raw SQL here
if err := db.SaveAppointment(body.Phone, body.Date); err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
c.Status(200)
}
// Manage/Delete Appointment
func DeleteAppointmentHandler(c *gin.Context) {
id := c.Param("id")

10
main.go
View File

@@ -1,13 +1,22 @@
package main
import (
"log"
"whatsapp-bot/db"
"whatsapp-bot/handlers"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
// 1. Load the .env file
err := godotenv.Load()
if err != nil {
log.Println("Warning: No .env file found. Hope you set your vars manually, seki.")
}
db.Init()
r := gin.Default()
@@ -21,6 +30,7 @@ func main() {
r.GET("/dashboard", handlers.ShowDashboard)
r.POST("/admin/test-ai", handlers.TestAIHandler)
r.DELETE("/admin/appointment/:id", handlers.DeleteAppointmentHandler)
r.POST("/admin/appointment", handlers.CreateAppointmentHandler)
// A little something for the root so you don't get a 404 again, seki
r.GET("/", func(c *gin.Context) {

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
}

View File

@@ -1,69 +1,210 @@
{{ define "dashboard.html" }}
<!DOCTYPE html>
<html>
<html lang="en">
<head>
<title>SekiBot Admin</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SekiBot Admin | Discord Edition</title>
<style>
body { font-family: 'Segoe UI', sans-serif; background: #121212; color: white; padding: 40px; }
.card { background: #1e1e1e; padding: 20px; border-radius: 8px; margin-bottom: 20px; border: 1px solid #333; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid #333; }
button { background: #3498db; color: white; border: none; padding: 8px 15px; border-radius: 4px; cursor: pointer; }
button.delete { background: #e74c3c; }
input { padding: 8px; border-radius: 4px; border: 1px solid #444; background: #222; color: white; width: 70%; }
#ai-response { margin-top: 10px; padding: 10px; background: #2c3e50; border-radius: 4px; display: none; }
:root {
--blurple: #5865F2;
--background-dark: #36393f;
--background-darker: #2f3136;
--background-deep: #202225;
--text-normal: #dcddde;
--text-muted: #b9bbbe;
--header-primary: #ffffff;
--danger: #ed4245;
--success: #3ba55c;
}
body {
background-color: var(--background-deep);
color: var(--text-normal);
font-family: 'Whitney', 'Helvetica Neue', Helvetica, Arial, sans-serif;
margin: 0;
display: flex;
height: 100vh;
}
/* Sidebar Look */
.sidebar {
width: 260px;
background-color: var(--background-darker);
display: flex;
flex-direction: column;
padding: 20px;
border-right: 1px solid #202225;
}
.main-content {
flex-grow: 1;
background-color: var(--background-dark);
padding: 40px;
overflow-y: auto;
}
h1, h2, h3 { color: var(--header-primary); margin-top: 0; }
.card {
background-color: var(--background-darker);
border-radius: 8px;
padding: 24px;
margin-bottom: 24px;
box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}
/* Inputs & Buttons */
input {
background-color: var(--background-deep);
border: 1px solid #202225;
color: white;
padding: 10px;
border-radius: 4px;
width: calc(100% - 22px);
margin-bottom: 10px;
font-size: 14px;
}
button {
background-color: var(--blurple);
color: white;
border: none;
padding: 10px 20px;
border-radius: 3px;
cursor: pointer;
font-weight: 500;
transition: background 0.2s;
}
button:hover { background-color: #4752c4; }
button.delete { background-color: var(--danger); }
button.delete:hover { background-color: #c03537; }
/* Table Styling */
table { width: 100%; border-collapse: collapse; margin-top: 10px; }
th { text-align: left; color: var(--text-muted); text-transform: uppercase; font-size: 12px; padding-bottom: 10px; }
td { padding: 12px 0; border-top: 1px solid #42454a; vertical-align: middle; }
.status-pill {
background-color: var(--success);
color: white;
padding: 2px 8px;
border-radius: 12px;
font-size: 11px;
font-weight: bold;
}
#ai-response-box {
margin-top: 15px;
padding: 15px;
background-color: var(--background-deep);
border-radius: 4px;
border-left: 4px solid var(--blurple);
display: none;
white-space: pre-wrap;
}
</style>
</head>
<body>
<h1>SekiBot Business Analytics</h1>
<!-- AI Test Section -->
<div class="card">
<h2>Test OpenRouter AI</h2>
<input type="text" id="ai-input" placeholder="Ask the bot something...">
<button onclick="testAI()">Send</button>
<div id="ai-response"></div>
<div class="sidebar">
<h2>SekiBot</h2>
<p style="font-size: 12px; color: var(--text-muted);">v1.0.0-beta</p>
<hr style="border: 0.5px solid #42454a; width: 100%;">
<nav>
<p style="color: var(--blurple); font-weight: bold;"># dashboard</p>
<p style="color: var(--text-muted);"># analytics</p>
<p style="color: var(--text-muted);"># settings</p>
</nav>
</div>
<!-- Appointments Management -->
<div class="card">
<h2>Manage Appointments</h2>
<table>
<tr><th>ID</th><th>Customer</th><th>Date</th><th>Status</th><th>Actions</th></tr>
{{range .Appointments}}
<tr id="appt-{{.ID}}">
<td>{{.ID}}</td>
<td>{{.CustomerPhone}}</td>
<td>{{.Date}}</td>
<td>{{.Status}}</td>
<td>
<button class="delete" onclick="deleteAppt({{.ID}})">Cancel</button>
</td>
</tr>
{{end}}
</table>
<div class="main-content">
<h1>Welcome back, seki</h1>
<!-- AI Tester -->
<div class="card">
<h3>Test OpenRouter AI</h3>
<div style="display: flex; gap: 10px;">
<input type="text" id="ai-input" placeholder="Message the bot..." style="margin-bottom: 0;">
<button onclick="testAI()">Send</button>
</div>
<div id="ai-response-box"></div>
</div>
<!-- Manual Entry -->
<div class="card">
<h3>Quick Create Appointment</h3>
<div style="display: grid; grid-template-columns: 1fr 1fr 100px; gap: 10px;">
<input type="text" id="phone" placeholder="Customer Phone (+569...)">
<input type="datetime-local" id="date">
<button onclick="createAppt()">Add</button>
</div>
</div>
<!-- The Table -->
<div class="card">
<h3>Scheduled Appointments</h3>
<table>
<thead>
<tr>
<th>ID</th>
<th>Customer</th>
<th>Date & Time</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{range .Appointments}}
<tr id="row-{{.ID}}">
<td>#{{.ID}}</td>
<td style="font-weight: bold;">{{.CustomerPhone}}</td>
<td>{{.Date}}</td>
<td><span class="status-pill">{{.Status}}</span></td>
<td>
<button class="delete" onclick="deleteAppt({{.ID}})">Cancel</button>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
</div>
<script>
async function testAI() {
const input = document.getElementById('ai-input').value;
const resDiv = document.getElementById('ai-response');
resDiv.style.display = 'block';
resDiv.innerText = 'Thinking... (Wait for it, seki)';
const box = document.getElementById('ai-response-box');
box.style.display = 'block';
box.innerText = "Processing message...";
const res = await fetch('/admin/test-ai', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({prompt: input})
});
const data = await res.json();
resDiv.innerText = data.response;
box.innerText = data.response;
}
async function createAppt() {
const phone = document.getElementById('phone').value;
const date = document.getElementById('date').value;
if(!phone || !date) return alert("Fill the fields, seki.");
await fetch('/admin/appointment', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({phone, date})
});
location.reload();
}
async function deleteAppt(id) {
if(!confirm('Really cancel this?')) return;
if(!confirm("Terminate this appointment?")) return;
await fetch(`/admin/appointment/${id}`, { method: 'DELETE' });
document.getElementById(`appt-${id}`).remove();
document.getElementById(`row-${id}`).remove();
}
</script>
</body>