KSzeto
KSzeto

Reputation: 155

Is there any way to limit a Gmail watch to only messages added?

When setting up a watch for a user is there a way to limit the watch to only messages added to the inbox?

Based on the documentation (https://developers.google.com/gmail/api/v1/reference/users/watch) I see that there is the option for INBOX labelId, but I want to limit it to only messages added as well. We're currently having to handle this by passing 'history/messagesAdded' in the fields string in the subsequent history.list call.

Upvotes: 2

Views: 763

Answers (2)

user439199
user439199

Reputation: 397

It looks like history.list added a new parameter "historyTypes". If you set that to "messageAdded", the api will only return history records of that type.

Upvotes: 0

Januka samaranyake
Januka samaranyake

Reputation: 2597

Unfortunately you cannot. what you have to do is

Get the history when notification arrived. History returns a json and it contains a 'messagesAdded' if new message is added.

You can keep a predefined array of labels like below

predefinedLabels = ['UNREAD', 'CATEGORY_PERSONAL', 'INBOX']

Now you can check, (each is the history json)

 if 'messagesAdded' in each:
    labels = each["messagesAdded"][0]["message"]["labelIds"]
    intersectionOfTwoArrays = list(set(predefinedLabels) & set(labels))

Here you get the intersection of labels. Now you have to check that with predefined labels

if set(predefinedLabels) == set(intersectionOfTwoArrays):
     #get the messageId and do what you want

finally you can filter the notification as you want!.

It is better to store histroyId and update it with every notification and use it when you get the history. It will help you to get updated history only.

Please note I used python when I was building my sever. So above demo code written using python

Upvotes: 2

Related Questions