Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Googleアシスタントアプリのハンズオン資料

1coin
September 20, 2018

 Googleアシスタントアプリのハンズオン資料

ハンズオン資料

@【9/20(木)開催】「『ステップバイステップで力がつく Googleアシスタントアプリ開発入門』発売記念ハンズオン ~AI会話機能アプリを作ってみよう!~」 ~第12回 本とITを研究する会セミナー~ - 本とITを研究する会 | Doorkeeper - https://tech-dialoge.doorkeeper.jp/events/79316

1coin

September 20, 2018
Tweet

More Decks by 1coin

Other Decks in Technology

Transcript

  1. ϋϯζΦϯશମͷྲྀΕ 1. ʮ΍·ͼ͜ʯΞΫγϣϯΛ࡞ͬͯɺ։ൃͷྲྀΕΛ஌Ζ͏ʂ • ! ʲֶशϙΠϯτʳActions on GoogleͱDialogflowͷ࢖͍ํɾ࿈ܞํ๏ 2. Cloud

    Functions for FirebaseΛ࢖ͬͯಈతͳ͡ΌΜ͚ΜήʔϜΛ࡞ͬͯΈΑ͏ʂ • ! ʲֶशϙΠϯτʳΞΫγϣϯͷBEͱͳΔCloud Functions for Firebaseͷ׆༻ํ๏ 3. ΞΫγϣϯͷ༷ʑͳදݱํ๏(GUIɺSSML[߹੒Ի੠ૢ࡞]) • ิ଍ • ॻ੶ͷ2ষޙ൒~3ষલ൒ͱಉ༷ͷྲྀΕͰɺਐΊ͍͖ͯ·͢ɻ • ஫ҙ఺΍ิ଍ࣄ߲Λڧௐ͓ͯ͠࿩͍͚ͯ͠Ε͹ͱɻ © 2018 Shinji Ichien 2
  2. ʮ΍·ͼ͜ʯΞΫγϣϯΛ࡞ͬͯɺ։ൃͷྲྀΕΛ஌Ζ͏ ΞδΣϯμ ࣄલ஌ࣝΠϯϓοτ - ΞΫγϣϯ։ൃͷΤίγεςϜΛ೺Ѳ͠Α͏ʂ ΍Δ͜ͱϦετ 1. Actions on GoogleͰϓϩδΣΫτ࡞੒

    2. Dialogflowͱ࿈ܞͤ͞Α͏ 3. IntentΛ࢖ͬͯɺ΍·ͼ͜ձ࿩Λ࡞Ζ͏ 4. GoogleΞγελϯτ γϛϡϨʔλͰಈ࡞֬ೝͯ͠ΈΑ͏ © 2018 Shinji Ichien 3
  3. 1. Actions on GoogleͰ ϓϩδΣΫτͷ࡞੒ • Actions on GoogleͷDeveloper Console

    ͔ΒɺϓϩδΣΫτΛ࡞੒͠ɺτοϓ ը໘Λදࣔ͢Δ • https://goo.gl/AZTY2y • ʮACTIONS CONSOLEʯϘλϯΛબ୒ © 2018 Shinji Ichien 8
  4. 1. Actions on GoogleͰ ϓϩδΣΫτͷ࡞੒ • Project Name: ೚ҙͰ޷͖ͳͷΛઃఆ •

    σϑΥϧτݴޠ: Japanese • ࠃ: Japan • ࠷ޙʹɺʮCREATE PROJECTʯϘλϯ Λબ୒ © 2018 Shinji Ichien 10
  5. 2. Dialogflowͱ ࿈ܞͤ͞Α͏ Intentͷ঺հ • Dialogflowͱ࿈ܞͤͯ͞ɺձ࿩ϑϩʔ࡞੒ • ձ࿩ϑϩʔߏஙʹ͸ɺIntentͷཧղ͕ඞཁ • Intentͱ͸?

    • Ϣʔβͷ໰͍͔͚ͱιϑτ΢ΣΞଆͷॲཧΛϚοϐϯά͢Δ࢓ ૊Έ • Ϣʔβͱͷձ࿩ͷ࠷খ୯Ґͱߟ͑Δͱྑ͍ ࢀߟ: ʲGoogle HomeରԠʳεςοϓόΠεςοϓͰྗ͕ͭ͘ GoogleΞγελϯτΞϓϦ։ൃೖ໳ʢཬࢁೆਓʗҰԁਅ࣏ʣ | ॻ੶ ຊ | ιγϜ` © 2018 Shinji Ichien 14
  6. 2. Dialogflowͱ ࿈ܞͤ͞Α͏ • දࣔ͞ΕΔμΠΞϩά͔ΒʮCustom IntentʯΛબ୒ • μΠΞϩάͷӈԼͷʮBUILDʯΛબ୒ • ※

    ʮBUILDʯબ୒ޙɺ͓࢖͍ͷGoogle ΞΧ΢ϯτͰDialogflowΛॳΊͯར༻ ͢Δ৔߹͸ɺར༻ن໿ͷ֬ೝμΠΞϩ ά͕දࣔ͞ΕΔɺ಺༰Λ֬ೝͯ͠ ʮACCEPTʯΛબ୒ © 2018 Shinji Ichien 16
  7. Entityͱ͸? • ༻ޠͷࣙॻ (ex: ༵೔ɺ৭ɺ೔ຊͷϩοΫόϯυ໊ɺ͡ΌΜ͚Μͷख) • Parameterͱ૊Έ߹ΘͤΔ͜ͱͰɺࣗવݴޠจ͔ΒΞΫγϣϯͰ൑ผՄೳͳม਺஋Λநग़͢ Δ͜ͱ͕Մೳ छྨ આ໌

    ྫ γεςϜEntity Ұൠͷ֓೦޲͖ʹ Dialogflow͕ఏڙ ༵೔ɺ৭ ΧελϜEntity Ϣʔβ͕ಠࣗʹఆٛ ೔ຊͷϩοΫόϯυ໊ɺ͡Ό Μ͚Μͷख © 2018 Shinji Ichien 23
  8. நग़ͨ͠ϑϨʔζΛ ΍·ͼ͜ͱͯ͠Ԡ౴͢Δ • Text responseʹϢʔβʹԠ౴͢Δ಺༰ Λઃఆ • ࠓճ͸ParameterͰઃఆͨ͠ʮ$echoʯ Λೖྗ •

    ʮ$echoʯʹ͸Ϣʔβ͔ΒͷϑϨʔζ಺ ༰͕ͦͷ··֨ೲ͞Ε͓ͯΓɺ͜ΕΛ Ԡ౴͢Δ͜ͱͰ΍·ͼ͜ػೳΛ࣮ݱ © 2018 Shinji Ichien 26
  9. 4. GoogleΞγελϯτ γϛϡϨʔλͰಈ࡞֬ೝ • ࠷৽ͷDialogflowΤʔδΣϯτΛ Actions on GoogleͷϓϩδΣΫτͱ౷ ߹͢Δඞཁ͕͋Δ •

    ʮIntegrationsʯ͔ΒGoogleΞγελϯ τͷʮINTEGRATIONS SETTINGʯΛબ ୒ © 2018 Shinji Ichien 29
  10. 4. GoogleΞγελϯτ γϛϡϨʔλͰಈ࡞֬ೝ • ʮTESTʯΛબ୒͠ɺActions on Googleͷϓϩ δΣΫτʹΞοϓϩʔυ • ʮUpdating

    Actions on Googleʯจݴදࣔޙɺγ ϛϡϨʔλը໘ʹભҠ ! ʮAuto-preview changesʯ͸ʮONʯʹ͓ͯ͘͠ͱ ྑ͍Ͱ͠ΐ͏ɻ͔͠͠ɺ໌ࣔతʹมߋͨ͠ Dialogflowͷঢ়ଶΛ֬ೝ͢ΔͨΊɺΞϓϦΛࢼ࣌͢ ͸ຖճɺΞοϓϩʔυखॱΛ࣮ࢪ͢Δ͜ͱΛΦεε ϝ͠·͢ʂ © 2018 Shinji Ichien 30
  11. ͜͜·Ͱͷ·ͱΊ • Actions on GoogleͱDialogflowΛ࿈ܞͤͯ͞ձ࿩ϑϩʔΛߏங • Dialogflowͷػೳͷ঺հ • Intent: Ϣʔβͷձ࿩ͱιϑτ΢ΣΞͷॲཧΛϚοϐϯά͢Δɻ஥հ໾ɻ

    • Entity: ༻ޠͷࣙॻ • Parameter: ࣗવձ࿩จ͔Β୯ޠɾݴ༿ͷநग़ɻEntityͱ૊Έ߹Θͤͯར༻ɻ • GoogleΞγελϯτ γϛϡϨʔλΛ࢖ͬͯΞϓϦͷಈ࡞֬ೝɾσόοά © 2018 Shinji Ichien 33
  12. Cloud Functions For Firebaseͱ͸? • HTTPϦΫΤετΛτϦΨʔʹͯ͠ɺ αʔόϨεͰ೚ҙͷॲཧΛ࣮ߦ • Firebaseͱ౷߹͞Ε͓ͯΓɺ࿈ܞ͕༰ қ

    • Firebase Realtime Databaseɺ Firebase Authentication etc.. • Dialogflowͱ͸ Intent໊ Ͱ࿈ܞ © 2018 Shinji Ichien 35
  13. ͡ΌΜ͚ΜήʔϜͷઆ໌ • ͡ΌΜ͚Μͷखʹର͠ɺಈతʹCloud Functions ଆͰखΛܾΊɺউͪෛ͚Λ൑அ͢Δ • Intent঺հ Intent໊ ໾ׂ Match

    ͡ΌΜ͚ΜήʔϜͷॲ ཧɻ͡ΌΜ͚Μͷखͱ উഊ൑அɻ Default Welcome Intent ΞϓϦىಈ࣌ʹ࢖͍ํ ΍આ໌Λ͢Δɻ © 2018 Shinji Ichien 36
  14. ͡ΌΜ͚ΜήʔϜͷDialogflowΤʔδΣϯτΛ෮ݩ͢Δ ʲ1/3ʳDialogflowΤʔδΣϯτͷόοΫΞοϓϑΝΠϧͷμ΢ϯϩʔυ • αϯϓϧΞϓϦͷGithub(https://goo.gl/dm8jd3) ͔Β sample-3-4.zipΛμ΢ϯϩʔυ • ֤αϯϓϧΞϓϦͷσΟϨΫτϦͷߏ଄ <ϓϩδΣΫτϧʔτ> +-

    dialogflow/ # DialogflowͷΤʔδΣϯτͷόοΫΞοϓϑΝΠϧΛ഑ஔ͢ΔσΟϨΫτϦ | | | +- xxx.zip # DialogflowͷΤʔδΣϯτͷόοΫΞοϓϑΝΠϧ | +- functions/ # ʲಈతͷΈʳCloud Functions for Firebaseͷιʔείʔυ͕ೖΔσΟϨΫτϦ | +- .eslintrc.json # JavaScriptͷߏจνΣοΫπʔϧESLintͷઃఆϑΝΠϧ | +- package.json # ར༻͢Δ֎෦πʔϧ΍ϥΠϒϥϦͳͲΛઃఆ͢ΔϑΝΠϧ | +- index.js # functiosͷϝΠϯίʔυ © 2018 Shinji Ichien 38
  15. ੩తͳ͡ΌΜ͚ΜήʔϜΛ ಈతʹมߋ • Ϣʔβ͔Βͷ͡ΌΜ͚ΜखΛCloud FunctionsʹૹΓɺউഊ൑அͷॲཧΛҕৡ • DialogflowͷʮFullfillmentʯ߲໨͔Βઃఆ ΍Δ͜ͱ 1. Cloud

    FunctionsͰΞΫγϣϯͷBEߏங 2. Match IntentͷFullfillmentΛONʹ 3. Fullfillmentʹ1.Ͱ࡞੒ͨ͠BEͷΤϯυϙ ΠϯτΛઃఆ © 2018 Shinji Ichien 44
  16. 1. Cloud FunctionsͰΞΫγϣϯͷBEߏங • ࠓճ͸ॻ੶ͷαϯϓϧίʔυΛར༻͠·͢(https://goo.gl/QrghFV) • ୹ॖલ: https://github.com/google-assistants-apps-book/samples/tree/master/3-5_ಈత ॲཧ/functions $

    git clone https://github.com/google-assistants-apps-book/samples.git $ cd samples/3-5_ಈతॲཧ $ tree functions functions !"" index.js # functionsͷϝΠϯίʔυɻ͡ΌΜ͚ΜϩδοΫɻ !"" constants.js # index.jsͰࢀর͢Δఆ਺Λ·ͱΊͨϑΝΠϧ !"" firebase.json # ϓϩδΣΫτͷϏϧυ࣌ͷઃఆ etc !"" package-lock.json # Versionݻఆͨ͠ґଘϥΠϒϥϦͷ؅ཧϑΝΠϧ #"" package.json # ґଘϥΠϒϥϦͷ؅ཧϑΝΠϧ © 2018 Shinji Ichien 46
  17. index.jsͷղઆ ᶃ • line: 4-13 // DialogflowΛ؅ཧ͢Δ֎෦ϞδϡʔϧͷಡΈ͜Έ const { dialogflow

    } = require('actions-on-google'); // Cloud functionsΛ؅ཧ͢Δ֎෦ϞδϡʔϧͷಡΈ͜Έ const functions = require('firebase-functions'); // ఆ਺ϞδϡʔϧͷಡΈࠐΈ const constants = require('./constants.js'); // DialogflowͷΞϓϦέʔγϣϯΠϯελϯεΛੜ੒ const app = dialogflow({ debug: true }); © 2018 Shinji Ichien 47
  18. index.jsͷղઆ ᶄ • line: 20-49 // Match IntentΛ؂ࢹ app.intent('Match', (conv)

    => { // ϢʔβʔͷखΛऔಘɻDialogflowͷParameter໊ let userHandType = conv.parameters['handType']; // ------ ͜͜ʹҟৗܥͷॲཧ͕͋Δ ------ // ΞϓϦͷखΛܾఆɻϥϯμϜͰબ୒͍ͯ͠Δɻ const appHandType = constants.VALID_HAND_TYPE[Math.floor(Math.random() * constants.VALID_HAND_TYPE.length)]; // উഊΛܾఆ const matchType = constants.MATCH_RESULT[userHandType][appHandType]; // উഊผͷॲཧ const resultMessage = constants.SPEAK_MESSAGE_RESULT[matchType].replace("%HAND", appHandType); // Ϩεϙϯε conv.ask(resultMessage); }); © 2018 Shinji Ichien 48
  19. Cloud FunctionsΛJavaScriptͰ։ൃ͢ΔͨΊͷ؀ڥΛ੔͑Δ ໊લ OS આ໌ nodebrew macOS Node.jsͷόʔδϣϯΛ؅ཧɾ੾ ସͰ͖Δπʔϧ nodist

    Windows Node.jsͷόʔδϣϯΛ؅ཧɾ੾ ସͰ͖Δπʔϧ npm macOS/Windows Node.jsͷύοέʔδ؅ཧπʔϧ Firebase CLI macOS/Windows FirebaseϓϩδΣΫτͷ؅ཧɺ දࣔɺσϓϩΠΛ࣮ߦ͢ΔͨΊ ͷπʔϧ © 2018 Shinji Ichien 51
  20. ʲmacOS޲͚ʳNode.js, npmͷΠϯετʔϧ # nodebrewެࣜΑΓɺҎԼͷίϚϯυΛ࣮ߦ $ curl -L git.io/nodebrew | perl

    - setup # pathΛ௨͢ɻ ex: bash ======================================== Add path: export PATH=$HOME/.nodebrew/current/bin:$PATH ======================================== # ௥Ճͨ͠pathΛಡΈࠐΉ $ source ~/.bash_profile $ nodebrew help nodebrew 1.0.0 Usage: <ҎԼলུ> © 2018 Shinji Ichien 52
  21. ʲmacOS޲͚ʳNode.js, npmͷΠϯετʔϧ # ར༻Ͱ͖ΔόʔδϣϯҰཡΛ֬ೝ $ nodebrew ls-remote # ࠓճ͸Node.js6Λ࢖͍·͢ɻ˞࠷ۙɺCloud Functions͕8΋αϙʔτɻ

    $ nodebrew install-binary v6.14.0 # όʔδϣϯ੾Γସ͑ $ nodebrew use v6.14.0 $ npm -v © 2018 Shinji Ichien 53
  22. ʲWindows޲͚ʳNode.js, npmͷΠϯετʔϧ • nodist͸ެࣜϖʔδ͔Β.exeϑΝΠϧΛμ΢ϯϩʔυ͠ɺखॱʹ͕ͨͬͯ͠ɺΠϯε τʔϧɻ • ShellͰόʔδϣϯ֬ೝ͠·͠ΐ͏ɻ • Node.jsΛΠϯετʔϧ #

    ར༻Ͱ͖ΔόʔδϣϯΛ֬ೝ $ nodist dist | Select-String 6.14.0 $ nodist + 6.14.0 # όʔδϣϯ੾Γସ͑ $ nodist 6.14.0 # Node.jsʹ߹͏npmΛΠϯετʔϧ $ nodist npm match © 2018 Shinji Ichien 54
  23. Firebase CLIΛΠϯετʔϧ # -g ͰσόΠε಺Ͱάϩʔόϧʹར༻Ͱ͖Δ $ npm install -g firebase-tools

    $ firebase -V # Πϯετʔϧ͕׬ྃͨ͠ΒɺfirebaseʹϩάΠϯ͢Δ $ firebase login © 2018 Shinji Ichien 55
  24. Firebase initͰCloud FunctionsϓϩδΣΫτΛॳظઃఆ # ./samples/3-5_ಈతॲཧ σΟϨΫτϦʹͯ $ firebase init functions

    # ࿈ܞ͢ΔFirebaseϓϩδΣΫτIDΛબ୒͠ɺEnterԡԼʂ # Dialogflowͷઃఆ͔Β֬ೝͰ͖Δɻ ⚙ ઃఆ->Geneal->ʮGOOGLE PROJECTʯ->ʮProject IDʯ ? Select a default Firebase project for this directory: Sign-in-only (sign-in-only) speak-transcribe (speak-transcribe) Call-My-Location (call-lat-lon) › Rock-Paper-Scissors-Game-2 (rock-paper-scissors-game-2) ... © 2018 Shinji Ichien 57
  25. Firebase initͰCloud FunctionsϓϩδΣΫτΛॳظઃఆ # αϯϓϧ͕JsͳͷͰɺJavaScriptΛબ୒͠ɺEnterԡԼʂ ? What language would you

    like to use to write Cloud Functions? (Use arrow keys) › JavaScript TypeScript # ্ॻ͖͠ͳ͍ͷͰɺ͢΂ͯ ʮNʯ ͱೖྗ͠ɺEnterԡԼʂ ? Do you want to use ESLint to catch probable bugs and enforce style? No ? File functions/package.json already exists. Overwrite? No i Skipping write of functions/package.json ? File functions/index.js already exists. Overwrite? (y/N) N # firebaseͷϓϩδΣΫτ৘ใ͕͕อ࣋͞Ε͍ͯΔӅ͠ϑΝΠϧ͕࡞੒͞Ε͍ͯΔ $ cat .firebaserc { "projects": { "default": "rock-paper-scissors-game-2" } } © 2018 Shinji Ichien 58
  26. ʲ͓·͚ʳݱࡏͷFirebaseϓϩδΣΫτͷ֬ೝɾߋ৽ # ֬ೝ $ firebase use Active Project: rock-paper-scissors-game-2 Project

    aliases for /xxxx/samples/3-5_ಈతॲཧ: * default (rock-paper-scissors-game-2) Run firebase use --add to define a new project alias. # มߋ͍ͨ͠৔߹͸ͪ͜ΒɻaliasΛ͚ͭΔ͜ͱ΋Ͱ͖Δ $ firebase use --add © 2018 Shinji Ichien 59
  27. ιʔείʔυΛσϓϩΠ # functionssσΟϨΫτϦ௚Լ(package.json͕͋Δॴ)Ͱɺؔ࿈͢ΔϥΠϒϥϦΛΠϯετʔϧ $ cd functions $ npm install #

    σΟϨΫτϦΛҠಈͯ͠ $ cd .. $ ls dialogflow firebase.json functions # functionsσΟϨΫτϦΛ--onlyͰࢦఆͯ͠ɺσϓϩΠɻ # දࣔ͞ΕͨΤϯυϙΠϯτͷURLΛFirebaseίϯιʔϧ͔Β֬ೝ $ firebase deploy --only functions ... ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/rock-paper-scissors-game-2/overview © 2018 Shinji Ichien 60
  28. ιʔείʔυΛσϓϩΠ ࣦഊ͢Δ৔߹ɺࢼͯ͠ΈΑ͏ -ɹʮError: An unexpected error has occurred.ʯͱΤϥʔ͕ग़Δ৔ ߹ɺ࠶౓࣮ߦ͍ͯͩ͘͠͞ɻ -ɹWindows؀ڥͰΤϥʔʹͳΔ৔߹ɺfirebase.jsonΛॻ͖׵͑Δ

    # ͜ΕΛ $ npm --prefix \"$RESOURCE_DIR\" run lint # ↓ͷ༷ʹॻ͖׵͑Δ $ npm --prefix \"%RESOURCE_DIR%\" run lint © 2018 Shinji Ichien 61