Skip to content

Instantly share code, notes, and snippets.

@timfanda35
Created March 17, 2019 19:26
Show Gist options
  • Save timfanda35/9981fa24e535c8bea555b1d57827bbc2 to your computer and use it in GitHub Desktop.
Save timfanda35/9981fa24e535c8bea555b1d57827bbc2 to your computer and use it in GitHub Desktop.
GCP Cloud Functions example - Upload Notify
const lineNotifyApiHost = 'notify-api.line.me';
const lineNotifyApiPath = '/api/notify';
const lineNotifyApiToken = process.env.ACCESS_TOKEN;
const https = require('https');
const querystring = require('querystring');
//
// Push message
//
function pushMessage(message) {
// Build the post string from an object
var post_data = querystring.stringify({
'message' : message
});
// An object of options to indicate where to post to
var post_options = {
host: lineNotifyApiHost,
port: 443,
path: lineNotifyApiPath,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer '+ lineNotifyApiToken,
'Content-Length': Buffer.byteLength(post_data)
}
};
// Set up the request
var post_req = https.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}
//
// Endpoint
//
exports.notify = function notify (event, callback) {
console.log(JSON.stringify(event));
pushMessage("File is uploaded: " + event.data.name);
callback(null, 'Success!');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment