teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コード追記

2021/12/21 15:26

投稿

Yusuke_m25
Yusuke_m25

スコア74

title CHANGED
File without changes
body CHANGED
@@ -18,4 +18,95 @@
18
18
 
19
19
  キーとなる単語や進め方、指針が無く困っております。
20
20
  関連するワードの羅列でも構いません。皆様のご回答をきっかけに深掘りしていきたいと考えています。
21
- よろしくお願いいたします。
21
+ よろしくお願いいたします。
22
+
23
+
24
+
25
+
26
+
27
+
28
+ ```ここに言語を入力
29
+ const https = require("https")
30
+ const express = require("express")
31
+ const app = express()
32
+
33
+ const PORT = process.env.PORT || 3000
34
+ const TOKEN = process.env.LINE_ACCESS_TOKEN
35
+
36
+ const globalValue = {}
37
+
38
+ app.use(express.json())
39
+ app.use(express.urlencoded({
40
+ extended: true
41
+ }))
42
+
43
+
44
+ app.get("/", (req, res) => {
45
+ res.sendStatus(200)
46
+ })
47
+
48
+
49
+ app.post("/webhook", function (req, res) {
50
+ res.send("HTTP POST request sent to the webhook URL")
51
+ // ユーザーがボットにメッセージを送った場合、返信メッセージを送る
52
+ if (req.body.events[0].type === 'message') {
53
+ globalValue[req.body.events[0].source.userId] = !globalValue[req.body.events[0].source.userId]
54
+ yourStatus = globalValue[req.body.events[0].source.userId] ? 'true' : 'false'
55
+ const dataString = JSON.stringify({
56
+ replyToken: req.body.events[0].replyToken,
57
+ messages: [
58
+ {
59
+ "type": "text",
60
+ "text": "your status is " + yourStatus
61
+ },
62
+ {
63
+ "type": "text",
64
+ "text": 'test'
65
+ }
66
+ ]
67
+ })
68
+
69
+
70
+ //リクエストヘッダー
71
+ const headers = {
72
+ "Content-Type": "application/json",
73
+ "Authorization": "Bearer " + TOKEN
74
+ }
75
+
76
+
77
+ // リクエストに渡すオプション
78
+ const webhookOptions = {
79
+ "hostname": "api.line.me",
80
+ "path": "/v2/bot/message/reply",
81
+ "method": "POST",
82
+ "headers": headers,
83
+ "body": dataString
84
+ }
85
+
86
+
87
+ // リクエストの定義
88
+ const request = https.request(webhookOptions, (res) => {
89
+ res.on("data", (d) => {
90
+ process.stdout.write(d)
91
+ })
92
+ })
93
+
94
+ // エラーをハンドル
95
+ request.on("error", (err) => {
96
+ console.error(err)
97
+ })
98
+
99
+ //データを送信
100
+ request.write(dataString)
101
+ request.end()
102
+ }
103
+ })
104
+
105
+
106
+ app.listen(PORT, () => {
107
+ console.log(`Example app listening at http://localhost:${PORT}`)
108
+ })
109
+
110
+
111
+
112
+ ```