-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample5.js
63 lines (53 loc) · 1.79 KB
/
sample5.js
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
const path = require("path");
const express = require("express");
const line = require("@line/bot-sdk");
const lineConfig = {
channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN,
channelSecret: process.env.LINE_CHANNEL_SECRET
};
const lineClient = new line.Client(lineConfig);
function createReplyMessage(input) {
const hands = ["グー", "チョキ", "パー"];
const messages = [];
let text = "";
function message(str) {
return {
type: "text",
text: str
}
}
if (hands.indexOf(input) === -1) {
text = "グー・チョキ・パーのどれかを入力してね";
messages.push(message(text));
} else {
let user_hand = hands.indexOf(input);
let cpu_hand = Math.floor(hands.length * Math.random());
text = hands[cpu_hand];
messages.push(message(text));
let judge_text = "";
let judge = (user_hand - cpu_hand + 3) % 3; //じゃんけんの判定
if (judge === 0){ //あいこ
judge_text = `aiko`;
} else if (judge === 1) { //botの勝ち
judge_text = `わいの勝ちや!`;
} else if (judge === 2) { //userの勝ち
judge_text = `負けた(;_;)`;
}
text = judge_text;
messages.push(message(text));
}
return messages;
}
const server = express();
server.use("/images", express.static(path.join(__dirname, "images")));
server.post("/webhook", line.middleware(lineConfig), (req, res) => {
// LINEのサーバーに200を返す
res.sendStatus(200);
for (const event of req.body.events) {
if (event.type === "message" && event.message.type === "text") {
const message = createReplyMessage(event.message.text);
lineClient.replyMessage(event.replyToken, message);
}
}
});
server.listen(process.env.PORT || 8080);