-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsupport.js
More file actions
71 lines (63 loc) · 2.43 KB
/
Copy pathsupport.js
File metadata and controls
71 lines (63 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { telegram } from "../telegram.js";
import { getState, setState, clearState } from "../kv/state.js";
import { getAdmins } from "../kv/config.js";
const FLOW = "support";
export const Support = {
async start(chatId, env) {
await setState(chatId, FLOW, { step: "awaiting_ticket_text", data: {} }, env);
await telegram.sendMessage(chatId, "Please describe your issue. Send it as a text message.", env);
},
// Matches the blueprint's Support.isAwaitingText(chatId, env) contract exactly
// (env last, opposite of telegram.js's convention — deliberate per spec).
async isAwaitingText(chatId, env) {
const state = await getState(chatId, FLOW, env);
return !!state && state.step === "awaiting_ticket_text";
},
async promptTextOnly(chatId, env) {
await telegram.sendMessage(chatId, "Please send your response as text.", env);
},
async handleText(chatId, userId, text, env) {
const id = await nextTicketId(env);
const ticket = {
id,
chat_id: chatId,
user_id: userId,
text,
status: "open",
created_at: Date.now(),
};
await env.KV.put(`ticket:${id}`, JSON.stringify(ticket));
await clearState(chatId, FLOW, env);
await telegram.sendMessage(chatId, `Ticket #${id} created. We'll get back to you soon.`, env);
await notifyStaff(ticket, env);
return ticket;
},
async resolve(ticketId, notes, resolverId, env) {
const key = `ticket:${ticketId}`;
const raw = await env.KV.get(key);
if (!raw) throw new Error(`Ticket ${ticketId} not found`);
const ticket = JSON.parse(raw);
ticket.status = "resolved";
ticket.resolved_by = resolverId;
ticket.resolution = notes || "";
ticket.resolved_at = Date.now();
await env.KV.put(key, JSON.stringify(ticket));
return ticket;
},
};
async function nextTicketId(env) {
const counterKey = "counter:ticket";
const current = await env.KV.get(counterKey);
const next = (current ? parseInt(current, 10) : 0) + 1;
await env.KV.put(counterKey, String(next));
return next;
}
async function notifyStaff(ticket, env) {
const admins = await getAdmins(env);
const recipients = new Set([Number(env.OWNER_ID), ...admins]);
const text = `New support ticket #${ticket.id}\n\n${ticket.text}`;
const keyboard = {
inline_keyboard: [[{ text: "✅ Mark resolved", callback_data: `support:resolve:${ticket.id}` }]],
};
await telegram.sendToMany(recipients, text, env, { reply_markup: keyboard });
}