iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 18
1

在sub端 googleHomeService要進行一些更動

  • 加入pub/sub的subsciption

  • 加入金鑰的json檔案

  • 設定subscriberOptions

  • pubsub.subscription to google tts push message

  • 設定ack

      const googleHome = require('google-home-push');
      const { PubSub } = require('@google-cloud/pubsub');
      const config = require('./config')
    
      const language = "zh-TW"
      const accent = "zh-TW"
      const options = {
        language: language,
        accent: accent
      }
    
      const subscriptionName = 'projects/utliot-3a89b/subscriptions/utl-smart-speakers'
    
    
      const pubsub = new PubSub({
        projectId: 'your-projectId',
        keyFilename: '/home/pi/Desktop/googleHomeService/金曜.json'
      });
    
    
      const subscriberOptions = {
        flowControl: {
          maxMessages: 1
        }
      }
      const subscription = pubsub.subscription(subscriptionName,subscriberOptions);
      const messageHandler = message => {
    
        console.log(message.data)
        console.log(Buffer.from(message.data, 'base64').toString())
        const notifyMessage =  Buffer.from(message.data, 'base64').toString()
    
          const googleHomeIp = config.moudule.ip
          const myHome = new googleHome(googleHomeIp, options)
          myHome.speak(`${notifyMessage}`).then((status)=>{
          console.log(status)
          })
        }
    
        message.ack();
      }
      // Listen for new messages until timeout is hit
      subscription.on('message',messageHandler )
      subscription.on('error', function (error) {
        // Do something with the error
        console.error(`ERROR: ${error}`);
        throw error;
      });
    

記得先到GCP該專案的IAM與管理,建立服務帳戶

  • step1
  • step 2 選擇Pub/sub 發布/訂閱管理員
  • step 3
  • step 4 進該項目點擊新增金鑰,並下載JSON


上一篇
Day17 pubsub整合raspbin(1)pub端
下一篇
Day19 google action
系列文
手拉手教自己用樹梅派、GoogleHome、GCP做一個AI智慧提醒機器人!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言