Created
February 1, 2021 18:09
-
-
Save PamornT/1c563b7c12cdc16c72037f9ab2277158 to your computer and use it in GitHub Desktop.
This file contains 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
export const webhookLINE = functions.region(REGION).https.onRequest( async(req, res) => { | |
console.log('🚀 Start Webhook!!', JSON.stringify(req.body) ) | |
if(!req.body.events) { res.status(200).end()} | |
const event = req.body.events[0] | |
if(typeof(event) === 'undefined') { res.status(200).end()} | |
try { | |
await util_line.verify_signature(req, res) | |
let replyMessage: any = null | |
switch (event.type) { | |
case 'message': | |
if (event.message.type === 'text') { | |
const message = event.message.text | |
if(message.toLowerCase() === 'trello') { | |
replyMessage = await getBoardPayload() | |
} | |
if(replyMessage) { | |
await util_line.reply(event.replyToken, replyMessage) | |
} | |
} | |
break | |
case 'postback': | |
break | |
default: | |
} | |
} catch (err) { | |
console.error(err) | |
res.status(500).end() | |
} | |
res.status(200) | |
}) | |
const getBoardPayload = async() => { | |
const data = await firestore.collection('board').orderBy('changeTime', 'desc').limit(13).get() | |
if (!data.empty) { | |
const payloadBoardList = data.docs.map( (doc:any) => { | |
const {id, name, changeTime} = doc.data() | |
return msgTrello.boardItem(id, name, formatTime(changeTime)) | |
}) | |
return msgTrello.boardMain(payloadBoardList) | |
} else { | |
return msgTrello.boardEmpty() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment