"use strict"; const line = require("@line/bot-sdk"); const express = require("express"); const config = require("./config.json"); const bodyParser = require("body-parser"); const axios = require("axios"); const fs = require("fs"); const request = require("request"); const moment = require("moment"); const { type } = require("os"); // create LINE SDK client const client = new line.Client(config); const app = express(); // webhook callback app.use("/webhook", line.middleware(config)); app.post("/webhook", (req, res) => { // req.body.events should be an array of events if (!Array.isArray(req.body.events)) { return res.status(500).end(); } // handle events separately Promise.all( req.body.events.map((event) => { console.log("event", event); // check verify webhook event if ( event.replyToken === "00000000000000000000000000000000" || event.replyToken === "ffffffffffffffffffffffffffffffff" ) { return; } return handleEvent(event); }) ) .then(() => res.end()) .catch((err) => { console.error(err); res.status(500).end(); }); }); // simple reply function const replyText = (token, texts) => { texts = Array.isArray(texts) ? texts : [texts]; return client.replyMessage( token, texts.map((text) => ({ type: "text", text })) ); }; const tolow = (str) => { return str.toLowerCase(); }; const handleEvent = (event) => { let payload = { type: "text", text: "Hello From PUI", }; if (event.message.type == "text") { let selecttext = tolow(event.message.text); let get_text = mockText()[selecttext]; if (get_text) { payload = get_text; } } else { payload.text = "Other Message =>>>" + JSON.stringify(event); } return client.replyMessage(event.replyToken, payload); }; app.use(bodyParser.json()); app.get("/", (req, res) => { res.json({ line: "ok" }); }); app.get("/push", bodyParser.json(), (req, res) => { let message = { type: "text", text: `use Push Message! to `, }; client.pushMessage("Uaac2ca5a02feab67a18d5521b572b5aa", message); res.json(message); }); app.post("/save", bodyParser.json(), async (req, res) => { console.log("saveFile!"); try { const downloadFile = function (uri, filename, callback) { request.head(uri, function (err, res, body) { console.log("content-type:", res.headers["content-type"]); console.log("content-length:", res.headers["content-length"]); console.log("Content-Type:", res.headers["Content-Type"]); console.log("res.headers ::", res.headers); request(uri, { headers: { Authorization: "Bearer be/XHjQ+gMoypZE78Us7hk0h6PA04TyfpQciMOq+B/OVPmumozdhGzYUwopDgsOMCM7RymTK8m++q20GSj3c6B7gZkgEmuGYEYPvc6j+4as6X5bu7tEg+KAZKMfBVDnk+ekpAorC7FMwVPyt2frGRQdB04t89/1O/w1cDnyilFU=", }, }) .pipe(fs.createWriteStream(filename)) .on("close", callback); }); }; let unquie_file = moment().format("YYYY-MM-DD_HHmmssss"); let file_name = `filesave_${unquie_file}`; let message_id = req.body.message_id; let URI = `https://api-data.line.me/v2/bot/message/${message_id}/content`; console.log("message_id ::", message_id); console.log("file_name ::", file_name); axios .get(URI, { headers: { Authorization: "Bearer be/XHjQ+gMoypZE78Us7hk0h6PA04TyfpQciMOq+B/OVPmumozdhGzYUwopDgsOMCM7RymTK8m++q20GSj3c6B7gZkgEmuGYEYPvc6j+4as6X5bu7tEg+KAZKMfBVDnk+ekpAorC7FMwVPyt2frGRQdB04t89/1O/w1cDnyilFU=", }, }) .then(function (response) { // handle success console.log("axios =>>", response.headers["content-type"]); let sp = response.headers["content-type"].split("/"); let type = sp[sp.length - 1]; let full_file_name = file_name + "." + type; console.log("full_file_name =", full_file_name); downloadFile(URI, full_file_name, function () { console.log("done!"); res.json({ LoadFlieName: "Success" }); }); }) .catch(function (error) { // handle error console.log(error); console.error("errorxx ::", error); res.json({ error: "error" }); }); } catch (error) { console.error("errorxx ::", error); } }); app.post("/saveNew", bodyParser.json(), async (req, res) => { console.log("saveFile!"); try { let stream = await client.getMessageContent("14955535936627"); console.log("stream!", stream); } catch (error) { console.error(error); } res.json({ OK: "OK" }); }); app.post("/push", (req, res) => { let body = req.body; let { user_id } = body; console.log("body ::", body); let message = { type: "text", text: `use Push Message! to ${user_id}`, }; client.pushMessage(user_id, message); res.json(message); }); app.post("/multicast", (req, res) => { let body = req.body; let { user_ids } = body; console.log("body ::", body); let message = [ { type: "text", text: `use multicast Message1! to ${JSON.stringify(user_ids)}`, }, { type: "text", text: `use multicast Message2! to ${JSON.stringify(user_ids)}`, }, ]; client.multicast(user_ids, message); res.json(message); }); const port = config.port; app.listen(port, () => { console.log(`listening on ${port}`); }); const GenContentFlex = (content) => { return { flex1: { type: "flex", altText: "GenContentFlex!", contents: content, }, }; }; const mockText = () => { return { flex1: { type: "flex", altText: "this is a flex message", contents: { type: "bubble", body: { type: "box", layout: "vertical", contents: [ { type: "text", text: "hello", }, { type: "text", text: "world", }, ], }, }, }, flex2: { type: "flex", altText: "this is a flex message", contents: { type: "bubble", direction: "ltr", header: { type: "box", layout: "vertical", contents: [ { type: "text", text: "แมว", align: "center", contents: [], }, ], }, hero: { type: "image", url: "https://static.trueplookpanya.com/tppy/member/m_545000_547500/545994/cms/images/2019-Q3/แมว9ชีวิต.jpg", size: "full", aspectRatio: "1.51:1", aspectMode: "fit", }, body: { type: "box", layout: "vertical", contents: [ { type: "text", text: "คำอธิบาย", align: "center", contents: [], }, ], }, footer: { type: "box", layout: "horizontal", contents: [ { type: "button", action: { type: "uri", label: "ดูรูปแมวชัดๆ", uri: "https://static.trueplookpanya.com/tppy/member/m_545000_547500/545994/cms/images/2019-Q3/แมว9ชีวิต.jpg", }, }, ], }, }, }, flex3: { type: "flex", altText: "this is a flex message", contents: { type: "bubble", direction: "ltr", hero: { type: "image", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_3_movie.png", size: "full", aspectRatio: "20:13", aspectMode: "cover", action: { type: "uri", label: "Action", uri: "https://linecorp.com/", }, }, body: { type: "box", layout: "vertical", spacing: "md", contents: [ { type: "text", text: "BROWN'S ADVENTURE\nIN MOVIE", weight: "bold", size: "xl", gravity: "center", wrap: true, contents: [], }, { type: "box", layout: "baseline", margin: "md", contents: [ { type: "icon", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png", size: "sm", }, { type: "icon", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png", size: "sm", }, { type: "icon", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png", size: "sm", }, { type: "icon", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gold_star_28.png", size: "sm", }, { type: "icon", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/review_gray_star_28.png", size: "sm", }, { type: "text", text: "4.0", size: "sm", color: "#999999", flex: 0, margin: "md", contents: [], }, ], }, { type: "box", layout: "vertical", spacing: "sm", margin: "lg", contents: [ { type: "box", layout: "baseline", spacing: "sm", contents: [ { type: "text", text: "Date", size: "sm", color: "#AAAAAA", flex: 1, contents: [], }, { type: "text", text: "Monday 25, 9:00PM", size: "sm", color: "#666666", flex: 4, wrap: true, contents: [], }, ], }, { type: "box", layout: "baseline", spacing: "sm", contents: [ { type: "text", text: "Place", size: "sm", color: "#AAAAAA", flex: 1, contents: [], }, { type: "text", text: "7 Floor, No.3", size: "sm", color: "#666666", flex: 4, wrap: true, contents: [], }, ], }, { type: "box", layout: "baseline", spacing: "sm", contents: [ { type: "text", text: "Seats", size: "sm", color: "#AAAAAA", flex: 1, contents: [], }, { type: "text", text: "C Row, 18 Seat", size: "sm", color: "#666666", flex: 4, wrap: true, contents: [], }, ], }, ], }, { type: "box", layout: "vertical", margin: "xxl", contents: [ { type: "spacer", }, { type: "image", url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/linecorp_code_withborder.png", size: "xl", aspectMode: "cover", }, { type: "text", text: "You can enter the theater by using this code instead of a ticket", size: "xs", color: "#AAAAAA", margin: "xxl", wrap: true, contents: [], }, ], }, ], }, }, }, cat: { type: "bubble", direction: "ltr", header: { type: "box", layout: "vertical", contents: [ { type: "text", text: "แมว", align: "center", contents: [], }, ], }, hero: { type: "image", url: "https://static.trueplookpanya.com/tppy/member/m_545000_547500/545994/cms/images/2019-Q3/แมว9ชีวิต.jpg", size: "full", aspectRatio: "1.51:1", aspectMode: "fit", }, body: { type: "box", layout: "vertical", contents: [ { type: "text", text: "คำอธิบาย", align: "center", contents: [], }, ], }, footer: { type: "box", layout: "horizontal", contents: [ { type: "button", action: { type: "uri", label: "ดูรูปแมวชัดๆ", uri: "https://static.trueplookpanya.com/tppy/member/m_545000_547500/545994/cms/images/2019-Q3/แมว9ชีวิต.jpg", }, }, ], }, }, location: { type: "text", // ① text: "Select your favorite food category or send me your location!", quickReply: { // ② items: [ { type: "action", // ③ imageUrl: "https://example.com/sushi.png", action: { type: "message", label: "Sushi", text: "Sushi", }, }, { type: "action", imageUrl: "https://example.com/tempura.png", action: { type: "message", label: "Tempura", text: "Tempura", }, }, { type: "action", // ④ action: { type: "location", label: "Send location", }, }, ], }, }, }; };