From eb28d472dfdd78993b47618c9e5bd8ab0920d8ac Mon Sep 17 00:00:00 2001 From: sc.surasity@gmail.com Date: Tue, 26 Oct 2021 19:23:50 +0700 Subject: [PATCH] update Service --- index copy.js | 317 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- indexbk.js | 166 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- indexv1.js | 553 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- services/ContentService.js | 23 +++++++++++++++++++++++ services/LineService.js | 29 +++++++++++++++++++++++++++++ services/flexMsg.js | 749 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1178 insertions(+), 902 deletions(-) create mode 100644 index copy.js delete mode 100644 indexbk.js delete mode 100644 indexv1.js create mode 100644 services/ContentService.js create mode 100644 services/LineService.js create mode 100644 services/flexMsg.js diff --git a/index copy.js b/index copy.js new file mode 100644 index 0000000..327f6de --- /dev/null +++ b/index copy.js @@ -0,0 +1,317 @@ +"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 flexMsg = require("./flexMsg"); + +// 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 genMsgContent = flexMsg.GenContentFlex; +const flexs = flexMsg.flexs; +// console.log(genMsgContent()); + +const mockText = () => { + return { + flex0: flexs.flex0, + flex1: flexs.flex1, + + bub1: flexs.bub1, + bub2: flexs.bub2, + bub3: flexs.bub3, + bub4: flexs.bub4, + bub5: flexs.bub5, + bub6: flexs.bub6, + + confirm:{ + "type": "template", + "altText": "this is a confirm template", + "template": { + "type": "confirm", + "text": "Are you sure?", + "actions": [ + { + "type": "message", + "label": "Yes", + "text": "yes" + }, + { + "type": "message", + "label": "No", + "text": "no" + } + ] + } + }, + + + 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", + }, + }, + ], + }, + }, + + + + + // + image_carousel: { + "type": "template", + "altText": "this is a image carousel template", + "template": { + "type": "image_carousel", + "columns": [ + { + "imageUrl": "https://example.com/bot/images/item1.jpg", + "action": { + "type": "postback", + "label": "Buy", + "data": "action=buy&itemid=111" + } + }, + { + "imageUrl": "https://example.com/bot/images/item2.jpg", + "action": { + "type": "message", + "label": "Yes", + "text": "yes" + } + }, + { + "imageUrl": "https://example.com/bot/images/item3.jpg", + "action": { + "type": "uri", + "label": "View detail", + "uri": "http://example.com/page/222" + } + } + ] + } + } + + }; +}; diff --git a/index.js b/index.js index 327f6de..e786934 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,8 @@ const fs = require("fs"); const request = require("request"); const moment = require("moment"); const flexMsg = require("./flexMsg"); +const ContentService = require("./services/ContentService"); +const LineService = require("./services/LineService"); // create LINE SDK client const client = new line.Client(config); @@ -43,18 +45,6 @@ app.post("/webhook", (req, res) => { }); }); -// 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", @@ -62,8 +52,8 @@ const handleEvent = (event) => { }; if (event.message.type == "text") { - let selecttext = tolow(event.message.text); - let get_text = mockText()[selecttext]; + let selecttext = String(event.message.text).toLowerCase(); + let get_text = ContentService.mockText()[selecttext]; if (get_text) { payload = get_text; } @@ -73,21 +63,73 @@ const handleEvent = (event) => { return client.replyMessage(event.replyToken, payload); }; -app.use(bodyParser.json()); +app.use(bodyParser.json()); app.get("/", (req, res) => { res.json({ line: "ok" }); }); -app.get("/push", bodyParser.json(), (req, res) => { +app.post("/push", (req, res) => { + let body = req.body; + let { user_id } = body; + console.log("push =>> body ::", body); let message = { type: "text", - text: `use Push Message! to `, + text: `Push Message! to ${user_id}`, }; - client.pushMessage("Uaac2ca5a02feab67a18d5521b572b5aa", message); + 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); }); +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); +}); + +app.post("/broadcast", async (req, res) => { + let body = req.body; + let { messages } = body; + console.log("body ::", body); + + let resx = await LineService.Broadcast(messages); + console.log("resx", resx); + + res.json({ message: "OK" }); +}); + app.post("/save", bodyParser.json(), async (req, res) => { console.log("saveFile!"); try { @@ -146,172 +188,7 @@ app.post("/save", bodyParser.json(), async (req, res) => { } }); -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 genMsgContent = flexMsg.GenContentFlex; -const flexs = flexMsg.flexs; -// console.log(genMsgContent()); - -const mockText = () => { - return { - flex0: flexs.flex0, - flex1: flexs.flex1, - - bub1: flexs.bub1, - bub2: flexs.bub2, - bub3: flexs.bub3, - bub4: flexs.bub4, - bub5: flexs.bub5, - bub6: flexs.bub6, - - confirm:{ - "type": "template", - "altText": "this is a confirm template", - "template": { - "type": "confirm", - "text": "Are you sure?", - "actions": [ - { - "type": "message", - "label": "Yes", - "text": "yes" - }, - { - "type": "message", - "label": "No", - "text": "no" - } - ] - } - }, - - - 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", - }, - }, - ], - }, - }, - - - - - // - image_carousel: { - "type": "template", - "altText": "this is a image carousel template", - "template": { - "type": "image_carousel", - "columns": [ - { - "imageUrl": "https://example.com/bot/images/item1.jpg", - "action": { - "type": "postback", - "label": "Buy", - "data": "action=buy&itemid=111" - } - }, - { - "imageUrl": "https://example.com/bot/images/item2.jpg", - "action": { - "type": "message", - "label": "Yes", - "text": "yes" - } - }, - { - "imageUrl": "https://example.com/bot/images/item3.jpg", - "action": { - "type": "uri", - "label": "View detail", - "uri": "http://example.com/page/222" - } - } - ] - } - } - - }; -}; diff --git a/indexbk.js b/indexbk.js deleted file mode 100644 index 1d307d3..0000000 --- a/indexbk.js +++ /dev/null @@ -1,166 +0,0 @@ -"use strict"; - -const line = require("@line/bot-sdk"); -const express = require("express"); -const config = require("./config.json"); - -// create LINE SDK client -const client = new line.Client(config); - -const app = express(); - -// webhook callback -app.post("/webhook", line.middleware(config), (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 handleEvent2(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 handleEvent = (event) => { - // const payload = { - // type: "text", - // text: "Hello From PUI", - // }; - - - const payload = { - "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" - } - } - ] - } - } - - return client.replyMessage(event.replyToken, payload); -}; - -// callback function to handle a single event -function handleEvent2(event) { - switch (event.type) { - case 'message': - const message = event.message; - switch (message.type) { - case 'text': - return handleText(message, event.replyToken); - case 'image': - return handleImage(message, event.replyToken); - case 'video': - return handleVideo(message, event.replyToken); - case 'audio': - return handleAudio(message, event.replyToken); - case 'location': - return handleLocation(message, event.replyToken); - case 'sticker': - return handleSticker(message, event.replyToken); - default: - throw new Error(`Unknown message: ${JSON.stringify(message)}`); - } - - case 'follow': - return replyText(event.replyToken, 'Got followed event'); - - case 'unfollow': - return console.log(`Unfollowed this bot: ${JSON.stringify(event)}`); - - case 'join': - return replyText(event.replyToken, `Joined ${event.source.type}`); - - case 'leave': - return console.log(`Left: ${JSON.stringify(event)}`); - - case 'postback': - let data = event.postback.data; - return replyText(event.replyToken, `Got postback: ${data}`); - - case 'beacon': - const dm = `${Buffer.from(event.beacon.dm || '', 'hex').toString('utf8')}`; - return replyText(event.replyToken, `${event.beacon.type} beacon hwid : ${event.beacon.hwid} with device message = ${dm}`); - - default: - throw new Error(`Unknown event: ${JSON.stringify(event)}`); - } -} - -function handleText(message, replyToken) { - return replyText(replyToken, message.text); -} - -function handleImage(message, replyToken) { - return replyText(replyToken, "Got Image"); -} - -function handleVideo(message, replyToken) { - return replyText(replyToken, "Got Video"); -} - -function handleAudio(message, replyToken) { - return replyText(replyToken, "Got Audio"); -} - -function handleLocation(message, replyToken) { - return replyText(replyToken, "Got Location"); -} - -function handleSticker(message, replyToken) { - return replyText(replyToken, "Got Sticker"); -} - -const port = config.port; -app.listen(port, () => { - console.log(`listening on ${port}`); -}); diff --git a/indexv1.js b/indexv1.js deleted file mode 100644 index f516ac0..0000000 --- a/indexv1.js +++ /dev/null @@ -1,553 +0,0 @@ -"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", - }, - }, - ], - }, - }, - }; -}; diff --git a/services/ContentService.js b/services/ContentService.js new file mode 100644 index 0000000..6599c93 --- /dev/null +++ b/services/ContentService.js @@ -0,0 +1,23 @@ + +const flexMsg = require("./flexMsg"); +const genMsgContent = flexMsg.GenContentFlex; +const flexs = flexMsg.flexs; + + +const ContentService = { + mockText: () => { + return { + flex0: flexs.flex0, + flex1: flexs.flex1, + + bub1: flexs.bub1, + bub2: flexs.bub2, + bub3: flexs.bub3, + bub4: flexs.bub4, + bub5: flexs.bub5, + bub6: flexs.bub6, + }; + }, +}; + +module.exports = ContentService; diff --git a/services/LineService.js b/services/LineService.js new file mode 100644 index 0000000..e1c2545 --- /dev/null +++ b/services/LineService.js @@ -0,0 +1,29 @@ +const axios = require("axios").default; +const headers = { + Authorization: + "Bearer be/XHjQ+gMoypZE78Us7hk0h6PA04TyfpQciMOq+B/OVPmumozdhGzYUwopDgsOMCM7RymTK8m++q20GSj3c6B7gZkgEmuGYEYPvc6j+4as6X5bu7tEg+KAZKMfBVDnk+ekpAorC7FMwVPyt2frGRQdB04t89/1O/w1cDnyilFU=", + "Content-Type": "application/json", +}; + +const LineService = { + Broadcast: async (messages) => { + console.log(""); + return new Promise(async (resolve, reject) => { + let URL = "https://api.line.me/v2/bot/message/broadcast"; + let payload = { messages: messages }; + try { + let res = await axios.post(URL, payload, { + headers: headers, + }); + // console.log("res ::", res); + resolve(res); + } catch (e) { + console.log("bbb!"); + console.log("Error ::", e); + reject(null); + } + }); + }, +}; + +module.exports = LineService; diff --git a/services/flexMsg.js b/services/flexMsg.js new file mode 100644 index 0000000..1ec7963 --- /dev/null +++ b/services/flexMsg.js @@ -0,0 +1,749 @@ +const GenContentFlex = (content, text = "0") => { + return { + type: "flex", + altText: "GenContentFlex!" + text, + contents: content, + }; +}; + +const flexs = { + flex0: { + 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", + }, + ], + }, + }, + }, + flex1: GenContentFlex({ + type: "bubble", + body: { + type: "box", + layout: "vertical", + contents: [ + { + type: "text", + text: "hello", + }, + { + type: "text", + text: "world", + }, + ], + }, + }), + + bub1: GenContentFlex({ + type: "bubble", + direction: "ltr", + header: { + type: "box", + layout: "vertical", + contents: [ + { + type: "text", + text: "Header", + align: "center", + contents: [], + }, + ], + }, + hero: { + type: "image", + url: "https://vos.line-scdn.net/bot-designer-template-images/bot-designer-icon.png", + size: "full", + aspectRatio: "1.51:1", + aspectMode: "fit", + }, + body: { + type: "box", + layout: "vertical", + contents: [ + { + type: "text", + text: "Body", + align: "center", + contents: [], + }, + ], + }, + footer: { + type: "box", + layout: "horizontal", + contents: [ + { + type: "button", + action: { + type: "uri", + label: "Button", + uri: "https://linecorp.com", + }, + }, + ], + }, + }), + + bub2: GenContentFlex({ + type: "bubble", + hero: { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_1_cafe.png", + size: "full", + aspectRatio: "20:13", + aspectMode: "cover", + action: { + type: "uri", + label: "Line", + uri: "https://linecorp.com/", + }, + }, + body: { + type: "box", + layout: "vertical", + contents: [ + { + type: "text", + text: "Brown Cafe", + weight: "bold", + size: "xl", + 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: "Place", + size: "sm", + color: "#AAAAAA", + flex: 1, + contents: [], + }, + { + type: "text", + text: "Miraina Tower, 4-1-6 Shinjuku, Tokyo", + size: "sm", + color: "#666666", + flex: 5, + wrap: true, + contents: [], + }, + ], + }, + { + type: "box", + layout: "baseline", + spacing: "sm", + contents: [ + { + type: "text", + text: "Time", + size: "sm", + color: "#AAAAAA", + flex: 1, + contents: [], + }, + { + type: "text", + text: "10:00 - 23:00", + size: "sm", + color: "#666666", + flex: 5, + wrap: true, + contents: [], + }, + ], + }, + ], + }, + ], + }, + footer: { + type: "box", + layout: "vertical", + flex: 0, + spacing: "sm", + contents: [ + { + type: "button", + action: { + type: "uri", + label: "CALL", + uri: "https://linecorp.com", + }, + height: "sm", + style: "link", + }, + { + type: "button", + action: { + type: "uri", + label: "WEBSITE", + uri: "https://linecorp.com", + }, + height: "sm", + style: "link", + }, + { + type: "spacer", + size: "sm", + }, + ], + }, + }), + bub3: GenContentFlex({ + type: "bubble", + header: { + type: "box", + layout: "horizontal", + contents: [ + { + type: "text", + text: "NEWS DIGEST", + weight: "bold", + size: "sm", + color: "#AAAAAA", + contents: [], + }, + ], + }, + hero: { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_4_news.png", + size: "full", + aspectRatio: "20:13", + aspectMode: "cover", + action: { + type: "uri", + label: "Action", + uri: "https://linecorp.com/", + }, + }, + body: { + type: "box", + layout: "horizontal", + spacing: "md", + contents: [ + { + type: "box", + layout: "vertical", + flex: 1, + contents: [ + { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/02_1_news_thumbnail_1.png", + gravity: "bottom", + size: "sm", + aspectRatio: "4:3", + aspectMode: "cover", + }, + { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/02_1_news_thumbnail_2.png", + margin: "md", + size: "sm", + aspectRatio: "4:3", + aspectMode: "cover", + }, + ], + }, + { + type: "box", + layout: "vertical", + flex: 2, + contents: [ + { + type: "text", + text: "7 Things to Know for Today", + size: "xs", + flex: 1, + gravity: "top", + contents: [], + }, + { + type: "separator", + }, + { + type: "text", + text: "Hay fever goes wild", + size: "xs", + flex: 2, + gravity: "center", + contents: [], + }, + { + type: "separator", + }, + { + type: "text", + text: "LINE Pay Begins Barcode Payment Service", + size: "xs", + flex: 2, + gravity: "center", + contents: [], + }, + { + type: "separator", + }, + { + type: "text", + text: "LINE Adds LINE Wallet", + size: "xs", + flex: 1, + gravity: "bottom", + contents: [], + }, + ], + }, + ], + }, + footer: { + type: "box", + layout: "horizontal", + contents: [ + { + type: "button", + action: { + type: "uri", + label: "More", + uri: "https://linecorp.com", + }, + }, + ], + }, + }), + bub4: GenContentFlex({ + type: "bubble", + hero: { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_2_restaurant.png", + size: "full", + aspectRatio: "20:13", + aspectMode: "cover", + action: { + type: "uri", + label: "Action", + uri: "https://linecorp.com", + }, + }, + body: { + type: "box", + layout: "vertical", + spacing: "md", + action: { + type: "uri", + label: "Action", + uri: "https://linecorp.com", + }, + contents: [ + { + type: "text", + text: "Brown's Burger", + weight: "bold", + size: "xl", + contents: [], + }, + { + type: "box", + layout: "vertical", + spacing: "sm", + contents: [ + { + type: "box", + layout: "baseline", + contents: [ + { + type: "icon", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/restaurant_regular_32.png", + }, + { + type: "text", + text: "$10.5", + weight: "bold", + margin: "sm", + contents: [], + }, + { + type: "text", + text: "400kcl", + size: "sm", + color: "#AAAAAA", + align: "end", + contents: [], + }, + ], + }, + { + type: "box", + layout: "baseline", + contents: [ + { + type: "icon", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/restaurant_large_32.png", + }, + { + type: "text", + text: "$15.5", + weight: "bold", + flex: 0, + margin: "sm", + contents: [], + }, + { + type: "text", + text: "550kcl", + size: "sm", + color: "#AAAAAA", + align: "end", + contents: [], + }, + ], + }, + ], + }, + { + type: "text", + text: "Sauce, Onions, Pickles, Lettuce & Cheese", + size: "xxs", + color: "#AAAAAA", + wrap: true, + contents: [], + }, + ], + }, + footer: { + type: "box", + layout: "vertical", + contents: [ + { + type: "spacer", + size: "xxl", + }, + { + type: "button", + action: { + type: "uri", + label: "Add to Cart", + uri: "https://linecorp.com", + }, + color: "#905C44", + style: "primary", + }, + ], + }, + }), + bub5: GenContentFlex({ + type: "bubble", + 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: [], + }, + ], + }, + ], + }, + }), + bub6: GenContentFlex({ + type: "bubble", + hero: { + type: "image", + url: "https://scdn.line-apps.com/n/channel_devcenter/img/fx/01_5_carousel.png", + size: "full", + aspectRatio: "20:13", + aspectMode: "cover", + }, + body: { + type: "box", + layout: "vertical", + spacing: "sm", + contents: [ + { + type: "text", + text: "Arm Chair, White", + weight: "bold", + size: "xl", + wrap: true, + contents: [], + }, + { + type: "box", + layout: "baseline", + contents: [ + { + type: "text", + text: "$49", + weight: "bold", + size: "xl", + flex: 0, + wrap: true, + contents: [], + }, + { + type: "text", + text: ".99", + weight: "bold", + size: "sm", + flex: 0, + wrap: true, + contents: [], + }, + ], + }, + ], + }, + footer: { + type: "box", + layout: "vertical", + spacing: "sm", + contents: [ + { + type: "button", + action: { + type: "uri", + label: "Add to Cart", + uri: "https://linecorp.com", + }, + style: "primary", + }, + { + type: "button", + action: { + type: "uri", + label: "Add to wishlist", + uri: "https://linecorp.com", + }, + }, + ], + }, + }), +}; + +module.exports = { GenContentFlex, flexs }; -- libgit2 0.21.2