-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (115 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
const clova = require('@line/clova-cek-sdk-nodejs');
const express = require('express');
const bodyParser = require('body-parser');
// 応答の最後に追加するテンプレート
const TEMPLATE_INQUIRY = '星座を言うか、使い方、もしくは終了と呼びかけて下さい。';
const clovaSkillHandler = clova.Client
.configureSkill()
// スキルの起動リクエスト
.onLaunchRequest(responseHelper => {
responseHelper.setSimpleSpeech({
lang: 'ja',
type: 'PlainText',
value: `「サンプル占い」が起動されました。${TEMPLATE_INQUIRY}`,
});
})
// カスタムインテント or ビルトインインテント
.onIntentRequest(responseHelper => {
const intent = responseHelper.getIntentName();
let speech;
switch (intent) {
// ユーザーのインプットが星座だと判別された場合。
case 'FortuneIntent': {
// 星座を取得
const slots = responseHelper.getSlots()
// スロット名を間違って付けてしまった場合
if (!('zodiac_signs' in slots)) {
speech = {
lang: 'ja',
type: 'PlainText',
value: `想定しないスロット名です。カスタムスロットの名前が正しいかご確認ください。`
}
responseHelper.setSimpleSpeech(speech)
break
}
// Slotに登録されていない星座はnullになる
if(slots.zodiac_signs == null) {
speech = {
lang: 'ja',
type: 'PlainText',
value: `星座に誤りがあります。他の星座でお試し下さい。`
}
responseHelper.setSimpleSpeech(speech)
// 第2引数にtrueを設定するとreprompt(入力が行われなかった場合の聞き返し)の文を定義できる
responseHelper.setSimpleSpeech(speech, true)
// 下記でも可
/*
responseHelper.setSimpleSpeech(
clova.SpeechBuilder.createSpeechText(`星座に誤りがあります。他の星座でお試し下さい。`)
);
*/
break
}
// 「中吉」だと「なかよし」発生されてしまう
const fortune = ['大吉', 'ちゅうきち', '小吉', '吉', '凶']
const zodiacSigns = ['牡羊座', '牡牛座', '双子座', '蟹座', '獅子座', '乙女座', '天秤座', '蠍座', '射手座', '山羊座', '水瓶座', '魚座']
// 日と星座を元に運勢を決定。日が変わると違う運勢に。
const fortuneToday = fortune[(new Date().getDate() + zodiacSigns.indexOf(slots.zodiac_signs)) % fortune.length]
speech = {
lang: 'ja',
type: 'PlainText',
value: `${slots.zodiac_signs}の今日の運勢は${fortuneToday}です。${TEMPLATE_INQUIRY}`
}
responseHelper.setSimpleSpeech(speech)
responseHelper.setSimpleSpeech(speech, true)
break;
}
// ビルトインインテント。ユーザーによるインプットが使い方のリクエストと判別された場合
case 'Clova.GuideIntent':
speech = {
lang: 'ja',
type: 'PlainText',
value: TEMPLATE_INQUIRY
}
responseHelper.setSimpleSpeech(speech)
responseHelper.setSimpleSpeech(speech, true)
//});
break;
// ビルトインインテント。ユーザーによるインプットが肯定/否定/キャンセルのみであった場合
case 'Clova.YesIntent':
case 'Clova.NoIntent':
case 'Clova.CancelIntent':
speech = {
lang: 'ja',
type: 'PlainText',
value: `意図しない入力です。${TEMPLATE_INQUIRY}`
}
responseHelper.setSimpleSpeech(speech)
break;
default:
speech = {
lang: 'ja',
type: 'PlainText',
value: `想定しないインテントです。カスタムインテントの名前が正しいかご確認ください。`
}
responseHelper.setSimpleSpeech(speech)
break;
}
})
// スキルの終了リクエスト
.onSessionEndedRequest(responseHelper => {
})
.handle();
const app = new express();
//TODO
// リクエストの検証を行う場合。環境変数APPLICATION_ID(値はClova Developer Center上で入力したExtension ID)が必須
const clovaMiddleware = clova.Middleware({
applicationId: process.env.APPLICATION_ID
});
app.post('/clova', clovaMiddleware, clovaSkillHandler);
// リクエストの検証を行わない
//app.post('/clova', bodyParser.json(), clovaSkillHandler);
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server running on ${port}`);
});