-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
122 lines (116 loc) · 4.06 KB
/
Copy pathserver.ts
File metadata and controls
122 lines (116 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { ClientConfig, Client, middleware, MiddlewareConfig, WebhookEvent, TextMessage, MessageAPIResponseBase } from "@line/bot-sdk";
import express, { Application, Request, Response } from "express";
import { load } from "ts-dotenv";
const env = load({
CHANNEL_ACCESS_TOKEN: String,
CHANNEL_SECRET: String,
PORT: Number,
BUCKET_NAME: String
});
const config = {
channelAccessToken: env.CHANNEL_ACCESS_TOKEN || "",
channelSecret: env.CHANNEL_SECRET || ""
};
const { Storage } = require('@google-cloud/storage');
const port = env.PORT || "";
const clientConfig: ClientConfig = config;
const middlewareConfig: MiddlewareConfig = config;
const client = new Client(clientConfig);
const app: Application = express();
const filePath = "message.txt";
const textCode = "utf8";
const storage = new Storage();
const bucket = storage.bucket(env.BUCKET_NAME);
const file = bucket.file(filePath);
app.get("/", async(_req: Request, res: Response): Promise<Response> => {
return res.status(200).send({
message: "success"
});
});
const textEventHandler = async(event: WebhookEvent): Promise<MessageAPIResponseBase | undefined> => {
if(event.type !== "message" || event.message.type !== "text") {
return;
}
const { replyToken } = event;
const { text } = event.message;
const response = (async(): Promise<TextMessage> => {
if(text.charAt(0) === "/") {
const command = text.substring(1);
if(command === "help") {
return {
type: "text",
text: `/status: 状況を確認\n/help: コマンド一覧\n/get: セットされているテキストを確認`
}
}
if(command === "status") {
return {
type: "text",
text: `バージョン: ${process.env.GAE_VERSION}\nデプロイID: ${process.env.GAE_DEPLOYMENT_ID}\nポート: ${process.env.PORT}`
}
}
if(command === "get") {
try {
const [content] = await file.download();
return {
type: "text",
text: `[${content}]がセットされています`
};
} catch (err) {
console.error(err);
return {
type: "text",
text: `読み取りに失敗しました\n${err}`
};
}
}
return {
type: "text",
text: `入力された開発コマンド、${command}は未実装です`
}
}
try {
await file.save(text, { contentType: textCode });
return {
type: "text",
text: `[${text}]をセットしました`
};
} catch (err) {
console.error(err);
return {
type: "text",
text: `[${text}]のセットに失敗しました\n${err}`
};
}
})();
return await client.replyMessage(replyToken, await response);
};
app.post("/webhook", middleware(middlewareConfig), async(req: Request, res: Response): Promise<Response> => {
const events: WebhookEvent[] = req.body.events;
await Promise.all(events.map(async(event: WebhookEvent) => {
try {
await textEventHandler(event);
} catch(e: unknown) {
if(e instanceof Error) {
console.error(e);
}
return res.status(500).send();
}
}));
return res.status(200).send();
});
app.get("/esp32", async(_req: Request, res: Response): Promise<Response> => {
try {
const [content] = await file.download();
return res.status(200).send({
message: content.toString()
});
} catch(err) {
console.error(err);
return res.status(200).send({
message: err
});
}
});
app.listen(port, () => {
console.log(`server is listen on port ${port}`);
});