Skip to content

Instantly share code, notes, and snippets.

@PamornT
Created February 1, 2021 18:09
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 PamornT/1c563b7c12cdc16c72037f9ab2277158 to your computer and use it in GitHub Desktop.
Save PamornT/1c563b7c12cdc16c72037f9ab2277158 to your computer and use it in GitHub Desktop.
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