TL;DR
- GCP のCloudFunctionsでLINE NotifyのAPIをキックする
- slackのOutgoing Webhooksに登録するとslackの通知をLINEに転送できるようになる
準備
gcloudコマンド
gcloud components install alpha
デプロイ先の gs のbucket
gsutil mb -p PROJECT_ID gs://BUCKET_NAME
実装
npm package
npm init
npm install config --save
npm instlal request --save
script
index.js
/**
* HTTP Cloud Function.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
var request = require('request');
var config = require('config');
var lineConfigAccessToken = config.Line.access_token;
exports.sendLineNotify = function sendLineNotify (req, res) {
var options = {
url: 'https://notify-api.line.me/api/notify',
method: 'POST',
headers: {
'Content-Type':'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + lineConfigAccessToken,
},
form: { message : `${req.body.text}` }
};
// request LINE Notify
request(options, recallback);
function recallback(error, response, body) {
if (error == true) {
console.log(body);
}
}
res.send('ok');
};
config
- LINE Notifyのaccess_tokenを取得して config/production.jsonに記述する
config/production.json
{
"Line": {
"access_token": "ACCESS_TOKEN"
}
}
deploy
-> % gcloud alpha functions deploy FUNCTION_NAME --stage-bucket BUCKET_NAME --trigger-http
Copying file:///var/folders/rt/gg6z17_51z3_l4jf7rtmgvc00000gn/T/tmpGwi1cH/fun.zip [Content-Type=application/zip]...
- [1 files][ 1.1 MiB/ 1.1 MiB]
Operation completed over 1 objects/1.1 MiB.
Waiting for operation to finish...done.
availableMemoryMb: 256
entryPoint: FUNCTION_NAME
gcsUrl: gs://BUCKET_NAME/us-central1-FUCNTIONNAME-xxxxxxxxxxx.zip
httpsTrigger:
url: https://us-central1-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME
latestOperation: operations/d3d3LXRrbnprLWNvbS91cy1jZW50cmFsMS9zZW5kTGluZU5vdGlmeS9PNEV0ZHFlZ3dRaw
name: projects/PROJECT_ID/locations/us-central1/functions/FUNCTION_NAME
status: READY
timeout: 60s
test call
- deployのときに表示された triggerの urlに対してcurlで POST Request
curl -X POST https://us-central1-PROJECT_ID.cloudfunctions.net/FUNCTION_NAME -H "Content-Type:application/json" --data '{"text":"from gcf text message"}'