const https = require("https"); const express = require("express"); const app = express(); const PORT = process.env.PORT || 3000; const axios = require("axios").default; const TOKEN = "NYy1k9OM7e/T7PgabnjafdqcxbRc58m7/K4A3kuNCVkoPxhBoZ/Jvg9gaarsqySG2BtvsoQcVgE4yulPaT1WJ7qTUyBaqegae+r8uh0oWrZO93zXIUILn3bOGUFtLmVFcTuBfRKgFHCaJhi1+wFQ6QdB04t89/1O/w1cDnyilFU="; app.use(express.json()); app.use( express.urlencoded({ extended: true, }) ); const flexMsg = require("./flexMsg"); app.get("/", (req, res) => { res.sendStatus(200); }); app.post("/Rewebhook", async (req, res) => { console.log("Rewebhook ::", req.body); res.json({ OK: req.body }); if (!Array.isArray(req.body.events)) { return res.status(500).end(); } 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, req.body); }) ) .then(() => res.end()) .catch((err) => { console.error(err); res.status(500).end(); }); }); const handleEvent = async (event, body) => { const { Channel_access_token } = body; const { replyToken } = event; const _headers = { Authorization: "Bearer " + Channel_access_token, "Content-Type": "application/json", }; const messages = [ { type: "text", text: "Hello, user", }, { type: "text", text: "May I help you?", }, ]; let template_text = null; if (event.message.type == "text") { let selecttext = tolow(event.message.text); let get_text = mockText()[selecttext]; if (get_text) { template_text = get_text; } } else { template_text.text = "Other Message =>>>" + JSON.stringify(event); } console.log("template_text", template_text); let URL = "https://api.line.me/v2/bot/message/reply"; let payload = { replyToken: replyToken, messages: template_text ? [template_text] : messages, }; console.log("payload ::", payload); try { let res = await axios.post(URL, payload, { headers: _headers, }); console.log("res ::", res.data); } catch (e) { console.log("Error Reply::", e); } }; const tolow = (str) => { return str.toLowerCase(); }; app.post("/webhook", function (req, res) { res.send("HTTP POST request sent to the webhook URL!"); // If the user sends a message to your bot, send a reply message console.log("req.body.events ::", req.body.events); if (req.body.events[0].type === "message") { // Message data, must be stringified const dataString = JSON.stringify({ replyToken: req.body.events[0].replyToken, messages: [ { type: "text", text: "Hello, user", }, { type: "text", text: "May I help you?", }, ], }); // Request header const headers = { "Content-Type": "application/json", Authorization: "Bearer " + TOKEN, }; // Options to pass into the request const webhookOptions = { hostname: "api.line.me", path: "/v2/bot/message/reply", method: "POST", headers: headers, body: dataString, }; // Define request const request = https.request(webhookOptions, (res) => { res.on("data", (d) => { process.stdout.write(d); }); }); // Handle error request.on("error", (err) => { console.error(err); }); // Send data request.write(dataString); request.end(); } }); app.listen(PORT, () => { console.log(`Example app listening at http://localhost:${PORT}`); }); const flexs = flexMsg.flexs; 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://image.sistacafe.com/images/uploads/summary/image/26097/cat-200313-020.jpg", action: { type: "postback", label: "Buy", data: "action=buy&itemid=111", }, }, { imageUrl: "https://image.sistacafe.com/images/uploads/summary/image/26097/cat-200313-020.jpg", action: { type: "message", label: "Yes", text: "yes", }, }, { imageUrl: "https://image.sistacafe.com/images/uploads/summary/image/26097/cat-200313-020.jpg", action: { type: "uri", label: "View detail", uri: "http://example.com/page/222", }, }, ], }, }, }; };