How to filter the Google Api inbox to read only the main messages?

Kyoto

I made a script to check if there are new emails in my inbox, but it returns the emails (Gmail) from the Main, Social and Promotions box, but I just want to read the one from the Main box.

Its an script for my IA

        # Call the Gmail API to fetch INBOX

        results = service.users().messages().list(userId='me',labelIds = ['INBOX', 'UNREAD']).execute()
        messages = results.get('messages', [])


        if not messages:
            frase = 'Sem novas mensagens na caixa de email'
            a = Pesquise(frase)
            a.fala(frase)
        else:
            for message in messages:
                msg = service.users().messages().get(userId='me', id=message['id']).execute()
                payld = msg['payload'] # get payload of the message 
                headr = payld['headers'] # get header of the payload
                for one in headr: # getting the Subject
                    if one['name'] == 'Subject':
                        msg_subject = remover_acentos(one['value'])
                    else:
                        pass
                frase = 'Hugo, novo email na caixa de entrada, ' + msg_subject
                a = Pesquise(frase)
                a.fala(frase)
        sleep(600)
except Exception as err:
    print err

I just want the script to read the Main box, not the Main, Social, and Promotions.

Jacque

Based from SGC's answer, you can add the following q parameter on your Users.messages: list :

in:inbox is:unread -category:(promotions OR social)

The result will be something like:

{
 "messages": [
  {
   "id": "169f891015afd26b",
   "threadId": "169f891015afd26b"
  },
  {
   "id": "169f67bfu70eed28",
   "threadId": "169f67bf5d0eed28"
  },
  {
   "id": "169f65c4bea507b9",
   "threadId": "169f27c4bea507b9"
  },
  {
   "id": "169ef58fd53f6c1d",
   "threadId": "169ef58fd53f6c1d"
  },
  {
   "id": "169eecy7846c17ca",
   "threadId": "169eecy7846c17ca"
  },
  {
   "id": "169ea5df63c40128",
   "threadId": "169ea5df63c40128"
  }
 ],
 "resultSizeEstimate": 6
}

You can try this here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I filter inbox and only download emails that have attachments

How to read my messages from Inbox or sent items from a specific email address and within a particular interval?

Facebook API - Get inbox messages count

How to only read Google maps API key in production

In GmailAPI, how to retrieve messages that are new in the Inbox

how to retrieve inbox messages of last 5 days?

Google Sheets API, Read Values by Filter

Filter Google Places API for only one country

filter only sublocalities in google maps api

How to filter the inbox in JMeter Mail reader sampler?

Fetching email ID's of only the emails in the main Gmail Inbox

Gmail API getting all Gmail Inbox messages limits to 500

Get all inbox messages with Gmail API using PHP

List inbox messages using node.js gmail api

Efficient way of syncing Gmail Inbox messages using the new Gmail API?

how to filter the rows I read from google-sheets with Java API

Outlook 2010 "Mark item as read when selection changes" ONLY in Inbox

Is there a way to make emails skip the inbox without a filter in Google Apps Script?

Linux read log file and filter to get the same type of log messages only once

Outlook 2010 Inbox Being Marked as READ Before I Ever Read Messages when every 5 mins

Google Calendar API: How to create a "read-only" / non-editable event?

How to prevent an user from sending messages to a group, but keep receiving messages from that group (read only)?

How to read channel messages with simple-slack-api?

com_error while trying to filter outlook inbox messages into python list

Node.js: Google Analytics API: How do I make authorized read-only Management API requests with a "service account"?

Getting Gmail Inbox using node.js and google-api

How do i setup a service that collects users credentials authenticate the user with google gmail api and fetch mails from their inbox

How to read e-mails from inbox of a GMail account in Java?

How to read inbox sms from a specific contact in android?