-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
218 lines (196 loc) · 8.25 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const clova = require('@line/clova-cek-sdk-nodejs');
const line = require('@line/bot-sdk');
const express = require('express');
const jsonData = require('./data.json');
require('dotenv').config();
const MAX_QUESTION = 5;
const msg = '動物を答えてください。';
const client = new line.Client({
channelAccessToken: process.env.CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.CHANNEL_SECRET
});
const clovaSkillHandler = clova.Client
.configureSkill()
//起動時に喋る
.onLaunchRequest(responseHelper => {
console.log('onLaunchRequest');
responseHelper.setSimpleSpeech(clova.SpeechBuilder.createSpeechText('こんにちは!鳴き声クイズへようこそ。クイズの説明を聞きますか?'));
responseHelper.setReprompt(getRepromptMsg(clova.SpeechBuilder.createSpeechText('クイズの説明を聞きますか?')));
responseHelper.setSessionAttributes({ "question": 0, "correct": 0, "incorrect": 0 });
})
//ユーザーからの発話が来たら反応する箇所
.onIntentRequest(async responseHelper => {
const intent = responseHelper.getIntentName();
var reqSessionAttributes = responseHelper.getSessionAttributes();
console.log('Intent:' + intent);
switch (intent) {
case 'Clova.GuideIntent':
var speech = getGuideSpeech(reqSessionAttributes);
responseHelper.setSpeechList(speech);
responseHelper.setReprompt(getRepromptMsg(clova.SpeechBuilder.createSpeechText(msg)));
break;
case 'Clova.YesIntent':
var speech = getGuideSpeech(reqSessionAttributes);
responseHelper.setSpeechList(speech);
responseHelper.setReprompt(getRepromptMsg(clova.SpeechBuilder.createSpeechText(msg)));
break;
case 'Clova.NoIntent':
// 問題取得
var speech = getQuestionSpeechList(0);
responseHelper.setSpeechList(speech);
responseHelper.setReprompt(getRepromptMsg(clova.SpeechBuilder.createSpeechText(msg)));
break;
case 'QuizIntent':
const slots = responseHelper.getSlots();
console.log(slots);
var currentQuestion = parseInt(reqSessionAttributes.question) || 0;
var nextQuestion = parseInt(currentQuestion) + 1;
var correct = parseInt(reqSessionAttributes.correct);
var incorrect = parseInt(reqSessionAttributes.incorrect);
var speechArray = [];
var speechArrayNextStep = [];
// 正解か不正解
if (jsonData[currentQuestion]["answer"] === slots.animal) {
speechArray = [
clova.SpeechBuilder.createSpeechUrl('https://www.dl.dropboxusercontent.com/s/2vkb4s1q6xm7j1w/correct2.mp3?dl=0'),
clova.SpeechBuilder.createSpeechText('正解です。')
];
// 正解数カウントアップ
correct = parseInt(correct) + 1;
} else {
speechArray = [
clova.SpeechBuilder.createSpeechUrl('https://www.dl.dropboxusercontent.com/s/d6cqnydyfqbdgao/incorrect1.mp3?dl=0'),
clova.SpeechBuilder.createSpeechText('残念、違います。'),
clova.SpeechBuilder.createSpeechText(`答えは${jsonData[currentQuestion]["answer"]}です。`)
];
// 不正解数カウントアップ
incorrect = parseInt(incorrect) + 1;
}
// LINE BOT送信
speechArray.push(clova.SpeechBuilder.createSpeechText(`動物鳴き声クイズボットから、${jsonData[currentQuestion]["answer"]}の詳細情報を送信します。`));
const { userId } = responseHelper.getUser();
sendLineBot(userId, currentQuestion);
// 値保持
responseHelper.setSessionAttributes({
"question": nextQuestion,
"correct": correct,
"incorrect": incorrect
});
if (nextQuestion < MAX_QUESTION) {
// 問題取得
speechArrayNextStep = getQuestionSpeechList(nextQuestion);
} else {
// 結果
if (correct === MAX_QUESTION) {
speechArrayNextStep = [
clova.SpeechBuilder.createSpeechUrl('https://www.dl.dropboxusercontent.com/s/ct1kak7abnjeppu/trumpet1.mp3?dl=0'),
clova.SpeechBuilder.createSpeechText('おめでとうございます!全問正解です。')
];
}else if(correct === 0){
speechArrayNextStep = [
clova.SpeechBuilder.createSpeechText('全問不正解です。')
];
}else{
speechArrayNextStep = [
clova.SpeechBuilder.createSpeechText(`${correct}問正解です。`)
];
}
speechArrayNextStep.push(clova.SpeechBuilder.createSpeechText('またクイズをしに遊びに来てくださいね。'));
responseHelper.endSession();
}
var speech = speechArray.concat(speechArrayNextStep);
responseHelper.setSpeechList(speech);
responseHelper.setReprompt(getRepromptMsg(clova.SpeechBuilder.createSpeechText(msg)));
break;
}
})
//終了時
.onSessionEndedRequest(async responseHelper => {
console.log('onSessionEndedRequest');
})
.handle();
function getRepromptMsg(speechInfo){
const speechObject = {
type: 'SimpleSpeech',
values: speechInfo,
};
return speechObject
}
function getGuideSpeech(reqSessionAttributes){
var helpMsg = [clova.SpeechBuilder.createSpeechText('クイズの説明をします。問題を五つ出題するので、なんの動物か分かったら、動物の名前を言ってくださいね。')];
// 問題取得
var speechArrayQuestion = getQuestionSpeechList(reqSessionAttributes.question);
var speech = helpMsg.concat(speechArrayQuestion);
return speech;
}
function getQuestionSpeechList(questionNo){
var arr = [
clova.SpeechBuilder.createSpeechText(`${questionNo + 1}問目です。`),
clova.SpeechBuilder.createSpeechUrl('https://www.dl.dropboxusercontent.com/s/gecnb42lb1otwzh/question1.mp3?dl=0'),
clova.SpeechBuilder.createSpeechText('この鳴き声は、なんの動物でしょう?'),
clova.SpeechBuilder.createSpeechUrl(jsonData[questionNo]["soundUrl"])
];
return arr;
}
function sendLineBot(userId, questionNo){
client.pushMessage(userId, [
{
type: 'flex',
altText: '正解の動物を送信しました',
contents:
{
"type": "bubble",
"hero": {
"type": "image",
"url": jsonData[questionNo]["imageUrl"],
"size": "full",
"aspectRatio": "20:13",
"aspectMode": "cover",
"action": {
"type": "uri",
"uri": jsonData[questionNo]["imageUrl"]
}
},
"body": {
"type": "box",
"layout": "vertical",
"spacing": "md",
"contents": [
{
"type": "text",
"text": jsonData[questionNo]["answer"],
"size": "xl",
"weight": "bold"
}
]
},
"footer": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "button",
"style": "primary",
"color": "#905c44",
"action": {
"type": "uri",
"label": "詳細を見る",
"uri": jsonData[questionNo]["wikiUrl"]
}
}
]
}
}
}
]).then(() => {
console.log('LINE Success')
}).catch((err) => {
//console.log('LINE Error')
//console.log(err)
})
}
const app = new express();
const port = process.env.PORT || 3000;
const clovaMiddleware = clova.Middleware({ applicationId: process.env.EXTENSION_ID });
app.post('/clova', clovaMiddleware, clovaSkillHandler);
app.listen(port, () => console.log(`Server running on ${port}`));