Skip to content

Instantly share code, notes, and snippets.

@tandevmode
Created November 1, 2019 11:11
Show Gist options
  • Save tandevmode/27dcc6b6e6e9b65b6c07b7622c671f6d to your computer and use it in GitHub Desktop.
Save tandevmode/27dcc6b6e6e9b65b6c07b7622c671f6d to your computer and use it in GitHub Desktop.
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp();
const request = require("request-promise");
const crypto = require('crypto');
const runtimeOpts = { timeoutSeconds: 4, memory: "2GB" };
const REGION = "asia-northeast1";
const LINE_MESSAGING_API = "https://api.line.me/v2/bot/message";
const LINE_CHANNEL_SECRET = "Your-Channel-Secret";
const GROUP_ID = "Caf6...";
const LINE_HEADER = {
"Content-Type": "application/json",
Authorization: "Bearer Your-Channel-Access-Token"
};
exports.LineWebhook = functions.region(REGION).runWith(runtimeOpts).https.onRequest(async (req, res) => {
const text = JSON.stringify(req.body);
const signature = crypto.createHmac('SHA256', LINE_CHANNEL_SECRET).update(text).digest('base64').toString();
if (signature !== req.headers['x-line-signature']) {
return res.status(401).send('Unauthorized');
}
let event = req.body.events[0];
if (event.message.type === 'text') {
let input = event.message.text;
await admin.firestore().collection('translations').doc('inputText').set({
input: input
}).then(function () {
console.log("Document successfully written!");
}).catch(function (error) {
console.error("Error writing document: ", error);
});
}
return res.status(200).send(req.method);;
});
exports.LineBotPush = functions.region(REGION).runWith(runtimeOpts).firestore.document('translations/inputText').onWrite(async (change, context) => {
let latest = change.after.data();
let input = latest.input;
let containsJapanese = input.match(/[\u3000-\u303f\u3040-\u309f\u30a0-\u30ff\uff00-\uff9f\u4e00-\u9faf\u3400-\u4dbf]/);
if (containsJapanese) {
push(GROUP_ID, latest.translated.th);
} else {
push(GROUP_ID, latest.translated.ja);
}
});
const push = (userId, msg) => {
return request.post({
headers: LINE_HEADER,
uri: `${LINE_MESSAGING_API}/push`,
body: JSON.stringify({
to: userId,
messages: [{ type: "text", text: msg }]
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment