-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiai-functions.ts
59 lines (53 loc) · 1.79 KB
/
apiai-functions.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
54
55
56
57
58
59
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { apiai } from '../core'
export const dbTrigger = functions.database.ref('/chatbot/messages/{pushId}')
.onWrite(event => {
const data = event.data.val();
if (data.to != undefined && data.text != undefined && data.from == 'bot') {
console.log("no reply for bot message");
return 0;
}
const query = data.text;
console.log("you said: ", query);
apiai.textQuery(query, {
sessionId: "adfadff"
}).then(function (response) {
const reply = response.result.fulfillment.speech;
console.log("reply is: ", reply);
return event.data.ref.parent.push({
text: reply,
from: "bot",
to: "zia"
});
});
});
export const webhook = functions.https.onRequest(async (req, res) => {
console.log("this is body: ", req.body);
console.log("this is headers: ", req.headers);
var response = {}
// response = {
// speech: "this is speech from firebase",
// displayText: "this is displayText from firebase",
// data: { "this": "is data from firebase" },
// contextOut: [
// {
// "name": "weather",
// "lifespan": 2,
// "parameters": { "city": "Rome" }
// }
// ],
// source: "this is source from firebase",
// // followupEvent: {
// // name: "signup",
// // data: {
// // 'givenname': "john",
// // 'lastname': "john"
// // }
// // },
// }
console.log("response: ", response);
res.json({
speech: "working"
});
});