【问题标题】:Is there any way to limit a Gmail watch to only messages added?有没有办法将 Gmail 手表限制为仅添加的邮件?
【发布时间】:2017-04-06 20:43:55
【问题描述】:

在为用户设置手表时,有没有办法将手表限制为仅显示添加到收件箱的消息?

根据文档 (https://developers.google.com/gmail/api/v1/reference/users/watch),我看到有 INBOX labelId 选项,但我想将其限制为仅添加的消息。我们目前必须通过在随后的 history.list 调用中的字段字符串中传递“history/messagesAdded”来处理这个问题。

【问题讨论】:

    标签: gmail-api


    【解决方案1】:

    很遗憾您不能。你要做的是

    在通知到达时获取历史记录。历史记录返回一个json,如果添加了新消息,它包含一个'messagesAdded'

    您可以保留predefined array of labels,如下所示

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

    现在你可以检查了,(each is the history json)

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

    您可以在此处获得intersection of labels。现在您必须使用predefined labels 进行检查

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

    终于可以filter the notification随心所欲了!

    最好存储histroyId 并在每次更新时更新它 notification 并在获得 history 时使用它。它会帮助你 仅获取更新的历史记录。

    请注意,我在构建服务器时使用了python。所以上面使用python写的demo代码

    【讨论】:

      【解决方案2】:

      history.list 似乎添加了一个新参数“historyTypes”。如果将其设置为“messageAdded”,则 api 将仅返回该类型的历史记录。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-02
        • 2016-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多