Commit 889c144c5142699a23c578e3f4ef7aff10e58eaa

Authored by Surasit Yerpui
1 parent b9184255
Exists in master

SW RICH MENU

Showing 1 changed file with 44 additions and 2 deletions   Show diff stats
index.js
1 1 const https = require("https");
2 2 const express = require("express");
3 3 const app = express();
4   -const PORT = process.env.PORT || 3000;
  4 +const PORT = process.env.PORT || 4000;
5 5 const axios = require("axios").default;
6 6 const TOKEN =
7 7 "NYy1k9OM7e/T7PgabnjafdqcxbRc58m7/K4A3kuNCVkoPxhBoZ/Jvg9gaarsqySG2BtvsoQcVgE4yulPaT1WJ7qTUyBaqegae+r8uh0oWrZO93zXIUILn3bOGUFtLmVFcTuBfRKgFHCaJhi1+wFQ6QdB04t89/1O/w1cDnyilFU=";
  8 +const ContentService = require("./services/ContentService");
8 9  
9 10 app.use(express.json());
10 11 app.use(
... ... @@ -70,12 +71,13 @@ const handleEvent = async (event, body) => {
70 71  
71 72 if (event.message.type == "text") {
72 73 let selecttext = tolow(event.message.text);
73   - let get_text = mockText()[selecttext];
  74 + let get_text = ContentService.mockText()[selecttext];
74 75 if (get_text) {
75 76 template_text = get_text;
76 77 }
77 78 } else {
78 79 template_text.text = "Other Message =>>>" + JSON.stringify(event);
  80 + return;
79 81 }
80 82  
81 83 console.log("template_text", template_text);
... ... @@ -105,6 +107,21 @@ app.post("/webhook", function (req, res) {
105 107 // If the user sends a message to your bot, send a reply message
106 108 console.log("req.body.events ::", req.body.events);
107 109  
  110 + if (req.body.events[0].type === "postback") {
  111 + const user_id = req.body.events[0].source.userId;
  112 + console.log(req.body.events[0].postback);
  113 + console.log('user_id ::',user_id)
  114 + console.log(req.body.events[0].postback == "Go MORE")
  115 + if (req.body.events[0].postback.data == "Go MORE") {
  116 + console.log('AA')
  117 + sendSWRichMenu(user_id,"richmenu-a9995ce54d06a8c1438ea96bf74c02aa")
  118 + }
  119 + else if (req.body.events[0].postback.data == "GO BACK") {
  120 + console.log('BB')
  121 + sendSWRichMenu(user_id,"richmenu-589b58f237a7042a57a4cf1698895d18")
  122 + }
  123 + }
  124 +
108 125 if (req.body.events[0].type === "message") {
109 126 // Message data, must be stringified
110 127 const dataString = JSON.stringify({
... ... @@ -136,6 +153,8 @@ app.post("/webhook", function (req, res) {
136 153 body: dataString,
137 154 };
138 155  
  156 +
  157 +
139 158 // Define request
140 159 const request = https.request(webhookOptions, (res) => {
141 160 res.on("data", (d) => {
... ... @@ -154,6 +173,22 @@ app.post("/webhook", function (req, res) {
154 173 }
155 174 });
156 175  
  176 +const sendSWRichMenu = async (user_id,richmenu_id) => {
  177 + // Request header
  178 + const headers = {
  179 + "Content-Type": "application/json",
  180 + Authorization: "Bearer " + TOKEN,
  181 + };
  182 +
  183 + const URL = `https://api.line.me/v2/bot/user/${user_id}/richmenu/${richmenu_id}`;
  184 + console.log('URL ::',URL);
  185 + let res = await axios.post(URL,{}, {
  186 + headers: headers,
  187 + }).then(res=>{
  188 + console.log('res ::',res.data);
  189 + })
  190 +};
  191 +
157 192 app.listen(PORT, () => {
158 193 console.log(`Example app listening at http://localhost:${PORT}`);
159 194 });
... ... @@ -222,6 +257,13 @@ const mockText = () => {
222 257 label: "Send location",
223 258 },
224 259 },
  260 + {
  261 + type: "action",
  262 + action: {
  263 + type: "camera",
  264 + label: "Camera",
  265 + },
  266 + },
225 267 ],
226 268 },
227 269 },
... ...