-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
188 lines (161 loc) · 5.4 KB
/
index.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import express from "express";
import { middleware, Client } from "@line/bot-sdk";
import dotenv from "dotenv";
import fs from "fs";
import _ from "lodash";
import mustache from "mustache";
import firebase from "firebase";
import bodyParser from "body-parser";
import bot from "./bot";
import RichMenu from "./rich";
if (process.env.DEV) {
const envConfig = dotenv.parse(fs.readFileSync(".env.dev"));
for (var k in envConfig) {
process.env[k] = envConfig[k];
}
} else {
dotenv.config();
}
firebase.initializeApp({
apiKey: process.env.FIR_KEY,
authDomain: `${process.env.PROJECT_NAME}.firebaseapp.com`,
databaseURL: `https://${process.env.PROJECT_NAME}.firebaseio.com`,
projectId: process.env.PROJECT_NAME,
storageBucket: `${process.env.PROJECT_NAME}.appspot.com`,
messagingSenderId: process.env.MSI
});
let users = [];
const app = express();
export let usersProfile = [];
export const usersRef = firebase.database().ref("users");
usersRef.on("value", function(snapshot) {
updateUsers(snapshot.val());
});
function updateUsers(allUsers) {
users = _.map(allUsers, (v, k) => k);
usersProfile = _.map(allUsers, (v, k) => v);
}
function getUserProfile(userId) {
return firebase
.database()
.ref("users/" + userId)
.once("value")
.then(s => s.val());
}
async function createUserProfile(userId) {
console.log("Create New User");
const profile = await client.getProfile(userId);
await writeUserData(userId, profile);
return profile;
}
function writeUserData(userId, profile) {
return firebase
.database()
.ref("users/" + userId)
.set(profile);
}
const config = {
channelAccessToken: process.env.ACCESS_TOKEN,
channelSecret: process.env.SECRET
};
const indexTemplate = fs.readFileSync("./static/templates/index.html", "utf8");
const flexTemplate = fs.readFileSync("./static/templates/flex.html", "utf8");
const profileTemplate = fs.readFileSync("./static/templates/profile.html", "utf8");
const liffTemplate = fs.readFileSync("./static/templates/liff.html", "utf8");
export const baseUrl = process.env.BASE_URL;
const client = new Client(config);
app.use("/static", express.static("static"));
app.use("/.well-known", express.static("static/.well-known"));
const rich = new RichMenu(client);
app.get("/", (req, res) => res.send(indexTemplate));
app.get("/liff", async (req, res) => {
try {
res.send(liffTemplate);
} catch (err) {
res.send("Not found");
}
});
app.get("/profile", async (req, res) => {
const { userId } = req.query;
if (!userId) {
res.send("尚未登入");
return;
}
try {
const profile = await client.getProfile(userId);
mustache.parse(profileTemplate);
const html = mustache.render(profileTemplate, {
name: profile.displayName || "",
status: profile.statusMessage || "",
image: profile.pictureUrl
});
res.send(html);
} catch (err) {
res.send("Not found");
}
});
app.get("/instant", (req, res) => {
const userId = req.query.userId;
const redirectUrl = `${baseUrl}/profile?userId=${userId}`;
res.redirect(redirectUrl);
});
app.get("/demo/dynamiclink", (req, res) => {
const userId = req.query.userId;
const appCode = process.env.APP_CODE;
const apn = process.env.APN;
const link = `${baseUrl}/profile?userId=${userId}`;
const redirectUrl = `https://${appCode}.app.goo.gl/?apn=${apn}&link=${link}&afl=${link}`;
res.redirect(redirectUrl);
});
app.get("/demo/instantapp", (req, res) => {
const userId = req.query.userId;
// const redirectUrl = 'intent://hotpads.com/indigo-at-twelve-west-portland-or-97205-skfrgn/pad#Intent;scheme=https;end'
const redirectUrl = `intent://${baseUrl.replace(/https:\/\//, "")}/instant?userId=${userId}#Intent;scheme=https;end`;
res.redirect(redirectUrl);
});
app.get("/demo/chrome", (req, res) => {
const userId = req.query.userId;
let redirectUrl = `intent://${baseUrl.replace(
/https:\/\//,
""
)}/profile?userId=${userId}#Intent;scheme=https;package=com.android.chrome;end`;
if (req.headers["user-agent"].search(/iPhone/g) > 0 || req.headers["user-agent"].search(/iPad/g) > 0) {
redirectUrl = `googlechrome://${baseUrl.replace(/https:\/\//, "")}/profile?userId=${userId}`;
}
res.redirect(redirectUrl);
});
const mydeepq_domain = "mydeepq.deepq.com";
app.get("/mydeepq/chrome", (req, res) => {
let redirectUrl = `intent://${mydeepq_domain}#Intent;scheme=https;package=com.android.chrome;end`;
if (req.headers["user-agent"].search(/iPhone/g) > 0 || req.headers["user-agent"].search(/iPad/g) > 0) {
redirectUrl = `googlechrome://${mydeepq_domain}`;
}
res.redirect(redirectUrl);
});
app.post("/webhook", middleware(config), async (req, res) => {
const userId = req.body.events[0].source.userId;
let profile = await getUserProfile(userId);
if (!profile) {
profile = await createUserProfile(userId);
}
console.log(profile);
await new bot(client, req.body.events, rich, users).start();
res.send("A_A");
});
app.get("/flex", (req, res) => res.send(flexTemplate));
app.post("/flex", bodyParser.urlencoded({ extended: false }), async (req, res) => {
console.log(JSON.stringify(req.body))
try {
await client.pushMessage(req.body.line_id, {
type: "flex",
altText: "this is a flex message",
contents: JSON.parse(req.body.message)
});
} catch (err) {
return res.send("error");
}
return res.send("Send!");
});
app.listen(process.env.PORT || 5000, () => {
console.log("Line BOT server has been started!");
});