前提・実現したいこと
電車遅延APIを使用してJavaScriptで遅延情報を表示させたい
最終的にはLINEbotに出力させる予定です。
電車遅延API→ https://rti-giken.jp/fhc/api/train_tetsudo/
該当のソースコード
JavaScript
1const ENDPOINT = 'https://tetsudo.rti-giken.jp/free/delay.json' 2const axios = require('axios'); 3 4const handleEvent = async event => { 5 // ガード節は1行でOK 6 if (event.type !== 'message' || event.message.type !== 'text') return; 7 usersRef.set(event.message.text); 8 9 let result = null; 10 11 const command = event.message.text; 12 switch (command) { 13 14 15 case '山手線': 16 17 async(userId, command) => { 18 const res = await axios.get(ENDPOINT); 19 const item = res.data; 20 let Tresult = item.filter((value) => { 21 return value.name === command 22 }) 23 console.log(Tresult) 24 if (Tresult.length > 0) { 25 await client.pushMessage(userId, result = { 26 type: 'text', 27 text: `たいへ〜ん!!${Tresult.name}は現在遅れが出ています!\n余裕を持って行動をしてくださいね!`, 28 }); 29 } else { 30 await client.pushMessage(userId, result = { 31 type: 'text', 32 text: `${command}は平常運転です。\n焦らず行動してください!`, 33 }); 34 } 35 } 36 break; 37 38 39 default: 40 result = { 41 type: 'text', 42 text: `${command}は不可能です。今後に期待して!!` 43 }; 44 } 45 46//お天気 47if (event.type !== 'message' || event.message.type !== 'text') return; 48 usersRef.set(event.message.text); 49 50 let result = null; 51 52 const command = event.message.text; 53 switch (command) { 54 case 'おはよう': //お天気情報取得 55 56 const city = '130010'; // 東京 57 const url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city; 58 const res = await axios.get(url); 59 const item = res.data; 60 const link = "http://weather.livedoor.com/area/forecast/130010"; 61 // タイトル 62 var wheather1; 63 var wheather2; 64 // 今日、明日、明後日の天気 65 const today = item.forecasts[0]; 66 const tomorrow = item.forecasts[1]; 67 //天気予報を変数へ代入 68 wheather1 = today.dateLabel + ' :' + today.telop + NEW_LINE; 69 wheather2 = tomorrow.dateLabel + ' :' + tomorrow.telop + NEW_LINE; 70 71 72 console.log(wheather1 + wheather2); 73 result = { 74 type: 'text', 75 text: '[東京の天気]' + NEW_LINE + wheather1 + wheather2 + '↓詳細↓' + NEW_LINE + link 76 }; 77 78 break; 79
試したこと
livedoorのお天気webAPIを使用して天気を表示させる機能は完成していますが、電車遅延情報は仕様が違うのか
あまりよくわかりません。
補足情報(FW/ツールのバージョンなど)
プログラミング学習中の学生ですので、わかりやすく教えていただけると幸いです。
よろしくお願い致します。