Skip to content

Instantly share code, notes, and snippets.

@tandevmode
Created June 7, 2020 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tandevmode/d1a0d3580245af346f38c9f29bd3c85b to your computer and use it in GitHub Desktop.
Save tandevmode/d1a0d3580245af346f38c9f29bd3c85b to your computer and use it in GitHub Desktop.
const dotenv = require("dotenv");
dotenv.config();
const express = require("express");
const lineApp = require("./lineapp");
const spotify = require("./spotify");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server available on port ${port}`);
});
app.post("/webhook", async (req, res) => {
let event = req.body.events[0];
let message;
if (event.type === 'message' && event.message.type === 'text') {
// ค้นหาเพลงจากข้อความที่ผู้ใช้พิมพ์ชื่อเพลง หรือชื่อศิลปินเข้ามา
let searchInput = event.message.text;
message = await lineApp.searchMusic(searchInput);
} else if (event.type === 'postback') {
// กรณีผู้ใช้กดปุ่ม Add (เพื่อเพิ่มเพลง) หรือกดปุ่ม More (เพื่อค้นหาเพลงเพิ่มเติม)
message = await lineApp.receivedPostback(event)
}
await lineApp.replyMessage(event.replyToken, message);
return res.status(200).send(req.method);
});
app.get("/spotify", (req, res) => {
// รอรับ Callbak URL หลังจากผู้ใช้ทำการ Login ก็ให้เริ่มเชื่อมต่อกับ Spotify
spotify.receivedAuthCode(req.query.code);
res.status(200).send("Login Successfully!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment