Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Last active October 11, 2023 16:49
Show Gist options
  • Save taichunmin/684839958fed6ff9b4f368c3d23a385b to your computer and use it in GitHub Desktop.
Save taichunmin/684839958fed6ff9b4f368c3d23a385b to your computer and use it in GitHub Desktop.
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"
}
}
@arguskao
Copy link

在 LINE 的聊天機器人中,如果你希望使用者按下按鈕後看不到你傳的資料,你可以使用 postback 這個 action 來傳送隱藏資料,同時你也會拿到一個 replyToken 讓你回傳訊息。\

請問程式裡面怎麼沒有提到postback?

@taichunmin
Copy link
Author

因為 postback 沒辦法在 LIFF 中使用,這個程式只是為了模擬 postback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment