2. Hello!
I am Cage Chung
I am here because I like to share my experiences.
You can find me at https://kaichu.io
QNAP 雲端應用部資深工程師
GCPUG Taipei organizer
Google Cloud Expert (GCP)
2
3.
4. GCPUG Taipei & Taiwan Java User Group Meetup #42
2018/11/19 at CIT
13. How it works
13 Cloud Functions - Event-driven Serverless Computing | Cloud Functions | Google Cloud - https://cloud.google.com/functions/
14. Functions will be assigned as endpoint
1. POST
2. PUT
3. GET
4. DELETE
5. OPTIONS
Triggers - Http
14
--trigger-http
15. Triggers - Bucket
15
Evey change in files in this bucket will trigger functions
execution.
--trigger-bucket=TRIGGER_BUCKET
16. Triggers - Topic
16
Name of Pub/Sub topic. Every message published in this
topic will trigger function execution with message
contents passed as input data.
--trigger-topic=TRIGGER_TOPIC
17. Triggers - Event type
17
EVENT_PROVIDER EVENT_TYPE EVENT_TYPE_DEFAULT RESOURCE_TYPE RESOURCE_OPTIONAL
cloud.pubsub google.pubsub.topic.publish Yes topic No
cloud.pubsub providers/cloud.pubsub/eventTypes/topic.publish No topic No
cloud.storage google.storage.object.archive No bucket No
cloud.storage google.storage.object.delete No bucket No
cloud.storage google.storage.object.finalize Yes bucket No
cloud.storage google.storage.object.metadataUpdate No bucket No
cloud.storage providers/cloud.storage/eventTypes/object.change No bucket No
google.firebase.analytics.event providers/google.firebase.analytics/eventTypes/event.log Yes firebase analytics No
google.firebase.database.ref providers/google.firebase.database/eventTypes/ref.create Yes firebase database No
google.firebase.database.ref providers/google.firebase.database/eventTypes/ref.delete No firebase database No
google.firebase.database.ref providers/google.firebase.database/eventTypes/ref.update No firebase database No
google.firebase.database.ref providers/google.firebase.database/eventTypes/ref.write No firebase database No
google.firestore.document providers/cloud.firestore/eventTypes/document.create Yes firestore document No
google.firestore.document providers/cloud.firestore/eventTypes/document.delete No firestore document No
google.firestore.document providers/cloud.firestore/eventTypes/document.update No firestore document No
google.firestore.document providers/cloud.firestore/eventTypes/document.write No firestore document No
25. Three steps
Authorization
Gmail / Drive /
Spreadsheet
Initialize
watch to get
notification
on new
emails
Processing
and acting on
drive
spreadsheet
25
26. 26
Authorize access
to G Suite data
Cloud
Functions
Cloud
Storage
Save token
Get gmail permission
token
27. 27
exports.oauth2init = (req, res) => {
// Define OAuth2 scopes
const scopes = [
'https://www.googleapis.com/auth/gmail.readonly,
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
];
// Generate + redirect to OAuth2 consent form URL
const authUrl = oauth.client.generateAuthUrl({
access_type: 'offline',
scope: scopes,
prompt: 'consent' // Required in order to receive a refresh token every time
});
return res.redirect(authUrl);
};
28. 28
exports.oauth2callback = (req, res) => {
// Get authorization code from request
const code = req.query.code;
// OAuth2: Exchange authorization code for access token
return new Promise((resolve, reject) => {
oauth.client.getToken(code, (err, token) =>
(err ? reject(err) : resolve(token))
);
}).then((token) => {
// Get user email (to use as a Datastore key)
oauth.client.credentials = token;
return Promise.all([token, oauth.getEmailAddress()]);
})
.then(([token, emailAddress]) => {
// Store token in Datastore
return Promise.all([
emailAddress,
oauth.saveToken(emailAddress)
]);
})
.then(([emailAddress]) => {
// Respond to request
res.redirect(`/initWatch?emailAddress=${querystring.escape(emailAddress)}`);
})
.catch((err) => {
// Handle error
console.error(err);
res.status(500).send('Something went wrong; check the logs.');
});
};
29. 29
Initialize a ‘watch’
for Gmail changes
Cloud
Functions
Initialize watch
Cloud
Pub/Sub
Watch notification
On new message
45. References
1. Using serverless on GCP to add custom intelligence to Gmail |
Google Cloud Blog
2. Push Notifications | Gmail API | Google Developers
3. API Reference | Gmail API | Google Developers
4. API Reference | Drive REST API | Google Developers
5. Google Sheets API | Sheets API | Google Developers
6. Cloud Functions Node.js Emulator | Cloud Functions
Documentation | Google Cloud
45