-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
49 lines (42 loc) · 1.25 KB
/
server.ts
File metadata and controls
49 lines (42 loc) · 1.25 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
import express, { json } from "express";
import {
EDialogueActor,
IConversation,
} from "@bavard/agent-config/dist/conversations";
import {
EAgentActionTypes,
EUserActionType,
} from "@bavard/agent-config/dist/enums";
import { IAgentUtteranceAction } from "@bavard/agent-config";
const app = express();
app.use(json());
const port = 3000;
app.post("/my-webhook-action", (req, res) => {
const secret = req.headers.authorization?.split(" ")?.[1];
if (secret !== "super-secret") {
res.sendStatus(401);
return;
}
const conversation: IConversation = req.body.conversation;
const lastTurn = conversation.turns[conversation.turns.length - 1];
const responseAction: IAgentUtteranceAction = {
type: EAgentActionTypes.UTTERANCE_ACTION,
name: "my-response-action",
utterance: "Sorry, Dave. I'm afraid I can't do that.",
};
if (
lastTurn.actor === EDialogueActor.USER &&
lastTurn.userAction.type === EUserActionType.UTTERANCE_ACTION
) {
if (lastTurn.userAction.utterance === "hi") {
responseAction.utterance = "hi";
}
}
res.send(
// NOTE: You must return an array containing 1 to 3 response actions.
[responseAction]
);
});
app.listen(port, () => {
console.log(`Listening http://localhost:${port}`);
});