-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathapp.js
63 lines (52 loc) · 1.5 KB
/
app.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
const {Telegraf} = require('telegraf')
const axios = require('axios');
require('dotenv').config();
const bot = new Telegraf(process.env.BOT_TOKEN)
async function interact(ctx, chatID, request) {
const response = await axios({
method: "POST",
url: `https://general-runtime.voiceflow.com/state/user/${chatID}/interact`,
headers: {
Authorization: process.env.VF_API_KEY
},
data: {
request
}
});
for (const trace of response.data) {
switch (trace.type) {
case "text":
case "speak":
{
await ctx.reply(trace.payload.message);
break;
}
case "visual":
{
await ctx.replyWithPhoto(trace.payload.image);
break;
}
case "end":
{
await ctx.reply("Conversation is over");
break;
}
}
}
};
bot.start(async (ctx) => {
let chatID = ctx.message.chat.id;
await interact(ctx, chatID, {type: "launch"});
});
const ANY_WORD_REGEX = new RegExp(/(.+)/i);
bot.hears(ANY_WORD_REGEX, async (ctx) => {
let chatID = ctx.message.chat.id;
await interact(ctx, chatID, {
type: "text",
payload: ctx.message.text
});
})
bot.launch()
// Enable graceful stop
process.once('SIGINT', () => bot.stop('SIGINT'))
process.once('SIGTERM', () => bot.stop('SIGTERM'))