質問編集履歴
1
コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -39,3 +39,185 @@
|
|
39
39
|
関連するワードの羅列でも構いません。皆様のご回答をきっかけに深掘りしていきたいと考えています。
|
40
40
|
|
41
41
|
よろしくお願いいたします。
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
```ここに言語を入力
|
56
|
+
|
57
|
+
const https = require("https")
|
58
|
+
|
59
|
+
const express = require("express")
|
60
|
+
|
61
|
+
const app = express()
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
const PORT = process.env.PORT || 3000
|
66
|
+
|
67
|
+
const TOKEN = process.env.LINE_ACCESS_TOKEN
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
const globalValue = {}
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
app.use(express.json())
|
76
|
+
|
77
|
+
app.use(express.urlencoded({
|
78
|
+
|
79
|
+
extended: true
|
80
|
+
|
81
|
+
}))
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
app.get("/", (req, res) => {
|
88
|
+
|
89
|
+
res.sendStatus(200)
|
90
|
+
|
91
|
+
})
|
92
|
+
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
app.post("/webhook", function (req, res) {
|
98
|
+
|
99
|
+
res.send("HTTP POST request sent to the webhook URL")
|
100
|
+
|
101
|
+
// ユーザーがボットにメッセージを送った場合、返信メッセージを送る
|
102
|
+
|
103
|
+
if (req.body.events[0].type === 'message') {
|
104
|
+
|
105
|
+
globalValue[req.body.events[0].source.userId] = !globalValue[req.body.events[0].source.userId]
|
106
|
+
|
107
|
+
yourStatus = globalValue[req.body.events[0].source.userId] ? 'true' : 'false'
|
108
|
+
|
109
|
+
const dataString = JSON.stringify({
|
110
|
+
|
111
|
+
replyToken: req.body.events[0].replyToken,
|
112
|
+
|
113
|
+
messages: [
|
114
|
+
|
115
|
+
{
|
116
|
+
|
117
|
+
"type": "text",
|
118
|
+
|
119
|
+
"text": "your status is " + yourStatus
|
120
|
+
|
121
|
+
},
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
"type": "text",
|
126
|
+
|
127
|
+
"text": 'test'
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
]
|
132
|
+
|
133
|
+
})
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
//リクエストヘッダー
|
140
|
+
|
141
|
+
const headers = {
|
142
|
+
|
143
|
+
"Content-Type": "application/json",
|
144
|
+
|
145
|
+
"Authorization": "Bearer " + TOKEN
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
// リクエストに渡すオプション
|
154
|
+
|
155
|
+
const webhookOptions = {
|
156
|
+
|
157
|
+
"hostname": "api.line.me",
|
158
|
+
|
159
|
+
"path": "/v2/bot/message/reply",
|
160
|
+
|
161
|
+
"method": "POST",
|
162
|
+
|
163
|
+
"headers": headers,
|
164
|
+
|
165
|
+
"body": dataString
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
// リクエストの定義
|
174
|
+
|
175
|
+
const request = https.request(webhookOptions, (res) => {
|
176
|
+
|
177
|
+
res.on("data", (d) => {
|
178
|
+
|
179
|
+
process.stdout.write(d)
|
180
|
+
|
181
|
+
})
|
182
|
+
|
183
|
+
})
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
// エラーをハンドル
|
188
|
+
|
189
|
+
request.on("error", (err) => {
|
190
|
+
|
191
|
+
console.error(err)
|
192
|
+
|
193
|
+
})
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//データを送信
|
198
|
+
|
199
|
+
request.write(dataString)
|
200
|
+
|
201
|
+
request.end()
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
})
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
|
210
|
+
|
211
|
+
app.listen(PORT, () => {
|
212
|
+
|
213
|
+
console.log(`Example app listening at http://localhost:${PORT}`)
|
214
|
+
|
215
|
+
})
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
```
|