LoginSignup
1
1

More than 1 year has passed since last update.

Google Assitant V2 対策(IFTTTの変数入力の実現)

Last updated at Posted at 2022-09-10

はじめに

●2022年9月からGoogle Assitantが改悪され、変数入力ができなくなりました。いままで、
WSLのUbuntuでyoutube音楽をGoogleHomeで操作する
などの複数のレシピを作成していたので、対策を考えました。

方法

1)変数を保存する場所としてGoogle keepを使用する
keepのタイトルのメモは「タスク」としました。
2)5秒周期でkeepのメモを監視し、変更があったら、task.jsを起動する。起動オプションで変数を渡す。
認証は16桁のアプリパスワードを使用する。
当初、ラズパイで構築しようとしたが、認証エラーになるので、WSLのUbuntu環境で実行しています。
3)task.jsで起動オプションをコマンド(例:音楽)と変数(例:あいみょん)に分割し、コマンドに対応したfirebaseに
変数を書き込むことで既存のレシピの代わりに、使用できます。(既存のレシピは不要)

使用方法

自分:OK グーグル タスクリストに追加
自分:OK グーグル タスク (ルーティンでタスク=タスクリストに追加と定義した場合)
GoogleHome:追加するものを教えて下さい
自分:音楽 あいみょん

Google keepを監視するプログラム(gkeeper.py)

import gkeepapi
import os
import requests
import time
import subprocess

WISHLIST_NOTE_NAME="タスク"

# dump_all_list print all list
def dump_all_list(target):
    for i in target:
        print(i.text)
        # リストが空でなければtask.jsを起動
        if i.text != "":
            say = i.text.replace(' ','')  #文字列の置換(半角空白を削除
            command = 'node /home/ユーザ名/task.js ' + say
            subprocess.run(command, shell=True)
            print(command)
        # リストアイテムを削除
        i.delete()

# getnotes checks last and current wish list and returns True if any updates are there.
def getnotes():
    keep = gkeepapi.Keep()

    # 16桁のアプリパスワードを使用
    success = keep.login("XXXXXX@gmail.com", "XXXXXXXXXXXXXXXX")

    gnotes = keep.all()
    for i in gnotes:
        if(i.title == WISHLIST_NOTE_NAME):
            # keep.sync()
            # print("Current list")
            dump_all_list(i.unchecked)
            keep.sync()

            break

if __name__ == "__main__":
    while True:
        getnotes()
        time.sleep(5)

firebaseに変数を書き込むプログラム(task.js)

var firebase = require("firebase");
var iconv = require('iconv-lite');
var value1 ;

//firebase config
var config = {
    apiKey: "XXXXXXXX",
    authDomain: "XXXXXXXX.firebaseapp.com",
    databaseURL: "https://XXXXXXXX.firebaseio.com",
    projectId: "XXXXXXXX",
    storageBucket: "XXXXXXXX.appspot.com",
    messagingSenderId: "XXXXXXXX"

};
firebase.initializeApp(config);

value = process.argv[2]
console.log(value)

//コマンドと変数を分離
value1 = value.substring(0,2);
value2 = value.substring(2,);

//firebaseに書き込み
if(value1 === "音楽") {
  var commentsRef = firebase.database().ref('googlehome5');
  commentsRef.set({ body: value2 });
} else if(value1 === "開け" || value1 === "開く") {
  var commentsRef = firebase.database().ref('googlehome8');
  commentsRef.set({ body: value2 });
} else if(value1 === "PC" || value1 === "PC") {
  var commentsRef = firebase.database().ref('googlehome1');
  commentsRef.set({ body: value2 });
}

function exit(){
  process.exit();
  }
setTimeout(exit,1000);

参考にした記事

Google AssitantでIFTTTの変数入力を再現する
GoogleKeepの買い物リストをLINE通知する

1
1
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
1
1