LoginSignup
0
3

More than 1 year has passed since last update.

GoogleHomeアプリ「うんこカウンター」をNode-RED(IBMCloud)で作った

Last updated at Posted at 2021-01-09

はじめに

もう2年前ですが、GoogleHomeアプリ「うんこカウンター」
Google Assistant+Node-RED(IBMCloud)で作成しました。
https://assistant.google.com/services/a/uid/000000b4699e0c63?hl=ja-JP
unko.png

この音声アプリはユーザーが喋った「うんこ」の数をカウントするだけのアプリですが
当時リリースするや否や爆発的アクセスで、バックグラウンドのNode-REDが大丈夫か?
と危ぶまれたものですが、とりあえず現在もなんとか稼働してます。
プログラミング初心者が右も左もわからぬままコツコツ作ったアプリですので色々変なところ満載かと思いますが備忘録的に晒しておきます。

紹介記事の紹介

「うんこカウンター」の遊び方とか色々紹介して頂いてます。マジ有難い記事ですので是非お読みください!
GoogleHome:名前で判断してはいけない良作!
「う○こカウンター」・・・・・・伏せ字にしましたが「うんこ」です。
https://sumasupi.net/2018/04/18/unko-counter/

Actions on Googleの説明

この音声アプリのプロジェクトはActions on GoogleDialogflowに作成いたします
詳しい設定方法の説明は省きますが以下の設定のスクショを参考にしてみて下さい

Actions on Googleの設定

1.png

2.png

3.png

Dialogflowの設定

4.png

5.png

6.png

7.png

8.png

9.png

10.png

11.png

12.png

アプリのバックグラウンド:Node-REDフロー

Google Assistantの音声認識出力(テキストデータ)をNode-RED(IBM Cloud)で処理しています
Node-REDでの基本的な処理は、初回処理通常処理終了処理の3通りです
GoogleHome/Assistantに向かってユーザーが「OK Google うんこカウンターにつないで」と喋りかけると初回処理に分岐されます。
それ以降は通常処理に分岐され「うんこ」の数をカウントし、効果音、音楽、カウント数に応じたコメントを返します。
最後にユーザーが「終わり」「やめて」などと喋ると終了処理に分岐され、最終コメントや効果音を返します。

flow1.png
Switchノードで処理を振り分けます
switchノード.png

効果音(SE)やBGM用のmp3ファイルを用意します。

ここが面倒と言えば面倒なのですが、音声アプリの醍醐味はSoundへの拘りだと思いますので頑張って下さい
GoogleAssistantで再生可能なファイルフォーマットは規定があります
通常の音楽用のHiFiなフォーマットとかだとNGです
https://developers.google.com/assistant/conversational/df-asdk/ssml#audio

また、GoogleAssistant用に予め用意された膨大なライブラリもありますので、こちらを利用できます
https://developers.google.com/assistant/tools/sound-library/cartoon

mp3ファイルをデータベースにアップロード

自作したmp3ファイルはCloudantデータベースに格納します
file_upload.png
手作業で1個づつファイルをアップロードしてください
file_upload2.png

アップロードしたファイルの確認

アップロードしたファイルは以下の操作で確認/再生できますのでリンク先URLをメモしておいていて下さい
file_check.png

データベースのパーミションの設定

mp3ファイルを読み出せるようにパーミションを変更します
permission.png

データベース(mp3)からのファイル読み出しを許可する

_readerにチェックを入れます
permission2.png

初回処理

ユーザーの「OK Google うんこカウンターにつないで」で初回だけ実行される処理です
前回保存されたスコアを取得するようになっていますが、初回では無意味ですね
初回は操作方法の説明メッセージが必要です(公開リリース審査でチェックされます)

//functionノード(初回処理)
//ユーザーストレージからユーザーのスコアを取得 無ければ生成
if(msg.payload.originalDetectIntentRequest.payload.user.userStorage){
    if(isNaN(msg.payload.originalDetectIntentRequest.payload.user.userStorage)){
        msg.userStorage = JSON.parse(msg.payload.originalDetectIntentRequest.payload.user.userStorage);
    }else{
        msg.userStorage = {"score":Number(msg.payload.originalDetectIntentRequest.payload.user.userStorage)};
    }

}else{
    msg.userStorage = {"score":0};
}
var text1 = "";
var text2 = "";
var url = 'https://xxxxxxxxxxxxxxxxxxxxxxx-bluemix.cloudant.com/mp3/SE/';
var unko_open = '<audio src="' + url + 'UNKO_OPEN.mp3"></audio>';
var beep1 = '<audio src="' + url + 'BEEP1.mp3"></audio>';
var music = '<audio src="https://xxxx.mybluemix.net/music.mp3"></audio>';

