Skip to content

Commit a2cc97e

Browse files
committed
shiritori: update to actionssdk v2
- update to actionssdk - add action package - remove dialogflow Bug: 77879750 Bug: 77880347 Change-Id: I8a17c47976448f96ac1ab8cc18747604ddeccdd0
1 parent 82548ef commit a2cc97e

File tree

4 files changed

+66
-37
lines changed

4 files changed

+66
-37
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
## 設定
44

5-
1.[Dialogflow](https://dialogflow.com/)」のプロジェクトを作成する。
6-
1. 「Firebase Cloud Functions」のfullfillmentをつかう。
5+
1.[Actions on Google](https://console.actions.google.com/)」のプロジェクトを作成する。
6+
1. 「Action package」をアープデートする:
7+
8+
gactions update --action_package action.json --project PROJECT_NAME
9+
710
1. [edict2](http://www.edrdg.org/jmdict/edict_doc.html)の辞書をダウンロードする。
811
1. utf-8に変換する:
912

@@ -24,7 +27,7 @@
2427
firebase deploy
2528

2629
1. [Cloud Console](https://console.cloud.google.com)`shiritori` のRAMを512MBまで上げる。
27-
1. Actions on Google Simulator」でロケールを日本語に設定する。
30+
1. [Actions on Google](https://console.actions.google.com/)Simulator」でロケールを日本語に設定する。
2831
1. アシスタントアプリをテストしてみる。
2932

3033
> テスト用アプリにつないで

action.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"locale": "ja",
3+
"actions": [
4+
{
5+
"name": "MAIN",
6+
"intent": {
7+
"name": "actions.intent.MAIN",
8+
"trigger": {
9+
"queryPatterns": [
10+
"しりとり",
11+
"しりとりで遊ぼう",
12+
"しりとりであそぼう",
13+
"しりとりで遊びたい",
14+
"しりとりであそびたい"
15+
]
16+
}
17+
},
18+
"fulfillment": {
19+
"conversationName": "shiritori-v2"
20+
}
21+
}
22+
],
23+
"conversations": {
24+
"shiritori-v2": {
25+
"name": "shiritori-v2",
26+
"url": "https://us-central1-shiritori-internal.cloudfunctions.net/shiritoriV2",
27+
"fulfillmentApiVersion": 2
28+
}
29+
}
30+
}

functions/index.js

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if (!Object.entries) {
1919
entries.shim();
2020
}
2121

22-
const { DialogflowApp } = require('actions-on-google');
22+
const { actionssdk, SimpleResponse } = require('actions-on-google');
2323
const functions = require('firebase-functions');
2424
const shiritori = require('./shiritori');
2525
const admin = require('firebase-admin');
@@ -34,50 +34,46 @@ const dict = k => {
3434
.then(snap => snap.val());
3535
};
3636

37-
function welcomeHandler (app) {
38-
app.ask('どうぞ、始めて下さい');
39-
}
37+
const app = actionssdk({
38+
debug: true,
39+
init: () => ({
40+
data: {
41+
used: []
42+
}
43+
})
44+
});
4045

41-
function gameHandler (app) {
46+
app.intent('actions.intent.MAIN', (conv) => {
47+
conv.ask('どうぞ、始めて下さい');
48+
});
49+
50+
app.intent('actions.intent.TEXT', (conv, input) => new Promise((resolve, reject) => {
4251
shiritori.loaded.then(() => {
43-
const input = app.getRawInput();
44-
shiritori.interact(dict, input, app.data.used, {
52+
shiritori.interact(dict, input, conv.data.used, {
4553
lose () {
46-
app.tell('ざんねん。あなたの負けです。');
54+
conv.close('ざんねん。あなたの負けです。');
55+
resolve();
4756
},
4857
win (word, kana) {
4958
if (word) {
50-
app.tell(`${word} [${kana}]`);
59+
conv.close(`${word} [${kana}]`);
60+
resolve();
5161
} else {
52-
app.tell('すごい!あなたの勝ちです。');
62+
conv.close('すごい!あなたの勝ちです。');
63+
resolve();
5364
}
5465
},
5566
next (word, kana) {
56-
app.data.used.unshift(input);
57-
app.data.used.unshift(word);
58-
app.ask({
67+
conv.data.used.unshift(input);
68+
conv.data.used.unshift(word);
69+
conv.ask(new SimpleResponse({
5970
speech: word,
60-
displayText: `${word} [${kana}]`
61-
});
71+
text: `${word} [${kana}]`
72+
}));
73+
resolve();
6274
}
6375
});
6476
});
65-
}
77+
}));
6678

67-
function defaultHandler (app) {
68-
app.tell('しりとりのゲーム');
69-
}
70-
71-
const actionMap = new Map();
72-
actionMap.set('input.welcome', welcomeHandler);
73-
actionMap.set('input.unknown', gameHandler);
74-
actionMap.set('default', defaultHandler);
75-
76-
exports.shiritori = functions.https.onRequest((request, response) => {
77-
const app = new DialogflowApp({request, response});
78-
app.data.used = app.data.used || [];
79-
console.log('Request headers: ' + JSON.stringify(request.headers));
80-
console.log('Request body: ' + JSON.stringify(request.body));
81-
app.handleRequest(actionMap);
82-
});
83-
exports.shiritori.actionMap = actionMap;
79+
exports.shiritoriV2 = functions.https.onRequest(app);

functions/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"test": "npm run lint"
1616
},
1717
"dependencies": {
18-
"actions-on-google": "^1.5.x",
18+
"actions-on-google": "^2.0.x",
1919
"firebase-admin": "^4.2.1",
2020
"firebase-functions": "^0.8.1",
2121
"kuroshiro": "^0.2.1",

0 commit comments

Comments
 (0)