@qemuguest/site-chat-server 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/index.cjs +671 -0
- package/dist/index.d.cts +259 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.js +634 -0
- package/package.json +40 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 qemuguest
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @qemuguest/site-chat-server
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Backend for website live chat. Web-standard handlers (`Request → Response`), pluggable storage, and relays
|
|
4
|
+
to **SimpleX** (one chat per customer, via [`@qemuguest/site-chat-simplex-bridge`](https://www.npmjs.com/package/@qemuguest/site-chat-simplex-bridge)),
|
|
5
|
+
**Telegram** (two-way) and **email**. Pairs with [`@qemuguest/site-chat-widget`](https://www.npmjs.com/package/@qemuguest/site-chat-widget).
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm i @qemuguest/site-chat-server
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
import { createSiteChat, sqlStore, d1Driver, simplexRelay, telegram, emailAlerts, resendEmail } from "@qemuguest/site-chat-server";
|
|
13
|
+
|
|
14
|
+
const chat = createSiteChat({
|
|
15
|
+
store: sqlStore(d1Driver(env.DB)),
|
|
16
|
+
basePath: "/api/site-chat", // where you mount it (default)
|
|
17
|
+
channels: [
|
|
18
|
+
simplexRelay({ secret: env.SITE_CHAT_SECRET }),
|
|
19
|
+
telegram({ botToken: env.TELEGRAM_BOT_TOKEN, webhookSecret: env.TELEGRAM_WEBHOOK_SECRET }),
|
|
20
|
+
emailAlerts({ to: "me@example.com", send: resendEmail({ apiKey: env.RESEND_API_KEY, from: "Site <chat@example.com>" }) }),
|
|
21
|
+
],
|
|
22
|
+
adminUrl: (id) => `https://example.com/admin/messages?c=${id}`, // linked from alerts
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// Any framework: hand it the Request.
|
|
26
|
+
export default { fetch: (req: Request) => chat.fetch(req) }; // Cloudflare Worker
|
|
27
|
+
export const GET = (req: Request) => chat.fetch(req), POST = GET; // Next.js app/api/site-chat/[...path]/route.ts
|
|
28
|
+
app.all("/api/site-chat/*", (c) => chat.fetch(c.req.raw)); // Hono
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`chat.handle(req)` resolves to `null` for paths that aren't its own, so you can mount it alongside other routes.
|
|
32
|
+
|
|
33
|
+
## Endpoints (under `basePath`)
|
|
34
|
+
|
|
35
|
+
| Method & path | Who | What |
|
|
36
|
+
| --- | --- | --- |
|
|
37
|
+
| `POST /messages` | widget | start / continue a chat, send `{ text }` or `{ details: { name, email, phone } }` |
|
|
38
|
+
| `GET /messages?id&token&after` | widget | poll for replies |
|
|
39
|
+
| `POST /contact` | widget | contact form `{ name, email, phone?, business?, message }` |
|
|
40
|
+
| `POST /telegram/webhook` | Telegram | bot updates (checked against `webhookSecret`) |
|
|
41
|
+
| `GET /relay/outbox`, `POST /relay/ack`, `POST /relay/reply` | SimpleX bridge | `Authorization: Bearer <secret>` |
|
|
42
|
+
|
|
43
|
+
Built in: per-IP limit on new conversations, per-conversation message limit, honeypot field, visitor tokens
|
|
44
|
+
(only the visitor can read their thread), IPs stored only as salted hashes.
|
|
45
|
+
|
|
46
|
+
## Storage
|
|
47
|
+
|
|
48
|
+
- `memoryStore()` for tests and dev.
|
|
49
|
+
- `sqlStore(driver, { prefix: "sc_" })` with `d1Driver(env.DB)` (Cloudflare D1) or `sqliteDriver(db)`
|
|
50
|
+
(better-sqlite3, `node:sqlite`). Create tables with `await store.migrate()` or run `schemaSql()` as a migration.
|
|
51
|
+
- Or implement the `ChatStore` interface for anything else.
|
|
52
|
+
|
|
53
|
+
## Admin
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
await chat.admin.list(); // conversations with last message + unread state
|
|
57
|
+
await chat.admin.get(id); // { conversation, messages }
|
|
58
|
+
await chat.admin.reply(id, "On it!"); // appears live in the visitor's chat, mirrored to Telegram / SimpleX
|
|
59
|
+
await chat.admin.markRead(id);
|
|
60
|
+
await chat.admin.unreadCount();
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Telegram
|
|
64
|
+
|
|
65
|
+
1. Create a bot with @BotFather; pick any long random `webhookSecret`.
|
|
66
|
+
2. `const tg = telegram({ botToken, webhookSecret })`, pass it in `channels`, then once:
|
|
67
|
+
`const { link } = await tg.connect("https://example.com/api/site-chat/telegram/webhook")`.
|
|
68
|
+
3. Open `link` and press Start. That chat is now the only one that gets messages and can answer.
|
|
69
|
+
Reply to a relayed message to answer the visitor live.
|
|
70
|
+
|
|
71
|
+
## SimpleX
|
|
72
|
+
|
|
73
|
+
Add `simplexRelay({ secret })` and run the bridge next to your SimpleX app. See
|
|
74
|
+
[`@qemuguest/site-chat-simplex-bridge`](https://www.npmjs.com/package/@qemuguest/site-chat-simplex-bridge).
|
|
75
|
+
|
|
76
|
+
## Custom channels
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const slack: Channel = {
|
|
80
|
+
name: "slack",
|
|
81
|
+
async notify(e, ctx) { if (e.type === "visitor") await postToSlack(e.conversation, e.body); },
|
|
82
|
+
routes: { async "POST /slack/events"(req, ctx) { /* … */ await ctx.agentReply(conversationId, text, "slack"); return new Response("ok"); } },
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
MIT
|