According to the Push Notifications guide within the Gmail API . The Gmail API can notify your back-end server application whenever your Gmail mailbox changes.
To do this, you need to setup a Cloud Pub/Sub client and create a topic. After that, you can then configure your Gmail account to send notifications for mailbox updates. The guide says that
To configure Gmail accounts to send notifications to your Cloud Pub/Sub topic, simply use your Gmail API client to call watch() on the Gmail user mailbox similar to any other Gmail API call. To do so, provide the topic name created above and any other options in your watch() request, such as labels to filter on." This quote was taken from the "Getting Gmail mailbox updates
section from the guide linked above. The guide also provides an example of making a watch() request.
My problem is that I need to filter by sender, not label. I am creating a bot that is going to be deployed to a hosting provider, probably GCP.
I am basically looking for a way to get the Gmail API to notify the bot whenever I get an email from a certain sender. I looked at the watch method in the docs and it seems like the only possible filter is label.
So is there an alternative way to get a notification via the Gmail API whenever I get an email from a certain sender?
Update:
Oh ok, so I was reading about publishing messages to topics via the Pub/Sub API, and according to the description of watch in the Gmail API docs, it seems like watch is responsible for getting Gmail to send (or publish) notifications to the topic. The Subscriber overview section of the Pub/Sub API docs say that
To receive messages published to a topic, you must create a subscription to that topic. The subscription connects the topic to a subscriber application that receives and processes messages published to the topic."
So the subscriber application would be the bot. According to the Filtering messages section of the Pub/Sub API docs, you can create a subscription with a filter. It says
"When you receive messages from a subscription with a filter, you only receive the messages that match the filter. The Pub/Sub service automatically acknowledges the messages that don't match the filter. You can filter messages by their attributes."
So, do the messages (or notifications) that Gmail is sending (or publishing) to the topic include a "Sender" attribute? I can't seem to find the attributes that Gmail includes in a notification message...
question from:
https://stackoverflow.com/questions/65557719/how-to-get-the-gmail-api-to-notify-a-bot-whenever-i-get-an-email-from-a-certain