Created
October 30, 2019 04:19
-
-
Save moomdate/45ef2e659269fb802909331a05bf8940 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "use strict"; | |
| const line = require("@line/bot-sdk"); | |
| const express = require("express"); | |
| const config = require("./config.json"); | |
| const axios = require("axios"); | |
| // create LINE SDK client | |
| const client = new line.Client(config); | |
| const app = express(); | |
| // webhook callback | |
| app.post("/webhook", line.middleware(config), (req, res) => { | |
| // req.body.events should be an array of events | |
| if (!Array.isArray(req.body.events)) { | |
| return res.status(500).end(); | |
| } | |
| // handle events separately | |
| Promise.all( | |
| req.body.events.map(event => { | |
| console.log("event", event); | |
| // check verify webhook event | |
| if ( | |
| event.replyToken === "00000000000000000000000000000000" || | |
| event.replyToken === "ffffffffffffffffffffffffffffffff" | |
| ) { | |
| return; | |
| } | |
| return handleEvent(event); | |
| }) | |
| ) | |
| .then(() => res.end()) | |
| .catch(err => { | |
| console.error(err); | |
| res.status(500).end(); | |
| }); | |
| }); | |
| // simple reply function | |
| const replyText = (token, texts) => { | |
| texts = Array.isArray(texts) ? texts : [texts]; | |
| return client.replyMessage( | |
| token, | |
| texts.map(text => ({ type: "text", text })) | |
| ); | |
| }; | |
| let headers = { | |
| "Content-Type": "application/json", | |
| Authorization: "Bearer token" | |
| }; | |
| const replyFlex = (token, texts) => { | |
| //const msg = texts; | |
| let flex_msg = { | |
| replyToken: token, | |
| messages: [texts] | |
| }; | |
| axios | |
| .post("https://api.line.me/v2/bot/message/reply", flex_msg, { | |
| headers: headers | |
| }) | |
| .then(res => { | |
| console.log(res); | |
| }) | |
| .catch(error => { | |
| console.log(error); | |
| }); | |
| }; | |
| // callback function to handle a single event | |
| function handleEvent(event) { | |
| switch (event.type) { | |
| case "message": | |
| const message = event.message; | |
| switch (message.type) { | |
| case "text": | |
| return handleText(message, event.replyToken); | |
| case "image": | |
| return handleImage(message, event.replyToken); | |
| case "video": | |
| return handleVideo(message, event.replyToken); | |
| case "audio": | |
| return handleAudio(message, event.replyToken); | |
| case "location": | |
| return handleLocation(message, event.replyToken); | |
| case "sticker": | |
| return handleSticker(message, event.replyToken); | |
| default: | |
| throw new Error(`Unknown message: ${JSON.stringify(message)}`); | |
| } | |
| case "follow": | |
| return replyText(event.replyToken, "Got followed event"); | |
| case "unfollow": | |
| return console.log(`Unfollowed this bot: ${JSON.stringify(event)}`); | |
| case "join": | |
| return replyText(event.replyToken, `Joined ${event.source.type}`); | |
| case "leave": | |
| return console.log(`Left: ${JSON.stringify(event)}`); | |
| case "postback": | |
| let data = event.postback.data; | |
| return replyText(event.replyToken, `Got postback: ${data}`); | |
| case "beacon": | |
| const dm = `${Buffer.from(event.beacon.dm || "", "hex").toString( | |
| "utf8" | |
| )}`; | |
| return replyText( | |
| event.replyToken, | |
| `${event.beacon.type} beacon hwid : ${event.beacon.hwid} with device message = ${dm}` | |
| ); | |
| default: | |
| throw new Error(`Unknown event: ${JSON.stringify(event)}`); | |
| } | |
| } | |
| function getTicket() { | |
| return { | |
| type: "flex", | |
| altText: "Flex Message", | |
| contents: { | |
| type: "bubble", | |
| hero: { | |
| type: "image", | |
| url: | |
| "https://static.posttoday.com/media/content/2018/12/23/D484B8DD44154064A9F5ED2D8B079816.jpg", | |
| size: "full", | |
| aspectRatio: "20:13", | |
| aspectMode: "cover", | |
| action: { | |
| type: "uri", | |
| label: "Action", | |
| uri: "https://linecorp.com" | |
| } | |
| }, | |
| body: { | |
| type: "box", | |
| layout: "vertical", | |
| spacing: "md", | |
| action: { | |
| type: "uri", | |
| label: "Action", | |
| uri: "https://linecorp.com" | |
| }, | |
| contents: [ | |
| { | |
| type: "text", | |
| text: "Super tu", | |
| size: "xl", | |
| weight: "bold" | |
| }, | |
| { | |
| type: "box", | |
| layout: "vertical", | |
| spacing: "sm", | |
| contents: [ | |
| { | |
| type: "box", | |
| layout: "baseline", | |
| contents: [ | |
| { | |
| type: "icon", | |
| url: | |
| "https://scdn.line-apps.com/n/channel_devcenter/img/fx/restaurant_regular_32.png" | |
| }, | |
| { | |
| type: "text", | |
| text: "$10.5", | |
| flex: 0, | |
| margin: "sm", | |
| weight: "bold" | |
| }, | |
| { | |
| type: "text", | |
| text: "400kcl", | |
| size: "sm", | |
| align: "end", | |
| color: "#AAAAAA" | |
| } | |
| ] | |
| }, | |
| { | |
| type: "box", | |
| layout: "baseline", | |
| contents: [ | |
| { | |
| type: "icon", | |
| url: | |
| "https://scdn.line-apps.com/n/channel_devcenter/img/fx/restaurant_large_32.png" | |
| }, | |
| { | |
| type: "text", | |
| text: "$15.5", | |
| flex: 0, | |
| margin: "sm", | |
| weight: "bold" | |
| }, | |
| { | |
| type: "text", | |
| text: "550kcl", | |
| size: "sm", | |
| align: "end", | |
| color: "#AAAAAA" | |
| } | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| type: "text", | |
| text: "Sauce, Onions, Pickles, Lettuce & Cheese", | |
| size: "xxs", | |
| color: "#AAAAAA", | |
| wrap: true | |
| } | |
| ] | |
| }, | |
| footer: { | |
| type: "box", | |
| layout: "vertical", | |
| contents: [ | |
| { | |
| type: "spacer", | |
| size: "xxl" | |
| }, | |
| { | |
| type: "button", | |
| action: { | |
| type: "uri", | |
| label: "Add to Cart", | |
| uri: "https://linecorp.com" | |
| }, | |
| color: "#905C44", | |
| style: "primary" | |
| } | |
| ] | |
| } | |
| } | |
| }; | |
| } | |
| function handleText(message, replyToken) { | |
| return replyText(replyToken, message.text); | |
| } | |
| function handleImage(message, replyToken) { | |
| return replyText(replyToken, "Got Image"); | |
| } | |
| function handleVideo(message, replyToken) { | |
| return replyText(replyToken, "Got Video"); | |
| } | |
| function handleAudio(message, replyToken) { | |
| return replyText(replyToken, "Got Audio"); | |
| } | |
| function handleLocation(message, replyToken) { | |
| return replyText(replyToken, "Got Location"); | |
| } | |
| function handleSticker(message, replyToken) { | |
| const msg = getTicket(); | |
| return replyFlex(replyToken, msg); | |
| } | |
| const port = config.port; | |
| app.listen(port, () => { | |
| console.log(`listening on ${port}`); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment