diff --git a/index.js b/index.js index a7e1bfa..37fa771 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,11 @@ const https = require("https"); const express = require("express"); const app = express(); -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 4000; const axios = require("axios").default; const TOKEN = "NYy1k9OM7e/T7PgabnjafdqcxbRc58m7/K4A3kuNCVkoPxhBoZ/Jvg9gaarsqySG2BtvsoQcVgE4yulPaT1WJ7qTUyBaqegae+r8uh0oWrZO93zXIUILn3bOGUFtLmVFcTuBfRKgFHCaJhi1+wFQ6QdB04t89/1O/w1cDnyilFU="; +const ContentService = require("./services/ContentService"); app.use(express.json()); app.use( @@ -70,12 +71,13 @@ const handleEvent = async (event, body) => { if (event.message.type == "text") { let selecttext = tolow(event.message.text); - let get_text = mockText()[selecttext]; + let get_text = ContentService.mockText()[selecttext]; if (get_text) { template_text = get_text; } } else { template_text.text = "Other Message =>>>" + JSON.stringify(event); + return; } console.log("template_text", template_text); @@ -105,6 +107,21 @@ app.post("/webhook", function (req, res) { // 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 === "postback") { + const user_id = req.body.events[0].source.userId; + console.log(req.body.events[0].postback); + console.log('user_id ::',user_id) + console.log(req.body.events[0].postback == "Go MORE") + if (req.body.events[0].postback.data == "Go MORE") { + console.log('AA') + sendSWRichMenu(user_id,"richmenu-a9995ce54d06a8c1438ea96bf74c02aa") + } + else if (req.body.events[0].postback.data == "GO BACK") { + console.log('BB') + sendSWRichMenu(user_id,"richmenu-589b58f237a7042a57a4cf1698895d18") + } + } + if (req.body.events[0].type === "message") { // Message data, must be stringified const dataString = JSON.stringify({ @@ -136,6 +153,8 @@ app.post("/webhook", function (req, res) { body: dataString, }; + + // Define request const request = https.request(webhookOptions, (res) => { res.on("data", (d) => { @@ -154,6 +173,22 @@ app.post("/webhook", function (req, res) { } }); +const sendSWRichMenu = async (user_id,richmenu_id) => { + // Request header + const headers = { + "Content-Type": "application/json", + Authorization: "Bearer " + TOKEN, + }; + + const URL = `https://api.line.me/v2/bot/user/${user_id}/richmenu/${richmenu_id}`; + console.log('URL ::',URL); + let res = await axios.post(URL,{}, { + headers: headers, + }).then(res=>{ + console.log('res ::',res.data); + }) +}; + app.listen(PORT, () => { console.log(`Example app listening at http://localhost:${PORT}`); }); @@ -222,6 +257,13 @@ const mockText = () => { label: "Send location", }, }, + { + type: "action", + action: { + type: "camera", + label: "Camera", + }, + }, ], }, }, -- libgit2 0.21.2