LoginSignup

This article is a Private article. Only a writer and users who know the URL can access it.
Please change open range to public in publish setting if you want to share this article with other users.

More than 3 years have passed since last update.

Firebase経由でGoogle Apps ScriptからGoogle Homeを喋らせよう

Last updated at Posted at 2019-05-21
1 / 16

Firebaseと連携する


新規プロジェクトの作成

image.png


秘密鍵の作成

image.png

ダウンロードしたJSONファイルはgoogle-home-voicetext-docker/myappsに格納してください。


Cloud Firestoreの設定

image.png


Cloud Firestoreのセキュリティルール

image.png


環境変数の設定

# export FIREBASE_DATABASE_URL={databaseURLの値}
# export FIREBASE_SECRET_KEY_PATH=/myapps/{JSONファイルのファイル名}

firestore.jsの起動

# node firestore.js &

動作確認
image.png


Google Apps Scriptと連携する


参考URL


Google Apps Scriptの画面

image.png


プロジェクトの作成

Google Drive > 新規 > その他 > Google Apps Script もしくは 「アプリを追加」で新規プロジェクトを作成


コード.jsを編集、保存

// Firebase Cloud Firestoreに格納されたメッセージを更新する
function updateMessage(message) {
  const email     = "{JSONファイルのclient_emailの値}";
  const key       = "{JSONファイルのprivate_keyの値}";
  const projectId = "{JSONファイルのproject_idの値}";
  const firestore = FirestoreApp.getFirestore(email, key, projectId);
  const data = {
    "message": message
  };
  firestore.updateDocument("googlehome/chant", data);
}

// 動作確認用
function testMessage() {
  updateMessage("テストです");
}

ライブラリの追加

メニューから
リソース > ライブラリ
「ライブラリを追加」に1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw を入力
image.png


動作確認

image.png


Googleカレンダーと連携

// カレンダーから1分以内に開始するイベントを取得し、タイトルをFirebase Cloud Firestoreに書き込む
function main() {
  const now = new Date();
  const startTime = new Date(now.getTime());                    // 開始時刻(現在時刻)
  const endTime   = new Date(now.getTime() + (1 * 60 * 1000));  // 終了時刻(現在時刻プラス1分)
  // IDを指定してカレンダーを取得
  const myCals = CalendarApp.getCalendarById("{GoogleカレンダーのID(メールアドレス)}");
  const myEvents = myCals.getEvents(startTime, endTime);  // 開始時刻と終了時刻の間に発生するイベントをカレンダーから取得
  if (!myEvents) return;
  for ( var index_e in myEvents) {
    const eventStartTime = myEvents[index_e].getStartTime();
    // 取得したイベントが1分以内に開始する場合はFirestoreを更新
    if (eventStartTime.getTime() > (startTime.getTime())) {
      updateMessage(myEvents[index_e].getTitle());
    }
  }
}

この関数をコード.gsに追加して1分間隔で実行するトリガーを作成すると、Googleカレンダーに書き込んだスケジュールが時間になると自動的に読み上げられます。

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