-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·104 lines (94 loc) · 4.02 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
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
const GIPHY_API_KEY = 'API_KEY';
const SEARCH_RESULTS_MORE = [
'Вот ещё пара гифок!',
'Надеюсь, эти тебе тоже понравятся.',
'На, лови еще парочку. Если что, у меня ещё есть.'
];
const SEARCH_RESULTS = [
'Хе-хе, сейчас покажу мои любимые.',
'Лови, отличная подборка гифок.',
'Смотри, что я нашел!'
];
// Import the Dialogflow module from the Actions on Google client library.
const { dialogflow, BrowseCarouselItem, BrowseCarousel, Suggestions, Image } = require('actions-on-google');
// Import the firebase-functions package for deployment.
const functions = require('firebase-functions');
// Import the request-promise package for network requests.
const request = require('request-promise');
// Instantiate the Dialogflow client.
const app = dialogflow({ debug: true });
function getCarouselItems(data) {
var carouselItems = [];
data.slice(0, 10).forEach(function (gif) {
carouselItems.push(new BrowseCarouselItem({
title: gif.title || gif.id,
url: gif.url,
image: new Image({
url: gif.images.downsized_medium.url,
alt: gif.title || gif.id
}),
}));
});
return carouselItems;
}
function search(conv, query, offset) {
// Send the GET request to GIPHY API.
return request({
method: 'GET',
uri: 'https://api.giphy.com/v1/gifs/search',
qs: {
"api_key": GIPHY_API_KEY,
'q': query,
'limit': 10,
'offset': offset,
'lang': 'ru'
},
json: true,
resolveWithFullResponse: true,
}).then(function (responce) {
// Handle the API call success.
console.log(responce.statusCode + ': ' + responce.statusMessage);
console.log(JSON.stringify(responce.body));
// Obtain carousel items from the API call response.
var carouselItems = getCarouselItems(responce.body.data);
// Validate items count.
if (carouselItems.length <= 10 && carouselItems.length >= 2) {
conv.data.query = query;
conv.data.offset = responce.body.pagination.count + responce.body.pagination.offset;
conv.data.paginationCount = conv.data.paginationCount || 0;
conv.data.searchCount = conv.data.searchCount || 0;
// Show successful response.
if (offset == 0) {
conv.ask(SEARCH_RESULTS[conv.data.searchCount % SEARCH_RESULTS.length]);
conv.data.searchCount++;
} else {
conv.ask(SEARCH_RESULTS_MORE[conv.data.paginationCount % SEARCH_RESULTS_MORE.length]);
conv.data.paginationCount++;
}
conv.ask(new BrowseCarousel({ items: carouselItems }));
conv.ask(new Suggestions(`Ещё`));
} else {
// Show alternative response.
conv.ask('Ничего не смог найти по такому запросу, может поищем что-то другое?)');
}
}).catch(function (error) {
// Handle the API call failure.
console.log(error);
conv.ask('Извини, кажется альбом с гифками потерялся.');
});
}
// Handle the Dialogflow intent named 'Search Intent'.
// The intent collects a parameter named 'query'.
app.intent('Search Intent', (conv, { query }) => {
return search(conv, query, 0);
});
// Handle the Dialogflow intent named 'Search Intent - more'.
app.intent('Search Intent - more', (conv) => {
// Load more gifs from the privious search query
return search(conv, conv.data.query, conv.data.offset);
});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);