Last active
January 8, 2021 07:02
-
-
Save tandevmode/e5045de4e811aa979f86fbf462e8c043 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
const express = require("express"); | |
const bodyParser = require("body-parser"); | |
const crypto = require('crypto'); | |
const request = require("request-promise"); | |
const LINE_MESSAGING_API = 'https://api.line.me/v2/bot/message'; | |
const LINE_CHANNEL_SECRET = 'XXXXXXXXXXXXX'; | |
const LINE_HEADER = { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer XXXXXXXXXXXXXXXXXXX` | |
}; | |
const app = express(); | |
app.use(bodyParser.json()); | |
app.listen(3000, () => { | |
console.log(`Server available on port 3000`); | |
}); | |
app.post("/webhook", async (req, res) => { | |
const body = JSON.stringify(req.body); | |
const signature = crypto.createHmac('SHA256', LINE_CHANNEL_SECRET).update(body).digest('base64').toString(); | |
if (signature !== req.headers['x-line-signature']) { | |
return res.status(401).send('Unauthorized'); | |
} | |
let event = req.body.events[0]; | |
if (event.type === 'message' && event.message.type === 'sticker') { | |
let keywords = event.message.keywords; | |
let stickerIntent = ""; | |
for (let i = 0; i <= 2; i++) { | |
stickerIntent += randomItem(keywords) + " "; | |
} | |
const newBody = { | |
destination: req.body.destination, | |
events: [{ | |
timestamp: req.body.events[0].timestamp, | |
mode: req.body.events[0].mode, | |
source: req.body.events[0].source, | |
replyToken: req.body.events[0].replyToken, | |
type: 'message', | |
message: { type: 'text', id: req.body.events[0].message.id, text: stickerIntent } | |
}] | |
}; | |
req.body = newBody; | |
req.headers["x-line-signature"] = crypto.createHmac('SHA256', LINE_CHANNEL_SECRET).update(JSON.stringify(newBody)).digest('base64').toString(); | |
req.headers["content-length"] = Buffer.byteLength(JSON.stringify(newBody), 'utf8') | |
postToDialogflow(req); | |
} else if (event.type === 'message' && event.message.type === 'text') { | |
postToDialogflow(req); | |
} else { | |
reply(req); | |
} | |
return res.status(200).send(req.method); | |
}); | |
function randomItem(items) { | |
return items[Math.floor(Math.random() * items.length)]; | |
} | |
const postToDialogflow = req => { | |
const body = JSON.stringify(req.body); | |
req.headers.host = "dialogflow.cloud.google.com"; | |
return request.post({ | |
uri: "https://dialogflow.cloud.google.com/v1/integrations/line/webhook/xxxxxx-xxxxxxxx", | |
headers: req.headers, | |
body: body | |
}); | |
}; | |
const reply = req => { | |
return request.post({ | |
uri: `${LINE_MESSAGING_API}/reply`, | |
headers: LINE_HEADER, | |
body: JSON.stringify({ | |
replyToken: req.body.events[0].replyToken, | |
messages: [ | |
{ | |
type: "text", | |
text: "Sorry, this chatbot did not support message type " + req.body.events[0].message.type | |
} | |
] | |
}) | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment