質問編集履歴

4

ソースコードの追加

2020/01/27 15:38

投稿

Okkawa
Okkawa

スコア6

test CHANGED
File without changes
test CHANGED
@@ -106,6 +106,74 @@
106
106
 
107
107
 
108
108
 
109
+ //お天気
110
+
111
+ if (event.type !== 'message' || event.message.type !== 'text') return;
112
+
113
+ usersRef.set(event.message.text);
114
+
115
+
116
+
117
+ let result = null;
118
+
119
+
120
+
121
+ const command = event.message.text;
122
+
123
+ switch (command) {
124
+
125
+ case 'おはよう': //お天気情報取得
126
+
127
+
128
+
129
+ const city = '130010'; // 東京
130
+
131
+ const url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city;
132
+
133
+ const res = await axios.get(url);
134
+
135
+ const item = res.data;
136
+
137
+ const link = "http://weather.livedoor.com/area/forecast/130010";
138
+
139
+ // タイトル
140
+
141
+ var wheather1;
142
+
143
+ var wheather2;
144
+
145
+ // 今日、明日、明後日の天気
146
+
147
+ const today = item.forecasts[0];
148
+
149
+ const tomorrow = item.forecasts[1];
150
+
151
+ //天気予報を変数へ代入
152
+
153
+ wheather1 = today.dateLabel + ' :' + today.telop + NEW_LINE;
154
+
155
+ wheather2 = tomorrow.dateLabel + ' :' + tomorrow.telop + NEW_LINE;
156
+
157
+
158
+
159
+
160
+
161
+ console.log(wheather1 + wheather2);
162
+
163
+ result = {
164
+
165
+ type: 'text',
166
+
167
+ text: '[東京の天気]' + NEW_LINE + wheather1 + wheather2 + '↓詳細↓' + NEW_LINE + link
168
+
169
+ };
170
+
171
+
172
+
173
+ break;
174
+
175
+
176
+
109
177
  ```
110
178
 
111
179
  ### 試したこと

3

ソースコードの更新

2020/01/27 15:38

投稿

Okkawa
Okkawa

スコア6

test CHANGED
File without changes
test CHANGED
@@ -16,77 +16,97 @@
16
16
 
17
17
  ```JavaScript
18
18
 
19
+ const ENDPOINT = 'https://tetsudo.rti-giken.jp/free/delay.json'
20
+
19
- const axios = require('axios');
21
+ const axios = require('axios');
20
22
 
21
23
 
22
24
 
23
- //天気のAPI
25
+ const handleEvent = async event => {
24
26
 
25
- const city = '130010'; // 東京
27
+ // ガード節は1行でOK
26
28
 
27
- const url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city;
29
+ if (event.type !== 'message' || event.message.type !== 'text') return;
28
30
 
29
- const res = await axios.get(url);
31
+ usersRef.set(event.message.text);
30
32
 
31
- const item = res.data;
32
33
 
33
- // タイトル
34
34
 
35
- var wheather1;
35
+ let result = null;
36
36
 
37
- var wheather2;
38
37
 
39
- // 今日、明日、明後日の天気
40
38
 
41
- const today = item.forecasts[0];
39
+ const command = event.message.text;
42
40
 
43
- const tomorrow = item.forecasts[1];
41
+ switch (command) {
44
42
 
45
- //天気予報を変数へ代入
43
+
46
44
 
47
- wheather1 = today.dateLabel + ' :' + today.telop + NEW_LINE;
48
45
 
46
+
47
+ case '山手線':
48
+
49
+
50
+
51
+ async(userId, command) => {
52
+
53
+ const res = await axios.get(ENDPOINT);
54
+
55
+ const item = res.data;
56
+
57
+ let Tresult = item.filter((value) => {
58
+
59
+ return value.name === command
60
+
61
+ })
62
+
63
+ console.log(Tresult)
64
+
65
+ if (Tresult.length > 0) {
66
+
67
+ await client.pushMessage(userId, result = {
68
+
69
+ type: 'text',
70
+
49
- wheather2 = tomorrow.dateLabel + ' :' + tomorrow.telop + NEW_LINE;
71
+ text: `たいへ〜ん!!${Tresult.name}は現在遅れが出ています!\n余裕を持って行動をしてくださいね!`,
72
+
73
+ });
74
+
75
+ } else {
76
+
77
+ await client.pushMessage(userId, result = {
78
+
79
+ type: 'text',
80
+
81
+ text: `${command}は平常運転です。\n焦らず行動してください!`,
82
+
83
+ });
84
+
85
+ }
86
+
87
+ }
88
+
89
+ break;
50
90
 
51
91
 
52
92
 
53
93
 
54
94
 
95
+ default:
96
+
97
+ result = {
98
+
99
+ type: 'text',
100
+
55
- console.log(wheather1 + wheather2);
101
+ text: `${command}は不可能です。今後に期待して!!`
102
+
103
+ };
104
+
105
+ }
56
106
 
57
107
 
58
108
 
59
- //天気を参考にした電車情報のソース
60
-
61
- const Turl = 'https://tetsudo.rti-giken.jp/free/delay.json';
62
-
63
- const Tres = await axios.get(Turl);
64
-
65
- const Titem = Tres.data;
66
-
67
- // タイトル
68
-
69
- var Train1;
70
-
71
-
72
-
73
- // 今日、明日、明後日の天気
74
-
75
- var YamanoteLine = Titem.data.name;
76
-
77
- //天気予報を変数へ代入
78
-
79
- Train1 = YamanoteLine == "山手線";
80
-
81
-
82
-
83
- console.log(Train1);
84
-
85
109
  ```
86
-
87
-
88
-
89
-
90
110
 
91
111
  ### 試したこと
92
112
 

2

ソースコードの追加

2020/01/27 06:47

投稿

Okkawa
Okkawa

スコア6

test CHANGED
File without changes
test CHANGED
@@ -7,6 +7,86 @@
7
7
 
8
8
 
9
9
  電車遅延API→ https://rti-giken.jp/fhc/api/train_tetsudo/
10
+
11
+
12
+
13
+ ### 該当のソースコード
14
+
15
+
16
+
17
+ ```JavaScript
18
+
19
+ const axios = require('axios');
20
+
21
+
22
+
23
+ //天気のAPI
24
+
25
+ const city = '130010'; // 東京
26
+
27
+ const url = 'http://weather.livedoor.com/forecast/webservice/json/v1?city=' + city;
28
+
29
+ const res = await axios.get(url);
30
+
31
+ const item = res.data;
32
+
33
+ // タイトル
34
+
35
+ var wheather1;
36
+
37
+ var wheather2;
38
+
39
+ // 今日、明日、明後日の天気
40
+
41
+ const today = item.forecasts[0];
42
+
43
+ const tomorrow = item.forecasts[1];
44
+
45
+ //天気予報を変数へ代入
46
+
47
+ wheather1 = today.dateLabel + ' :' + today.telop + NEW_LINE;
48
+
49
+ wheather2 = tomorrow.dateLabel + ' :' + tomorrow.telop + NEW_LINE;
50
+
51
+
52
+
53
+
54
+
55
+ console.log(wheather1 + wheather2);
56
+
57
+
58
+
59
+ //天気を参考にした電車情報のソース
60
+
61
+ const Turl = 'https://tetsudo.rti-giken.jp/free/delay.json';
62
+
63
+ const Tres = await axios.get(Turl);
64
+
65
+ const Titem = Tres.data;
66
+
67
+ // タイトル
68
+
69
+ var Train1;
70
+
71
+
72
+
73
+ // 今日、明日、明後日の天気
74
+
75
+ var YamanoteLine = Titem.data.name;
76
+
77
+ //天気予報を変数へ代入
78
+
79
+ Train1 = YamanoteLine == "山手線";
80
+
81
+
82
+
83
+ console.log(Train1);
84
+
85
+ ```
86
+
87
+
88
+
89
+
10
90
 
11
91
  ### 試したこと
12
92
 

1

/

2020/01/24 06:15

投稿

Okkawa
Okkawa

スコア6

test CHANGED
File without changes
test CHANGED
@@ -21,5 +21,3 @@
21
21
  プログラミング学習中の学生ですので、わかりやすく教えていただけると幸いです。
22
22
 
23
23
  よろしくお願い致します。
24
-
25
- ここにより詳細な情報を記載してください。