//--------------------- OPENING MASSAGE(0点のユーザー)-----------------------------
if ( msg.userStorage.score < 2 ){

    text1 ="うんこカウンターへようこそ!。。このアプリは、あなたが喋った文章を分析して、うんこの数をカウントします。。";
    text2 = "終わるときは、終わると言ってくださいね。。ビープ音が鳴ってから喋ってください。。それでは勇気を出して、スタート!";
    msg.payload = {
        "fulfillmentText": '<speak><prosody rate="fast" pitch="medium">' +
                            unko_open + text1 + text2 + beep1 + '</prosody></speak>',
        "payload" : {
            "google" : {
                "userStorage" : JSON.stringify(msg.userStorage)
                        }
                    }                    
    };
    msg.tweet = text1 + text2;
    return msg; 
}
//--------------------- OPENING MASSAGE(1点以上のユーザー)-----------------------------
if ( msg.userStorage.score ){
    text1 ="お帰りなさいませ!。。お待ちしておりました。。前回までの自己ベストは、";
    text2 ="点でしたっけ。。すみません忘れちゃいました。。それでは気合を入れて。スタート!";   
    msg.payload = {
        "fulfillmentText": '<speak><prosody rate="fast" pitch="medium">' +
            music + text1 + msg.userStorage.score + text2 + beep1 + '</prosody></speak>',
        "payload" : {
            "google" : {
                "userStorage" : JSON.stringify(msg.userStorage)
                        }
                    }   
    };
    msg.tweet = text1 + msg.userStorage.score + text2;
    return msg; 
}

通常処理

前回で保存したスコアを取得します。「うんこ」の数をカウントして現在のスコアを超えたらスコアを更新し保存します
スコアに応じて色々コメントをしましょう!

//functionノード(通常処理)
//ユーザーのスコアを取得
if(msg.payload.originalDetectIntentRequest.payload.user.userStorage){
    msg.userStorage = JSON.parse(msg.payload.originalDetectIntentRequest.payload.user.userStorage);
}else{
    msg.userStorage = {"score":0};
}

var text = msg.payload.queryResult.queryText
var text1 = ''
var text2 = ''
var text3 = ''
var sound1 = '<break time="1s"/>'
var url = 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-bluemix.cloudant.com/mp3/SE/'
var beep1 = '<audio src="' + url + 'BEEP1.mp3"></audio>'
var wash = '<audio src="' + url + 'WASH.mp3"></audio>'
//var wash ='<audio src="https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-bluemix.cloudant.com/mp3/music/LAST_MESSAGE.mp3"></audio>'



//---------------- UNKO COUNTER CENTER ------------------------
//はんこ あんこ わんこ (漢字に変換されちゃうものは回避)
var unko = 0;
if(text.match(/うんこ/gm)){
    unko = text.match(/うんこ/gm).length;
}
if(text.match(/ウンコ/gm)){
    unko = unko + text.match(/ウンコ/gm).length;
}
if(text.match(/うんち/gm)){
    unko = unko + text.match(/うんち/gm).length;
}
if(text.match(/ウンチ/gm)){
    unko = unko + text.match(/ウンチ/gm).length;
}
if(text.match(/あんこ/gm)){
    unko = unko + text.match(/あんこ/gm).length;
}
if(text.match(/わんこ/gm)){
    unko = unko + text.match(/わんこ/gm).length;
}
if(text.match(/ハンコ/gm)){
    unko = unko + text.match(/ハンコ/gm).length;
}
if(text.match(/はんこ/gm)){
    unko = unko + text.match(/はんこ/gm).length;
}
//------------- NGワード -----------------
if(text.match(/ちんこ/gm)){
    unko = unko - text.match(/ちんこ/gm).length;
    text3 = "。NGワードで減点がありました。"
}
if(text.match(/まんこ/gm)){
    unko = unko - text.match(/まんこ/gm).length;
    text3 = "。NGワードでマイナスされてます。"
}

//-----------------自己ベスト更新-----------------------
if (msg.userStorage.score < unko){
    msg.userStorage.score = unko;
    text2 = 'おめでとうございます!自己ベスト更新です。';
    sound1 =  '<audio src="https://xxxx.mybluemix.net/music.mp3"></audio>'
}

//-----------------応援メッセージ-----------------------    
if (unko === 0){
    text = text + '。。うんこが検出されませんでした。';
    text3 = '頑張ってくださいね!';
} 
if (unko === 2){
    text2 =text2 + 'うん、この調子!';
} 
if (unko === 3){
    text2 =text2 + '頑張って下さいね。まだまだ行けます。';
}    
if (unko > 4){
    text = text + '。。。凄い!';
}
if (unko > 9){
    text = text + "。これは凄い!";
} 
if (unko > 14){
    text = text + "。かなり来てる!";
}
if (unko > 30){
    text = text + "。超やばい!";
} 
if (unko > 19){
    text2 ="。まだまだいけます。頑張って";
} 
if (unko > 24){
    text2 ="。しかしまさか、ココまで来るなんて!";
}
if (unko > 29){
    text2 ="。もう想定外ですので、応援コメントもここまで!";
}
if (unko > 34){
    text2 ="。想定外のさらに想定外!";
}
if (unko > 39){
    text2 ="。実は、まだ上の点数が狙えるらしい!";
}
if (unko > 49){
    text2 ="。そろそろスコアの限界が近いかも。フリーズとかするかも知れませんので覚悟して下さいね。";
}
if (unko > 59){
    text2 ="。ここまで来るのは不可能と思われていたのだが、よくぞ来ましたね!";
}
if (unko > 69){
    text2 ="。ここまで頑張ったあなたは神です。";
}
if (unko > 79){
    text2 ="。あと少しで完全制覇です。頑張って!!";
}
if (unko > 84){
    text2 ="。ついに完全制覇です。。。誠に申し訳ありませんが、これ以上はシステムが応答しなくなるとの報告がありましたので、ご了承ください。";
}
//---------------- SE音声データURL取得 -------------
var SE = flow.get("SE");
//---------------- RANDOM
var rnd = Math.round( Math.random()*30 );
msg.payload={
    "fulfillmentText": '<speak><prosody rate="fast" pitch="medium">' +
        beep1 + text +    //-----------------復唱
        '<audio src="'+ SE[rnd] + '"></audio>'+    //------効果音
        'カウント数は'+ unko +'点。。'+ sound1 + text2 +  text3 + beep1 + //次の入力開始合図のピッ!
        '</prosody></speak>',
    "payload" : {
        "google" : {
            "userStorage" : JSON.stringify(msg.userStorage)
                    }
                } 
    }
    msg.tweet = "カウント数は" + unko + "点。" + text2 + text3
return msg;

終了処理

ユーザーの「おわり」「やめて」等でこの終了処理をします

//functionノード(終了処理)
//ユーザースコア取得
msg.userStorage = JSON.parse(msg.payload.originalDetectIntentRequest.payload.user.userStorage)

var text = msg.payload.queryResult.queryText
var text1 = ''
var text2 = ''
var text3 = ''
var url = 'https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-bluemix.cloudant.com/mp3/SE/'
var beep1 = '<audio src="' + url + 'BEEP1.mp3"></audio>'
var wash = '<audio src="' + url + 'WASH.mp3"></audio>'

//--------------------- ENDING MESSAGE 1 -----------------------------
if(msg.userStorage.score < 41 ){
    text1 ="どうもお疲れ様でした!。。今回の自己ベストは、";
    text2 ="点です。。次回また記録更新を狙いましょう。。それではまた!";    
    msg.payload = {
        "fulfillmentText" : '<speak><prosody rate="fast" pitch="medium">'+ text1 + msg.userStorage.score + text2 + wash + '</prosody></speak>',
        "payload" : {
            "google" : {
                "userStorage" : JSON.stringify(msg.userStorage)
                        }
                    }    
    }
    msg.tweet = text1 + msg.userStorage.score + text2
    return msg; 
}
//--------------------- ENDING MESSAGE 2 -----------------------------
if(msg.userStorage.score < 85 ){
    text1 ="いやはや、本当にお疲れ様でした!。。現在の自己ベストはなんと、";
    text2 ="点に到達いたしました。。この調子でがんばりましょう!。。。因みにリセットすれば最初からまた楽しめます。";    
    msg.payload = {
    "fulfillmentText" : '<speak><prosody rate="fast" pitch="medium">'+ text1 + msg.userStorage.score + text2 + wash + '</prosody></speak>',
    "payload" : {
        "google" : {
            "userStorage" : JSON.stringify(msg.userStorage)
                    }
                } 
            }
    msg.tweet = text1 + msg.userStorage.score + text2
    return msg; 
}
//--------------------- FINAL MESSAGE (スペシャルmp3再生でTTSはなし)-----------------------------
if(msg.userStorage.score > 84 ){
    text1 ="いやはや、本当にお疲れ様でした!。。ついに自己ベストが、";
    text2 ="点に、到達いたしました。。。思い起こせば、長く苛酷な戦いでしたね。。。そしてついに今、栄光のゴールを迎えました。。。この栄誉は永遠に語り継がれるでしょう。。。それでは、ごきげんよう。さようなら!";    
    msg.payload = {
    "fulfillmentText": '<speak><prosody rate="fast" pitch="medium">' +
        '<audio src="https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxx-bluemix.cloudant.com/mp3/music/LAST_MESSAGE.mp3"></audio>' +
        '</prosody></speak>',
    "payload" : {
        "google" : {
            "userStorage" : JSON.stringify(msg.userStorage)
                    }
                }           
    }
    msg.tweet = text1 + msg.userStorage.score + text2
   return msg; 
}

効果音のURL初期設定

お気に入りの効果音をサウンドライブラリの中から選んで、URLをここで纏めて設定しています
後で別の効果音に変更しても良いですね!

//functionノード(デプロイ時初期化)
var SE =[
"https://actions.google.com/sounds/v1/sports/metal_bat_hits_baseball.ogg",  //野球ヒット
"https://actions.google.com/sounds/v1/animals/tentacle_flop_down.ogg",  //鈍い音
"https://actions.google.com/sounds/v1/cartoon/cartoon_boing.ogg",   //ぼいーん
"https://actions.google.com/sounds/v1/cartoon/cartoon_cowbell.ogg", //Cartoon Cowbell
"https://actions.google.com/sounds/v1/cartoon/clang_and_wobble.ogg",    //Clang and wobble
"https://actions.google.com/sounds/v1/cartoon/cartoon_boing.ogg",   //Cartoon Boing
"https://actions.google.com/sounds/v1/cartoon/hair_ripping.ogg",    //Hair Ripping
"https://actions.google.com/sounds/v1/cartoon/instrument_strum.ogg",    //Instrument Strumギター
"https://actions.google.com/sounds/v1/cartoon/metal_twang.ogg", //Metal Twang三味線
"https://actions.google.com/sounds/v1/impacts/stone_drop.ogg",  //カップイン
"https://actions.google.com/sounds/v1/cartoon/pop.ogg", //ポン!
"https://actions.google.com/sounds/v1/sports/basketball_shot_rim_short.ogg",    //スポーツ
"https://actions.google.com/sounds/v1/cartoon/slap_with_glove.ogg", //HandCrap
"https://actions.google.com/sounds/v1/impacts/stones_and_water_on_cement.ogg",  //セメント
"https://actions.google.com/sounds/v1/cartoon/slapping_three_faces.ogg",    //タタタ
"https://actions.google.com/sounds/v1/cartoon/wood_plank_flicks.ogg",   //木琴ドレミ
"https://actions.google.com/sounds/v1/emergency/emergency_radio_alert.ogg", //ラジオDJ
"https://actions.google.com/sounds/v1/emergency/emergency_siren_short_burst.ogg",   //車アラーム
"https://actions.google.com/sounds/v1/impacts/dumpster_door_hit.ogg",   //鉄の扉
"https://actions.google.com/sounds/v1/impacts/crash_metal_sweetener_distant.ogg",   //発射
"https://actions.google.com/sounds/v1/impacts/debris_hits.ogg", //ガラス破壊
"https://actions.google.com/sounds/v1/impacts/dumpster.ogg",    //金属叩く
"https://actions.google.com/sounds/v1/impacts/glass_drop_and_roll.ogg", //空き瓶
"https://actions.google.com/sounds/v1/impacts/object_toss_and_smash.ogg",   //物音
"https://actions.google.com/sounds/v1/impacts/plastic_board_falling_over.ogg",  //物音
"https://actions.google.com/sounds/v1/impacts/fridge_door_fall_open.ogg",   //ドア破壊
"https://actions.google.com/sounds/v1/impacts/metal_crash.ogg", //破壊
"https://actions.google.com/sounds/v1/impacts/small_glass_pane_shatter.ogg",    //ガラス飛び散り
"https://actions.google.com/sounds/v1/impacts/glass_windows_breaking.ogg",  //壁破壊
"https://actions.google.com/sounds/v1/impacts/glass_windows_crashing.ogg",  //破壊
]
flow.set("SE",SE)
msg.payload = SE
return msg;

Actions on Googleのdeploy設定

Testで動作チェックして問題なければアプリを一般公開しましょう!
Deployの設定は以下
13_deploy.png

14_deploy.png

15_deploy.png

16_deploy.png

17_deploy.png

18_deploy.png

19_deploy.png

20_deploy.png

21_deploy.png

まとめ

説明不足いっぱいな感じで大変申し訳ないですがとりあえず公開させて頂きました
随時更新したいと思います(更新2021/09/16)

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3