Skip to content

Instantly share code, notes, and snippets.

@taichunmin
Last active December 10, 2019 14:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save taichunmin/5f1964d914d750f0ed7e6dff27ecf5ea to your computer and use it in GitHub Desktop.
Google Cloud Function for LINE pushMessage https://hackmd.io/@taichunmin/B1rUayhjr
const axios = require('axios')
exports.helloWorld = async (req, res) => {
res.set('Access-Control-Allow-Origin', '*')
res.set('Access-Control-Allow-Credentials', 'true')
if (req.method === 'OPTIONS') {
// Send response to OPTIONS requests
res.set('Access-Control-Allow-Methods', 'POST, OPTIONS')
res.set('Access-Control-Allow-Headers', 'authorization,content-type')
res.set('Access-Control-Max-Age', '3600')
res.status(204).send('')
} else if (req.method === 'POST' && /^application\/json/.test(req.get('content-type'))) {
try {
console.log(JSON.stringify(req.body))
let response = await axios.post('https://api.line.me/v2/bot/message/push', req.body, {
headers: {
Authorization: req.get('authorization')
},
validateStatus: status => true
})
console.log(JSON.stringify({ status: response.status, data: response.data }))
res.status(response.status).json(response.data)
} catch (err) {
res.status(500).json({stack: err.stack})
console.error(err)
}
} else {
res.status(403).send('')
}
}
[
{
"type": "flex",
"altText": "訊息推送工具 DEMO",
"contents": {
"type": "bubble",
"hero": {
"type": "image",
"url": "https://storage.googleapis.com/youbike-chatbot-photo/theme-golden-age/hero/atvt20190709-${_.random(1,11)}.png",
"size": "full",
"aspectRatio": "1.51:1",
"aspectMode": "cover",
"action": {
"type": "uri",
"uri": "https://hackmd.io/@taichunmin/B1rUayhjr"
}
},
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"text": "訊息推送工具 DEMO",
"weight": "bold",
"size": "xl"
},
{
"type": "box",
"layout": "vertical",
"margin": "lg",
"spacing": "sm",
"contents": [
{
"type": "box",
"layout": "baseline",
"spacing": "sm",
"contents": [
{
"type": "text",
"text": "名稱",
"color": "#aaaaaa",
"size": "sm",
"flex": 1
},
{
"type": "text",
"text": "${nick_name}",
"wrap": true,
"color": "#666666",
"size": "sm",
"flex": 5
}
]
},
{
"type": "box",
"layout": "baseline",
"spacing": "sm",
"contents": [
{
"type": "text",
"text": "擲骰",
"color": "#aaaaaa",
"size": "sm",
"flex": 1
},
{
"type": "text",
"text": "你擲出了 ${_.random(1,6)} 點",
"wrap": true,
"color": "#666666",
"size": "sm",
"flex": 5
}
]
}
]
}
]
},
"footer": {
"type": "box",
"layout": "vertical",
"spacing": "sm",
"contents": [
{
"type": "button",
"style": "link",
"height": "sm",
"action": {
"type": "uri",
"label": "投影片",
"uri": "https://hackmd.io/@taichunmin/B1rUayhjr"
}
}
]
}
}
}
]
{
"name": "gcf-fb-posts",
"version": "1.0.0",
"main": "index.js",
"repository": "git@gitlab.program.com.tw:AI-Country/gcf-fb-posts.git",
"author": "kelvin <kelvin@program.com.tw>",
"license": "MIT",
"dependencies": {
"@google-cloud/storage": "^3.2.1",
"axios": "^0.19.0",
"dotenv": "^8.1.0",
"lodash": "^4.17.15",
"moment": "^2.24.0"
},
"devDependencies": {
"eslint": "^6.4.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1"
},
"scripts": {
"repl": "node --experimental-repl-await repl.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment