Commit f4bbcb63ef640169aa5f8d6876eaee8e59548dba

Authored by Surasit Yerpui
1 parent eb28d472
Exists in master

เพิ่ม ตัวอย่าง action

exampleMassage/action.json 0 → 100644
... ... @@ -0,0 +1,59 @@
  1 +{
  2 + "action": {
  3 + "type": "text",
  4 + "text": "Hello Quick Reply!",
  5 + "quickReply": {
  6 + "items": [
  7 + {
  8 + "type": "action",
  9 + "action": {
  10 + "type": "uri",
  11 + "label": "Add to Cart",
  12 + "uri": "https://developers.line.me"
  13 + }
  14 + },
  15 + {
  16 + "type": "action",
  17 + "action": {
  18 + "type": "postback",
  19 + "label": "Postback",
  20 + "data": "action=buy&itemid=123"
  21 + }
  22 + },
  23 + {
  24 + "type": "action",
  25 + "action": {
  26 + "type": "datetimepicker",
  27 + "label": "Datetime Picker",
  28 + "data": "storeId=12345",
  29 + "mode": "datetime",
  30 + "initial": "2018-09-11T00:00",
  31 + "max": "2018-12-31T23:59",
  32 + "min": "2018-01-01T00:00"
  33 + }
  34 + },
  35 + {
  36 + "type": "action",
  37 + "action": {
  38 + "type": "camera",
  39 + "label": "Camera"
  40 + }
  41 + },
  42 + {
  43 + "type": "action",
  44 + "action": {
  45 + "type": "cameraRoll",
  46 + "label": "Gallery"
  47 + }
  48 + },
  49 + {
  50 + "type": "action",
  51 + "action": {
  52 + "type": "location",
  53 + "label": "Location"
  54 + }
  55 + }
  56 + ]
  57 + }
  58 + }
  59 +}
0 60 \ No newline at end of file
... ...
index.js
... ... @@ -18,12 +18,14 @@ const client = new line.Client(config);
18 18 const app = express();
19 19  
20 20 // webhook callback
  21 +let debugMode = false;
21 22 app.use("/webhook", line.middleware(config));
22 23 app.post("/webhook", (req, res) => {
23 24 // req.body.events should be an array of events
24 25 if (!Array.isArray(req.body.events)) {
25 26 return res.status(500).end();
26 27 }
  28 + console.log('req.body.events !',req.body.events)
27 29 // handle events separately
28 30 Promise.all(
29 31 req.body.events.map((event) => {
... ... @@ -51,7 +53,7 @@ const handleEvent = (event) => {
51 53 text: "Hello From PUI",
52 54 };
53 55  
54   - if (event.message.type == "text") {
  56 + if (event.type == "message" && event.message.type == "text") {
55 57 let selecttext = String(event.message.text).toLowerCase();
56 58 let get_text = ContentService.mockText()[selecttext];
57 59 if (get_text) {
... ... @@ -61,6 +63,8 @@ const handleEvent = (event) => {
61 63 payload.text = "Other Message =>>>" + JSON.stringify(event);
62 64 }
63 65  
  66 + console.log("SEND TO ==> " + JSON.stringify(payload));
  67 +
64 68 return client.replyMessage(event.replyToken, payload);
65 69 };
66 70  
... ... @@ -71,12 +75,14 @@ app.get("/", (req, res) => {
71 75  
72 76 app.post("/push", (req, res) => {
73 77 let body = req.body;
74   - let { user_id } = body;
  78 + let { user_id, message } = body;
75 79 console.log("push =>> body ::", body);
76   - let message = {
77   - type: "text",
78   - text: `Push Message! to ${user_id}`,
79   - };
  80 + if (!message) {
  81 + message = {
  82 + type: "text",
  83 + text: `Push Message! to ${user_id}`,
  84 + };
  85 + }
80 86 client.pushMessage(user_id, message);
81 87 res.json(message);
82 88 });
... ...
services/ContentService.js
1   -
2 1 const flexMsg = require("./flexMsg");
  2 +const { action } = require("../exampleMassage/action.json");
3 3 const genMsgContent = flexMsg.GenContentFlex;
4 4 const flexs = flexMsg.flexs;
5 5  
6   -
7 6 const ContentService = {
8 7 mockText: () => {
9 8 return {
... ... @@ -16,6 +15,7 @@ const ContentService = {
16 15 bub4: flexs.bub4,
17 16 bub5: flexs.bub5,
18 17 bub6: flexs.bub6,
  18 + action: action,
19 19 };
20 20 },
21 21 };
... ...
test.json 0 → 100644
... ... @@ -0,0 +1,53 @@
  1 +{
  2 + "type": "flex",
  3 + "altText": "GenContentFlex!0",
  4 + "contents": {
  5 + "type": "bubble",
  6 + "direction": "ltr",
  7 + "header": {
  8 + "type": "box",
  9 + "layout": "vertical",
  10 + "contents": [
  11 + {
  12 + "type": "text",
  13 + "text": "Header",
  14 + "align": "center",
  15 + "contents": []
  16 + }
  17 + ]
  18 + },
  19 + "hero": {
  20 + "type": "image",
  21 + "url": "https://1417094351.rsc.cdn77.org/articles/1439/1438984/thumbnail/small.gif?3",
  22 + "size": "full",
  23 + "aspectRatio": "1.51:1",
  24 + "aspectMode": "fit"
  25 + },
  26 + "body": {
  27 + "type": "box",
  28 + "layout": "vertical",
  29 + "contents": [
  30 + {
  31 + "type": "text",
  32 + "text": "Body",
  33 + "align": "center",
  34 + "contents": []
  35 + }
  36 + ]
  37 + },
  38 + "footer": {
  39 + "type": "box",
  40 + "layout": "horizontal",
  41 + "contents": [
  42 + {
  43 + "type": "button",
  44 + "action": {
  45 + "type": "uri",
  46 + "label": "Button",
  47 + "uri": "https://linecorp.com"
  48 + }
  49 + }
  50 + ]
  51 + }
  52 + }
  53 +}
0 54 \ No newline at end of file
... ...