Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

python - How to get the Gmail API to notify a bot whenever I get an email from a certain sender

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No attributes are populated by default

See this example notification structure here: https://developers.google.com/gmail/api/guides/push. As outlined in the spec, it's permissible for attributes to be empty so long as data is populated, which it is for Gmail API notifications.

So the answer to your question - if you cannot find the "Sender" attribute - this is because the message does not contain any.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...