-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
53 lines (43 loc) · 1.63 KB
/
index.ts
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
import * as admin from "firebase-admin";
import * as functions from "firebase-functions";
import { protos } from "@google-cloud/dialogflow";
import { WebhookClient } from "dialogflow-fulfillment";
interface Person {
country: string;
email: string;
name: string;
nationality: string;
}
interface RequestWithBody extends functions.https.Request {
body: {
queryResult: protos.google.cloud.dialogflow.v2.IQueryResult;
};
}
const welcome = (agent: WebhookClient): void => {
agent.add("Welcome to my agent!");
};
const fallback = (agent: WebhookClient): void => {
agent.add("I didn't understand");
agent.add("I'm sorry, can you try again?");
};
const firstContactHandler = (
result: protos.google.cloud.dialogflow.v2.IQueryResult
): Function => async (agent: WebhookClient): Promise<void> => {
const db = admin.firestore();
const { country, email, name, nationality } = result.parameters as Person;
const user = db.collection("users").doc(email);
await user.set({ country, nationality, name, email });
agent.add("Thank you very much for your data.");
agent.add("We will follow up with detailed information via email.");
};
export const dialogflowFirebaseFulfillment = functions.https.onRequest(
async (request: RequestWithBody, response): Promise<void> => {
const agent = new WebhookClient({ request, response });
const { queryResult } = request.body;
const intentMap = new Map();
intentMap.set("Default Welcome Intent", welcome);
intentMap.set("Default Fallback Intent", fallback);
intentMap.set("Relocation Intent", firstContactHandler(queryResult));
await agent.handleRequest(intentMap);
}
);