Blame view

index.js 5.34 KB
b9184255   Surasit Yerpui   update Code Reweb...
1
"use strict";
a8dde2dd   Surasit Yerpui   firstcommit
2

b9184255   Surasit Yerpui   update Code Reweb...
3
const line = require("@line/bot-sdk");
889c144c   Surasit Yerpui   SW RICH MENU
4
const express = require("express");
b9184255   Surasit Yerpui   update Code Reweb...
5
6
7
const config = require("./config.json");
const bodyParser = require("body-parser");
const axios = require("axios");
889c144c   Surasit Yerpui   SW RICH MENU
8
const fs = require("fs");
b9184255   Surasit Yerpui   update Code Reweb...
9
10
11
12
13
14
15
16
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);
a8dde2dd   Surasit Yerpui   firstcommit
17

a8dde2dd   Surasit Yerpui   firstcommit
18
const app = express();
b9184255   Surasit Yerpui   update Code Reweb...
19
20
21
22
23
24

// 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)) {
a8dde2dd   Surasit Yerpui   firstcommit
25
    return res.status(500).end();
b9184255   Surasit Yerpui   update Code Reweb...
26
  }
a8dde2dd   Surasit Yerpui   firstcommit
27
  // handle events separately
a8dde2dd   Surasit Yerpui   firstcommit
28
29
30
  Promise.all(
    req.body.events.map((event) => {
      console.log("event", event);
b9184255   Surasit Yerpui   update Code Reweb...
31
      // check verify webhook event
a8dde2dd   Surasit Yerpui   firstcommit
32
33
34
35
36
37
38
39
40
41
      if (
        event.replyToken === "00000000000000000000000000000000" ||
        event.replyToken === "ffffffffffffffffffffffffffffffff"
      ) {
        return;
      }
      return handleEvent(event);
    })
  )
    .then(() => res.end())
b9184255   Surasit Yerpui   update Code Reweb...
42
    .catch((err) => {
a8dde2dd   Surasit Yerpui   firstcommit
43
44
45
46
47
48
49
50
51
      console.error(err);
      res.status(500).end();
    });
});

const handleEvent = (event) => {
  let payload = {
    type: "text",
    text: "Hello From PUI",
b9184255   Surasit Yerpui   update Code Reweb...
52
53
54
55
56
57
  };

  if (event.message.type == "text") {
    let selecttext = String(event.message.text).toLowerCase();
    let get_text = ContentService.mockText()[selecttext];
    if (get_text) {
a8dde2dd   Surasit Yerpui   firstcommit
58
      payload = get_text;
b9184255   Surasit Yerpui   update Code Reweb...
59
60
61
62
63
64
65
66
67
68
69
70
    }
  } 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" });
});
a8dde2dd   Surasit Yerpui   firstcommit
71

b9184255   Surasit Yerpui   update Code Reweb...
72
73
app.post("/push", (req, res) => {
  let body = req.body;
889c144c   Surasit Yerpui   SW RICH MENU
74
  let { user_id } = body;
a8dde2dd   Surasit Yerpui   firstcommit
75
  console.log("push =>> body ::", body);
b9184255   Surasit Yerpui   update Code Reweb...
76
  let message = {
a8dde2dd   Surasit Yerpui   firstcommit
77
78
    type: "text",
    text: `Push Message! to ${user_id}`,
b9184255   Surasit Yerpui   update Code Reweb...
79
  };
889c144c   Surasit Yerpui   SW RICH MENU
80
  client.pushMessage(user_id, message);
a8dde2dd   Surasit Yerpui   firstcommit
81
82
  res.json(message);
});
b9184255   Surasit Yerpui   update Code Reweb...
83

