Skip to content

Commit 22f363c

Browse files
committed
feat: add Telegram notifications for call and assessment events
1 parent 6bb1aed commit 22f363c

3 files changed

Lines changed: 70 additions & 1 deletion

File tree

src/app/api/webhooks/assessment/status/route.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { prisma } from "@/lib/prisma";
3+
import { sendTelegramNotification } from "@/lib/telegram-api";
34

45
export async function POST(req: NextRequest) {
56
const formData = await req.formData();
@@ -16,10 +17,35 @@ export async function POST(req: NextRequest) {
1617

1718
if (session.status !== "COMPLETED") {
1819
if (callStatus === "completed" || callStatus === "no-answer" || callStatus === "busy" || callStatus === "failed") {
20+
const updatedStatus = session.status === "IN_PROGRESS" ? "COMPLETED" : "FAILED";
1921
await prisma.assessmentSession.update({
2022
where: { id: sessionId },
21-
data: { status: session.status === "IN_PROGRESS" ? "COMPLETED" : "FAILED" },
23+
data: { status: updatedStatus },
2224
});
25+
26+
// Send Telegram notification
27+
try {
28+
const profile = await prisma.elderlyProfile.findUnique({
29+
where: { id: session.elderlyProfileId },
30+
});
31+
if (profile) {
32+
if (updatedStatus === "COMPLETED" && session.overallScore !== null) {
33+
const severity = session.severity || "GREEN";
34+
const summary = session.summary || "";
35+
await sendTelegramNotification(
36+
session.elderlyProfileId,
37+
`${profile.name}'s assessment: score ${Math.round((session.overallScore ?? 0) * 100)}% (${severity}).\n${summary}`
38+
);
39+
} else if (updatedStatus === "FAILED") {
40+
await sendTelegramNotification(
41+
session.elderlyProfileId,
42+
`${profile.name}'s assessment call failed.`
43+
);
44+
}
45+
}
46+
} catch (err) {
47+
console.error("Telegram notification error:", err);
48+
}
2349
}
2450
}
2551

src/app/api/webhooks/twilio/route.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { prisma } from "@/lib/prisma";
3+
import { sendTelegramNotification } from "@/lib/telegram-api";
34

45
export async function POST(req: NextRequest) {
56
const formData = await req.formData();
@@ -22,6 +23,7 @@ export async function POST(req: NextRequest) {
2223
// Only deactivate one-time reminders; recurring ones should keep firing
2324
const reminder = await prisma.reminder.findUnique({
2425
where: { id: log.reminderId },
26+
include: { elderlyProfile: true, medication: true },
2527
});
2628
if (reminder && reminder.recurrence === "NONE") {
2729
await prisma.reminder.update({
@@ -30,6 +32,20 @@ export async function POST(req: NextRequest) {
3032
});
3133
}
3234

35+
// Send Telegram notification for confirmation
36+
if (reminder) {
37+
try {
38+
const name = reminder.elderlyProfile.name;
39+
const task = reminder.medication?.name || reminder.title;
40+
await sendTelegramNotification(
41+
reminder.elderlyProfileId,
42+
`${name} confirmed their ${reminder.scheduledTime} ${task} reminder.`
43+
);
44+
} catch (err) {
45+
console.error("Telegram notification error:", err);
46+
}
47+
}
48+
3349
return new NextResponse(
3450
"<Response><Say>Thank you! Your confirmation has been recorded. Take care!</Say></Response>",
3551
{ headers: { "Content-Type": "text/xml" } }

src/app/api/webhooks/twilio/status/route.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { NextRequest, NextResponse } from "next/server";
22
import { prisma } from "@/lib/prisma";
3+
import { sendTelegramNotification } from "@/lib/telegram-api";
34

45
export async function POST(req: NextRequest) {
56
const logId = req.nextUrl.searchParams.get("logId");
@@ -18,6 +19,32 @@ export async function POST(req: NextRequest) {
1819
where: { id: logId },
1920
data: { status: newStatus },
2021
});
22+
23+
// Send Telegram notification
24+
try {
25+
const reminder = await prisma.reminder.findUnique({
26+
where: { id: log.reminderId },
27+
include: { elderlyProfile: true, medication: true },
28+
});
29+
if (reminder) {
30+
const name = reminder.elderlyProfile.name;
31+
const task = reminder.medication?.name || reminder.title;
32+
const time = reminder.scheduledTime;
33+
if (newStatus === "NO_ANSWER") {
34+
await sendTelegramNotification(
35+
reminder.elderlyProfileId,
36+
`${name} didn't answer their ${time} ${task} reminder (attempt ${log.attemptNumber}).`
37+
);
38+
} else {
39+
await sendTelegramNotification(
40+
reminder.elderlyProfileId,
41+
`${name}'s ${time} ${task} reminder call failed.`
42+
);
43+
}
44+
}
45+
} catch (err) {
46+
console.error("Telegram notification error:", err);
47+
}
2148
}
2249

2350
return NextResponse.json({ ok: true });

0 commit comments

Comments
 (0)