130 lines
4 KiB
TypeScript
130 lines
4 KiB
TypeScript
import { createLogger } from "@link-stack/logger";
|
|
import { Hono } from "hono";
|
|
|
|
import type SignalService from "./service.ts";
|
|
|
|
const logger = createLogger("bridge-signal-routes");
|
|
|
|
const errorMessage = (error: unknown): string => (error instanceof Error ? error.message : String(error));
|
|
|
|
export function createRoutes(service: SignalService): Hono {
|
|
const app = new Hono();
|
|
|
|
// Start device linking
|
|
app.post("/api/bots/:id/register", async (c) => {
|
|
const id = c.req.param("id");
|
|
const { phoneNumber, deviceName } = await c.req.json<{
|
|
phoneNumber: string;
|
|
deviceName?: string;
|
|
}>();
|
|
|
|
try {
|
|
const result = await service.register(id, phoneNumber, deviceName);
|
|
logger.info({ id }, "Device linking started");
|
|
return c.json(result);
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to start device linking");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Bot status
|
|
app.get("/api/bots/:id", async (c) => {
|
|
const id = c.req.param("id");
|
|
try {
|
|
return c.json(await service.getBot(id));
|
|
} catch (error) {
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Send message
|
|
app.post("/api/bots/:id/send", async (c) => {
|
|
const id = c.req.param("id");
|
|
const { recipient, message, attachments, autoGroup } = await c.req.json<{
|
|
recipient: string;
|
|
message: string;
|
|
attachments?: Array<{ data: string; filename: string; mime_type: string }>;
|
|
autoGroup?: { ticketNumber: string };
|
|
}>();
|
|
|
|
try {
|
|
const result = await service.send(id, recipient, message, attachments, autoGroup);
|
|
logger.info({ id, recipient: result.recipient, attachmentCount: attachments?.length || 0 }, "Sent message");
|
|
return c.json({ result });
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to send message");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Unregister bot
|
|
app.post("/api/bots/:id/unregister", async (c) => {
|
|
const id = c.req.param("id");
|
|
try {
|
|
await service.unregister(id);
|
|
logger.info({ id }, "Bot unregistered");
|
|
return c.body(null, 200);
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to unregister bot");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Create group
|
|
app.post("/api/bots/:id/groups", async (c) => {
|
|
const id = c.req.param("id");
|
|
const { name, members, description } = await c.req.json<{
|
|
name: string;
|
|
members: string[];
|
|
description?: string;
|
|
}>();
|
|
|
|
try {
|
|
const result = await service.createGroup(id, name, members, description);
|
|
logger.info({ id, groupId: result.groupId }, "Group created");
|
|
return c.json(result);
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to create group");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Update group
|
|
app.put("/api/bots/:id/groups/:groupId", async (c) => {
|
|
const id = c.req.param("id");
|
|
const groupId = c.req.param("groupId");
|
|
const { name, description } = await c.req.json<{
|
|
name?: string;
|
|
description?: string;
|
|
}>();
|
|
|
|
try {
|
|
await service.updateGroup(id, groupId, name, description);
|
|
logger.info({ id, groupId }, "Group updated");
|
|
return c.json({ success: true });
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to update group");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// List groups
|
|
app.get("/api/bots/:id/groups", async (c) => {
|
|
const id = c.req.param("id");
|
|
try {
|
|
const groups = await service.listGroups(id);
|
|
return c.json(groups);
|
|
} catch (error) {
|
|
logger.error({ id, error: errorMessage(error) }, "Failed to list groups");
|
|
return c.json({ error: errorMessage(error) }, 500);
|
|
}
|
|
});
|
|
|
|
// Health check
|
|
app.get("/api/health", (c) => {
|
|
return c.json({ status: "ok" });
|
|
});
|
|
|
|
return app;
|
|
}
|