f4bbcb63   Surasit Yerpui   เพิ่ม ตัวอย่าง ac...
84
app.post("/multicast", (req, res) => {
b9184255   Surasit Yerpui   update Code Reweb...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  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)}`,
    },
  ];

a8dde2dd   Surasit Yerpui   firstcommit
99
  client.multicast(user_ids, message);
a8dde2dd   Surasit Yerpui   firstcommit
100
  res.json(message);
b9184255   Surasit Yerpui   update Code Reweb...
101
102
103
});

app.post("/multicast", (req, res) => {
a8dde2dd   Surasit Yerpui   firstcommit
104
  let body = req.body;
b9184255   Surasit Yerpui   update Code Reweb...
105
106
107
108
109
  let { user_ids } = body;
  console.log("body ::", body);
  let message = [
    {
      type: "text",
889c144c   Surasit Yerpui   SW RICH MENU
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
      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;
b9184255   Surasit Yerpui   update Code Reweb...
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  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 {
    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: {
f4bbcb63   Surasit Yerpui   เพิ่ม ตัวอย่าง ac...
145
            Authorization:
eb28d472   Surasit Yerpui   update Service
146
              "Bearer be/XHjQ+gMoypZE78Us7hk0h6PA04TyfpQciMOq+B/OVPmumozdhGzYUwopDgsOMCM7RymTK8m++q20GSj3c6B7gZkgEmuGYEYPvc6j+4as6X5bu7tEg+KAZKMfBVDnk+ekpAorC7FMwVPyt2frGRQdB04t89/1O/w1cDnyilFU=",
b9184255   Surasit Yerpui   update Code Reweb...
147
148
149
150
151
152
153
154
          },
        })
          .pipe(fs.createWriteStream(filename))
          .on("close", callback);
      });
    };

    let unquie_file = moment().format("YYYY-MM-DD_HHmmssss");
eb28d472   Surasit Yerpui   update Service
155
    let file_name = `filesave_${unquie_file}`;
889c144c   Surasit Yerpui   SW RICH MENU
156
157
    let message_id = req.body.message_id;
    let URI = `https://api-data.line.me/v2/bot/message/${message_id}/content`;
b9184255   Surasit Yerpui   update Code Reweb...
158
159
160
161
162
163
    console.log("message_id ::", message_id);
    console.log("file_name ::", file_name);

    axios
      .get(URI, {
        headers: {
a8dde2dd   Surasit Yerpui   firstcommit
164
          Authorization:
b9184255   Surasit Yerpui   update Code Reweb...
165
166
167
168
            "Bearer be/XHjQ+gMoypZE78Us7hk0h6PA04TyfpQciMOq+B/OVPmumozdhGzYUwopDgsOMCM7RymTK8m++q20GSj3c6B7gZkgEmuGYEYPvc6j+4as6X5bu7tEg+KAZKMfBVDnk+ekpAorC7FMwVPyt2frGRQdB04t89/1O/w1cDnyilFU=",
        },
      })
      .then(function (response) {
eb28d472   Surasit Yerpui   update Service
169
        // handle success
b9184255   Surasit Yerpui   update Code Reweb...
170
171
172
173
        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;
eb28d472   Surasit Yerpui   update Service
174
175
        console.log("full_file_name =", full_file_name);
        downloadFile(URI, full_file_name, function () {
889c144c   Surasit Yerpui   SW RICH MENU
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
          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);
  }
});

const port = config.port;
b9184255   Surasit Yerpui   update Code Reweb...
192
193
194
app.listen(port, () => {
  console.log(`listening on ${port}`);
});
eb28d472   Surasit Yerpui   update Service

b9184255   Surasit Yerpui   update Code Reweb...

eb28d472   Surasit Yerpui   update Service

b9184255   Surasit Yerpui   update Code Reweb...

eb28d472   Surasit Yerpui   update Service

b9184255   Surasit Yerpui   update Code Reweb...

a8dde2dd   Surasit Yerpui   firstcommit

b9184255   Surasit Yerpui   update Code Reweb...

889c144c   Surasit Yerpui   SW RICH MENU

b9184255   Surasit Yerpui   update Code Reweb...

a8dde2dd   Surasit Yerpui   firstcommit

b9184255   Surasit Yerpui   update Code Reweb...