0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

フォロー中のTwitter最新記事をしゃべらす(その2)

Last updated at Posted at 2021-10-17

はじめに

●「フォロー中のTwitter最新記事をしゃべらす」の記事のIFTTT/firebase版です。

環境

●Windows10 HOMEのPCにWSLのubuntuをインストールして使用してます。
●ubuntuでnpm,node.js,firebaseなどをインストールする
●WSLのubuntuでpulseaudioでPCのスピーカを使うようにする
●Windows側でpulseaudioサーバのインストールが必要です
 https://www.cendio.com/thinlinc/download の 「Client Bundle」のリンクからダウンロードできます
●音声出力はGoogle Translate Text to Speechを使用しています。

プログラム(nodejs)

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

var value1 ;
var formatted2 = '20210930000000';
var value2 ='aaaa';
vlet checksay = ["速報VL","中央本線","最大震度1"]

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

};
firebase.initializeApp(config);

//jsonからvalueに一致する値取得
const getJsonData = (value, json) => {
  for (var word in json)  if (value == word) return json[word]
  return json["default"]
}

//database更新時
const path = "/twitter"
const key = "message"
const db = firebase.database()
db.ref(path).on("value", function(changedSnapshot) {
//値取得
var value = changedSnapshot.child(key).val()

//入力テキストを修正する
if(value.indexOf("#NHK") !== -1) value = value.substring(0, value.indexOf("#NHK"));
if(value.indexOf("#nhk") !== -1) value = value.substring(0, value.indexOf("#nhk"));
if(value.indexOf("#JR") !== -1) value = value.substring(0, value.indexOf("#JR"));
if(value.indexOf("( https") !== -1) value = value.substring(0, value.indexOf("( https"));
if(value.indexOf("https") !== -1) value = value.substring(0, value.indexOf("https"));
value = value.replaceAll("<<<","");
value = value.replaceAll(">>>","");
value = value.replaceAll("","");
value = value.replaceAll(" "," ");
value = value.replaceAll("\n"," ");

console.log(value);

//更新時刻の取り出し
var dt = new Date();
var formatted = dt.toFormat("YYYYMMDDHH24MISS");
console.log(formatted);
console.log(formatted2);

//無処理の文字列かチェック
check = 0
for (let i =0; i < 3; i++) {
  if(value.indexOf(checksay[i]) !== -1) check = 1
}

//コマンド実行
//30秒以内で更新されたら、処理を無視する
if(formatted - formatted2 > 30) {
  if(value !== value2) {
    if(check === 0) {
//音声で通知する
      command = "/home/XXXXX/speak.sh" + ' ' + value;
      var exec = require('child_process').exec;
      exec(command, {maxBuffer: 1000*1024}, function(error, stdout, stderr) {
      });
    }
  }
}
//前回の更新時刻をセット
formatted2 = formatted;
value2 = value ;

//firebase clear
//db.ref(path).set({[key]: ""});
})

IFTTTの設定(例)

■NHKニュース速報
●If
New tweet from search
Search for
 from:nhk_news 速報 JUST IN
●Then
Make a web request
URL
 https://XXXXXXXXXXXXXXX.firebaseio.com/twitter/message.json
Method
 PUT
Content Type
 text/plan
Additional Headers
 空白
Body
 "{{Text}}"
■JR東日本[中央方面]
●If
New tweet from search
Search for
 JRE_F_Chuo
●Then
Make a web request
URL
 https://XXXXXXXXXXXXXXX.firebaseio.com/twitter/message.json
Method
 PUT
Content Type
 application/json
Additional Headers
 空白
Body
 "<<<{{Text}}>>>"

まとめ

●Twitterでテキストに改行があるとIFTTTでエラーになる場合があります。

参考

フォロー中のTwitter最新記事をしゃべらす

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?