app.js 1.61 KB
const express = require('express');
const axios = require('axios').default;
const FormData = require('form-data');

const constants = require('./constants');

const app = express();
app.use(express.json());

app.get('/', (req, res) => {
  res.send('Hello world!');
});

app.get('/notify', (req, res) => {
  const url = 'https://notify-api.line.me/api/notify'
  const form = new FormData();
  form.append('message', 'Line Notify Testing...');
  form.append('stickerPackageId', 1);
  form.append('stickerId', 1);

  axios.post(url, form, { headers: form.getHeaders({ authorization: `Bearer ${constants.LINE.TOKEN}` })})
    .then(notifyResponse => {
      console.log(notifyResponse);
      res.status(200);
      res.send('Request Success');
    }).catch(err => {
      console.log(err);
      res.send('Request Error');
    });
})

app.post('/webhook', (req, res) => {
  const { body } = req;
  const eventObj = body.events[0];
  const { source, message, replyToken } = eventObj;

  // request log
  console.log('======================', new Date() ,'======================');
  console.log('[request]', body);
  console.log('[request source] ', source);
  console.log('[request message]', message);

  const url = 'https://notify-api.line.me/api/notify'
  axios.post(url, {
    replyToken,
    messages: [
      {
        type: 'text',
        text: 'Hello!',
      },
      {
        type: 'text',
        text: 'May I Help U?',
      },
    ],
  }, {
    headers: `Bearer ${constants.LINE.TOKEN}`,
  }).then(notifyResponse => {
    res.status(200);
    res.send('Success');
  }).catch(err => {
    res.send('Failed');
  });
});

module.exports = app;