# Empower Your Gmail Inbox with Google Cloud Functions [🔗](https://codelabs.developers.google.com/codelabs/intelligent-gmail-processing)
[TOC]
###### tags: `gmail` `gcf`
---
## 1. Introduction
本實驗構建一 Gmail 擴展程序,自動對收到的電子郵件進行分類,並將這些類別保存在 Google Sheets 中。此擴展程序將使用 G Suite 的 RESTful API、Google Cloud Functions 和其它 Google Cloud Platform 服務。
### 1.1 將打造啥咪
本實驗將構建和部署一些連接到 G Suite API 和其它 Google Cloud Platform 服務的 Cloud Functions,這些功能將:
- Authorize secure access to your Gmail and Google Sheets data.
- Extract images attached to any incoming mail.
- Categorize those images using the Cloud Vision API.
- Write those categories, the sender's address, and the name of the attachment to a Google Sheet.
### 1.2 能學到啥咪
- G Suite RESTful APIs 的基礎知識。
- Basics of Google Cloud Functions and other Google Cloud Platform services.
- How to access Gmail programmatically using Google Cloud Functions.
### 1.3 會需要啥咪
- 可存取 Gmail 和 Google 試算表 Google 帳戶。
- 對 Javascript/Node.js 有基本的瞭解。
## 2. 萬丈高樓平地起
### 2.1 Enable the APIs
In this lab you will use the following Google products/services:
- Google Cloud Functions
- Google Cloud Pub/Sub
- Google Cloud Vision API
- Google Cloud Datastore
- Gmail API
- Google Sheets API
#### 2.1.1 Google Cloud Functions
Google Cloud Functions is Google's Serverless Functions-as-a-Service platform that allows you to run individual snippets of code (‘functions') in a simple, scalable manner.
To enable Google Cloud Functions, click the hamburger menu on the top left of your screen to open the left navigation sidebar:

Find **Cloud Functions** in the navigation menu and click it. Click **Enable API** to enable Google Cloud Functions in your project.
#### 2.1.2 Google Cloud Pub/Sub
Google Cloud Pub/Sub is a simple and scalable foundation for data streaming and event delivery. In this lab it serves as the courier between Gmail and Google Cloud Functions.
To enable Google Cloud Pub/Sub, open the left navigation sidebar, find **Pub/Sub**, and click it. Click **Enable API** to enable Google Cloud Pub/Sub in your project.
#### 2.1.3 Google Cloud Datastore
Google Cloud Datastore is a Serverless database that's scalable and distributed.
To enable Google Cloud Datastore, in the left navigation sidebar, find Datastore and click it. Click Select Datastore Mode in the new page.

You can use any database location for this lab. Click **Create Database** to enable Google Cloud Datastore; it may take a few minutes to complete.
#### 2.1.4 Google Cloud Vision
Google Cloud Vision API is a powerful machine learning service using pre-trained models to derive insights from your images.
See the instructions below for information on how to enable the Google Cloud Vision API.
#### 2.1.5 Enabling the Gmail API, Google Sheets API, and Google Cloud Vision API
Once again, open the left navigation sidebar, and find **APIs & Services**. Click **Library**. In the **Search for APIs & Services** field, type in **Gmail**. In the search results, select **Gmail API** and click **Enable**.
Go back to the API Library page. Search for **Google Sheets API** and enable it.
Repeat the process. Search for **Cloud Vision API** and enable it.
### 2.2 Open Google Cloud Shell
In this lab, you will use Google Cloud Shell to perform most operations. Cloud Shell provides you command-line access to your Google Cloud Platform resources directly from your browser, allowing you to manage them without using a local machine.
To open Google Cloud Shell, click the Activate Cloud Shell button on the top blue horizontal bar:

A new panel will appear at the bottom of the screen:

Click the Launch code editor button to start the Cloud Shell Code Editor:

Cloud Shell Code Editor will open in a new window.
### 2.3 Download the code
Run the command below in your Cloud Shell to clone the project:
```shell
$ git clone https://github.com/googlecodelabs/gcf-gmail-codelab.git
…
$ cd gcf-gmail-codelab/
```
You should see a new folder, `gcf-gmail-codelab/`, appearing in the Cloud Shell Code Editor.
## 3. Architectural overview
Below is the workflow of this lab:

1. User sets up Gmail push notifications: every time a new message arrives at inbox, Gmail will send a notification to Cloud Pub/Sub.
2. Cloud Pub/Sub delivers the new message notification to Google Cloud Functions.
3. Upon arrival of the new message notification, a Cloud Functions instance connects to Gmail and retrieves the new message.
4. If the mail has an image as an attachment, the Cloud Functions instance calls Cloud Vision API to analyze the attachment.
6. The Cloud Functions instance updates a Google Sheet of your choice, specifying who sends the message and where to download the attachment.