Skip to content

Instantly share code, notes, and snippets.

@taichunmin

taichunmin/LICENSE

Last active Apr 7, 2020
Embed
What would you like to do?
const _ = require('lodash')
const Line = require('@line/bot-sdk').Client
const senders = {
Brown: {
name: 'Brown',
iconUrl: 'https://stickershop.line-scdn.net/stickershop/v1/sticker/52002734/iPhone/sticker_key@2x.png'
},
Cony: {
name: 'Cony',
iconUrl: 'https://stickershop.line-scdn.net/stickershop/v1/sticker/52002735/iPhone/sticker_key@2x.png'
},
Sally: {
name: 'Sally',
iconUrl: 'https://stickershop.line-scdn.net/stickershop/v1/sticker/52002736/iPhone/sticker_key@2x.png'
}
}
const errToString = err => {
const debug = {}
_.each([
'args',
'code',
'message',
'name',
'originalError.response.data',
'raw',
'stack',
'status',
'statusCode',
'statusMessage',
], key => {
if (_.hasIn(err, key)) _.set(debug, key, _.get(err, key))
})
return JSON.stringify(debug)
}
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.main = async (req, res) => {
try {
// get access token
const channelAccessToken = req.path.substring(1)
if (!/^[a-zA-Z0-9+/=]+$/.test(channelAccessToken)) throw new Error('wrong channel access token')
const line = new Line({ channelAccessToken })
const events = _.get(req, 'body.events', [])
await Promise.all(_.map(events, async event => {
if (!event.replyToken) return // unfollow
if (_.get(event, 'source.userId') === 'Udeadbeefdeadbeefdeadbeefdeadbeef') return // webhook verify
let messages, url, sender
try {
url = _.get(event, 'message.contentProvider.originalContentUrl')
if (url) sender = senders[new URL(url).searchParams.get('sender')] || null
if (sender) messages = { type: 'text', text: `Hello, I am ${sender.name}!!`, sender }
if (!messages) messages = { type: 'text', text: `這個是「如何在 LIFF 傳送隱藏資料給機器人」的範例程式\n\n選擇 sender 請打開此 LIFF 連結: https://liff.line.me/1654046335-DzXpM8mx\n\n查看文章請點此: https://taichunmin.idv.tw/blog/2020-04-07-line-liff-send-hidden-data.html` }
await line.replyMessage(event.replyToken, messages)
} catch (err) {
console.log('event error =', errToString(err))
try {
await line.replyMessage(event.replyToken, messageText(errToString(err)))
} catch (err) {}
}
}))
res.status(200).send('OK')
} catch (err) {
console.log('error =', errToString(err))
res.status(err.status || 500).send(err.message)
}
}
MIT License
Copyright (c) 2020 戴均民
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{
"name": "line-liff-send-hidden-data",
"version": "1.0.0",
"main": "index.js",
"author": "taichunmin <taichunmin@gmail.com>",
"license": "MIT",
"dependencies": {
"@line/bot-sdk": "^6.8.4",
"lodash": "^4.17.15"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.