-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (141 loc) · 4.7 KB
/
index.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
require("dotenv").config();
const { provideCore } = require("@yext/answers-core");
const answers = provideCore({
apiKey: process.env.ANSWERS_API_KEY,
experienceKey: "dasha",
locale: "en",
experienceVersion: "PRODUCTION",
endpoints: {
universalSearch:
"https://liveapi-sandbox.yext.com/v2/accounts/me/answers/query?someparam=blah",
verticalSearch:
"https://liveapi-sandbox.yext.com/v2/accounts/me/answers/vertical/query",
questionSubmission:
"https://liveapi-sandbox.yext.com/v2/accounts/me/createQuestion",
status: "https://answersstatus.pagescdn.com",
universalAutocomplete:
"https://liveapi-sandbox.yext.com/v2/accounts/me/answers/autocomplete",
verticalAutocomplete:
"https://liveapi-sandbox.yext.com/v2/accounts/me/answers/vertical/autocomplete",
filterSearch:
"https://liveapi-sandbox.yext.com/v2/accounts/me/answers/filtersearch",
},
});
const dasha = require("@dasha.ai/sdk");
const fs = require("fs");
const main = async () => {
const app = await dasha.deploy("./app");
app.connectionProvider = async (conv) =>
conv.input.phone === "chat"
? dasha.chat.connect(await dasha.chat.createConsoleChat())
: dasha.sip.connect(new dasha.sip.Endpoint("default"));
app.ttsDispatcher = () => "dasha";
app.setExternal("lookForBranch", async (args, conv) => {
const street_num = args.street_num;
const street = args.street;
const city = args.city;
const state = args.state;
const zip_code = args.zip_code;
const locationQuery = `${street_num} ${street} ${city} ${state} ${zip_code}`;
console.log(
`street number: ${street_num} street: ${street} city: ${city} state: ${state} zip code: ${zip_code}`
);
const branchesResponse = await answers.verticalSearch({
query: locationQuery,
verticalKey: "locations",
limit: 1,
locationRadius: 40233.6, // 25 miles
});
if (branchesResponse.verticalResults.results[0]) {
const branch = branchesResponse.verticalResults.results[0].rawData;
const address = branch.address.line1;
const dayOfWeek = getDayOfWeek(new Date().getDay());
console.log(`start hour ${JSON.stringify(branch.hours)}`);
if (dayOfWeek && branch.hours[dayOfWeek].isClosed) {
// TODO: add logic to find next open day
return {
address,
open_time: "9 am",
closedTime: "5 pm",
is_closed: true,
next_open_day: "Monday",
};
} else {
return {
address,
open_time: get12HourTime(
branch.hours[dayOfWeek].openIntervals[0].start
),
close_time: get12HourTime(
branch.hours[dayOfWeek].openIntervals[0].end
),
is_closed: false,
next_open_day: "",
};
}
} else {
return null;
}
});
app.setExternal("searchSpeechFaq", async (args, conv) => {
const query = args.query;
const speechFaqResponse = await answers.verticalSearch({
query,
verticalKey: "speech_faq",
limit: 1,
});
const speechFaq = speechFaqResponse.verticalResults.results[0]
? speechFaqResponse.verticalResults.results[0].rawData
: {};
return {
verbal_response: speechFaq.c_verbalResponse,
follow_up_question: speechFaq.c_followUpQuestion ?? null,
follow_up_data: speechFaq.c_followUpData ?? null,
};
});
// mocking making an API call to book an appointment
app.setExternal("bookAppointment", async (args, conv) => {
setTimeout(() => console.log("Booking Appointment..."), 2000);
});
await app.start();
const conv = app.createConversation({
phone: process.argv[2] ?? "",
});
if (conv.input.phone !== "chat") conv.on("transcription", console.log);
const logFile = await fs.promises.open("./log.txt", "w");
await logFile.appendFile("#".repeat(100) + "\n");
conv.on("transcription", async (entry) => {
await logFile.appendFile(`${entry.speaker}: ${entry.text}\n`);
});
conv.on("debugLog", async (event) => {
if (event?.msg?.msgId === "RecognizedSpeechMessage") {
const logEntry = event?.msg?.results[0]?.facts;
await logFile.appendFile(JSON.stringify(logEntry, undefined, 2) + "\n");
}
});
await conv.execute();
await app.stop();
app.dispose();
};
const getDayOfWeek = (dayIndex) =>
[
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
][dayIndex] || "";
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
const get12HourTime = (time) => {
const [hour, minute] = time.split(":");
if (hour < 12) {
return `${hour}:${minute} AM`;
} else {
return `${parseInt(hour) - 12}:${minute} PM`;
}
};