Skip to content

Commit 210229d

Browse files
Merge pull request #87 from kasthuriraajan/master
Add listener module to gmail connector
2 parents 1b7c667 + 238d35a commit 210229d

20 files changed

+1620
-56
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#Config file
88
Config.toml
99
# Modules
10-
/modules
10+
/modules/googleapis_gmail
1111

1212
# BlueJ files
1313
*.ctxt

Ballerina.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
org = "ballerinax"
33
name = "googleapis_gmail"
4-
version = "0.99.4"
4+
version = "0.99.5"
55
authors = ["Ballerina"]
66
repository = "https://github.com/ballerina-platform/module-ballerinax-googleapis.gmail"
77
keywords = ["gmail","email"]

Package.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,206 @@ public function main(string... args) {
8686
}
8787
}
8888
```
89+
# Listener Module
90+
91+
Connects to Gmail Listener using Ballerina.
92+
93+
# Module Overview
94+
95+
The Gmail Listener Ballerina Connector provides the capability to listen the push notification for changes to Gmail mailboxes. The Gmail Listener Ballerina Connector supports to listen the changes of Gmail mailboxes such as receiving new message, receiving new thread, adding new label to a message, adding star to a message, removing label to a message, removing star to a message and receiving a new attachment with following trigger methods: `onMailboxChanges`, `onNewEmail`, `onNewThread`, `onNewLabeledEmail`, `onNewStaredEmail`, `onLabelRemovedEmail`,`onStarRemovedEmail`, `onNewAttachment`.
96+
97+
98+
# Prerequisites:
99+
100+
* Java 11 Installed
101+
Java Development Kit (JDK) with version 11 is required.
102+
103+
* Download the Ballerina [distribution](https://ballerinalang.org/downloads/)
104+
Ballerina Swan Lake Alpha 2 is required.
105+
106+
* Instantiate the connector by giving authentication details in the HTTP client config. The HTTP client config has built-in support for BasicAuth and OAuth 2.0. Gmail uses OAuth 2.0 to authenticate and authorize requests. The Gmail connector can be minimally instantiated in the HTTP client config using the client ID, client secret, and refresh token.
107+
* Client ID
108+
* Client Secret
109+
* Refresh Token
110+
* Refresh URL
111+
112+
## Obtaining Tokens
113+
114+
1. Visit [Google API Console](https://console.developers.google.com), click **Create Project**, and follow the wizard to create a new project.
115+
2. Go to **Library** from the left side menu. In the search bar enter required API/Service name(Eg: Gmail). Then select required service and click **Enable** button.
116+
3. Go to **Credentials -> OAuth consent screen**, enter a product name to be shown to users, and click **Save**.
117+
4. On the **Credentials** tab, click **Create credentials** and select **OAuth client ID**.
118+
5. Select an application type, enter a name for the application, and specify a redirect URI (enter https://developers.google.com/oauthplayground if you want to use
119+
[OAuth 2.0 playground](https://developers.google.com/oauthplayground) to receive the authorization code and obtain the refresh token).
120+
6. Click **Create**. Your client ID and client secret appear.
121+
7. In a separate browser window or tab, visit [OAuth 2.0 playground](https://developers.google.com/oauthplayground), select the required Gmail scopes, and then click **Authorize APIs**.
122+
123+
8. When you receive your authorization code, click **Exchange authorization code for tokens** to obtain the refresh token.
124+
125+
## Create push topic and subscription
126+
To use Gmail Listener connector, a topic and a subscription should be configured.
127+
128+
1. Enable Cloud Pub/Sub API for your project which is created in [Google API Console](https://console.developers.google.com).
129+
2. Go to [Google Cloud Pub/Sub API management console](https://console.cloud.google.com/cloudpubsub/topic/list) and create a topic([You can follow the instructions here](https://cloud.google.com/pubsub/docs/quickstart-console) and a subscription to that topic. The subscription should be a pull subscription in this case ([Find mode details here](https://cloud.google.com/pubsub/docs/subscriber))).
130+
3. For the push subscription , an endpoint URL should be given to push the notification. This URL is the URL where the gmail listener service runs. This should be in `https` format. (If the service runs in localhost, then ngrok can be used to get an `https` URL).
131+
4. Grant publish right on your topic. [To do this, see the instructions here](https://developers.google.com/gmail/api/guides/push#grant_publish_rights_on_your_topic).
132+
133+
5. Once you have done the above steps, get your topic name (It will be in the format of `projects/<YOUR_PROJECT_NAME>topics/<YOUR_TOPIC_NAME>`) from your console and give it to the `Config.toml` file as `topicName`.
134+
135+
136+
## Add project configurations file
137+
Add the project configuration file by creating a `Config.toml` file under the root path of the project structure.
138+
This file should have following configurations. Add the token obtained in the previous step to the `Config.toml` file.
139+
140+
```
141+
[ballerinax.googleapis_gmail]
142+
refreshToken = "enter your refresh token here"
143+
clientId = "enter your client id here"
144+
clientSecret = "enter your client secret here"
145+
port = "enter the port where your listener runs"
146+
topicName = "enter your push topic name"
147+
148+
```
149+
150+
# Compatibility
151+
152+
| Ballerina Language Versions | Gmail API Version |
153+
|:----------------------------:|:-----------------:|
154+
| Swan Lake Alpha 2 | v1 |
155+
156+
# Quickstart(s):
157+
158+
## Working with Gmail Listener
159+
160+
### Step 1: Import Gmail and Gmail Listener Ballerina Library
161+
First, import the ballerinax/googleapis_gmail and ballerinax/googleapis_gmail.'listener module into the Ballerina project.
162+
```ballerina
163+
import ballerinax/googleapis_gmail as gmail;
164+
import ballerinax/googleapis_gmail.'listener as gmailListener;
165+
```
166+
167+
### Step 2: Initialize the Gmail Client and Gmail Listener
168+
In order for you to use the Gmail Listener Endpoint, first you need to create a Gmail Client endpoint and a Gmail Listener endpoint.
169+
```ballerina
170+
configurable string refreshToken = ?;
171+
configurable string clientId = ?;
172+
configurable string clientSecret = ?;
173+
configurable int port = ?;
174+
configurable string topicName = ?;
175+
176+
gmail:GmailConfiguration gmailConfig = {
177+
oauthClientConfig: {
178+
refreshUrl: gmail:REFRESH_URL,
179+
refreshToken: refreshToken,
180+
clientId: clientId,
181+
clientSecret: clientSecret
182+
}
183+
};
184+
185+
gmail:Client gmailClient = new (gmailConfig);
186+
187+
listener gmailListener:Listener gmailEventListener = new(port, gmailClient, topicName);
188+
189+
```
190+
Then the endpoint triggers can be invoked as `var response = gmailEventListener->triggerName(arguments)`.
191+
192+
193+
# Samples
194+
195+
### On New Email
196+
197+
Triggers when a new e-mail appears in the mail inbox.
198+
199+
```ballerina
200+
import ballerina/http;
201+
import ballerina/log;
202+
import ballerinax/googleapis_gmail as gmail;
203+
import ballerinax/googleapis_gmail.'listener as gmailListener;
204+
205+
configurable string refreshToken = ?;
206+
configurable string clientId = ?;
207+
configurable string clientSecret = ?;
208+
configurable int port = ?;
209+
configurable string topicName = ?;
210+
211+
gmail:GmailConfiguration gmailConfig = {
212+
oauthClientConfig: {
213+
refreshUrl: gmail:REFRESH_URL,
214+
refreshToken: refreshToken,
215+
clientId: clientId,
216+
clientSecret: clientSecret
217+
}
218+
};
219+
220+
gmail:Client gmailClient = new (gmailConfig);
221+
222+
listener gmailListener:Listener gmailEventListener = new(port, gmailClient, topicName);
223+
224+
service / on gmailEventListener {
225+
resource function post web(http:Caller caller, http:Request req) {
226+
var payload = req.getJsonPayload();
227+
var response = gmailEventListener.onMailboxChanges(caller , req);
228+
if(response is gmail:MailboxHistoryPage) {
229+
var triggerResponse = gmailEventListener.onNewEmail(response);
230+
if(triggerResponse is gmail:Message[]) {
231+
if (triggerResponse.length()>0){
232+
//Write your logic here.....
233+
foreach var msg in triggerResponse {
234+
log:print("Message ID: "+msg.id + " Thread ID: "+ msg.threadId+ " Snippet: "+msg.snippet);
235+
}
236+
}
237+
}
238+
}
239+
}
240+
}
241+
```
242+
243+
### On New Labeled Email
244+
245+
Triggers when you label an email.
246+
247+
```ballerina
248+
import ballerina/http;
249+
import ballerina/log;
250+
import ballerinax/googleapis_gmail as gmail;
251+
import ballerinax/googleapis_gmail.'listener as gmailListener;
252+
253+
configurable string refreshToken = ?;
254+
configurable string clientId = ?;
255+
configurable string clientSecret = ?;
256+
configurable int port = ?;
257+
configurable string topicName = ?;
258+
259+
gmail:GmailConfiguration gmailConfig = {
260+
oauthClientConfig: {
261+
refreshUrl: gmail:REFRESH_URL,
262+
refreshToken: refreshToken,
263+
clientId: clientId,
264+
clientSecret: clientSecret
265+
}
266+
};
267+
268+
gmail:Client gmailClient = new (gmailConfig);
269+
270+
listener gmailListener:Listener gmailEventListener = new(port, gmailClient, topicName);
271+
272+
service / on gmailEventListener {
273+
resource function post web(http:Caller caller, http:Request req) {
274+
var payload = req.getJsonPayload();
275+
var response = gmailEventListener.onMailboxChanges(caller , req);
276+
if(response is gmail:MailboxHistoryPage) {
277+
var triggerResponse = gmailEventListener.onNewLabeledEmail(response);
278+
if(triggerResponse is gmailListener:ChangedLabel[]) {
279+
if (triggerResponse.length()>0){
280+
//Write your logic here.....
281+
foreach var changedLabel in triggerResponse {
282+
log:print("Message ID: "+ changedLabel.message.id + " Changed Label ID: "
283+
+changedLabel.changedLabelId[0]);
284+
}
285+
}
286+
}
287+
}
288+
}
289+
}
290+
```
291+
More samples are available at "https://github.com/ballerina-platform/module-ballerinax-googleapis.gmail/tree/master/samples/listener".

0 commit comments

Comments
 (0)