Power Automate:筛选没有分类的邮件

109 阅读2分钟

本文将展示如何通过 Power Automate 筛选Outlook 中未分类的邮件.

在当前的 Outlook 365 连接器中,没有可以筛选掉有/无分类的邮件的选项。

当我尝试构建流程时,我以为可以使用Get emails (V3)。尝试之后,我发现到正文中没有类别信息,并且这个还特别消耗API. 下面是来自它的输出信息的body值:一个邮件结构的示例Get emails (V3)

{
    "id": "value",
    "receivedDateTime": "",
    "hasAttachments": false,
    "internetMessageId": "",
    "subject": "",
    "bodyPreview": "Hi...",
    "importance": "normal",
    "conversationId": "",
    "isRead": false,
    "isHtml": true,
    "body": "html body",
    "from": "",
    "toRecipients": "",
    "attachments": []
},

在我检查每个操作后,发现只能使用Send an HTTP request 

使用Send an HTTP request

此操作构造要调用的 Microsoft Graph REST API 请求。支持以下段:

  1. 1st segment: /me, /users/
  2. 2nd segment: messages, mailFolders, events, calendar, calendars, outlook, inferenceClassification

/me/messages 可行,但我只想查看收件箱的未读邮件.

/me/mailFolders/Inbox/messages 这个刚刚好

为了演示目的,伪代码如下:

Send an request to query inbox message and apply to unread message.
Filter if any uncategorized message.
For each message found, assign category.

一些无效的方法:

  1. 使用 Odata 过滤器
filter=categories eq null //not work
filter=categories in //not work
filter=categories/any(c:c eq '')//not work
filter=categories/any(c:c eq null)//not work
filter=categories/any(c:c eq 'null')//not work
filter=categories/any(c:c eq '')//not work

2. ##### 使用条件过滤

equals(item()?['categories'], null) //not work.
equals(length(item()?['categories']), 0)) //nope

备选的方法

filter=not categories/any() 
//仅当纯筛选未分类的邮件

可行的方法。

empty(item()?['categories'])
//Returns true if the object, array, or string is empty.

因为categories是一个数组,恰好empty函数可以接受数组为参数.

流程概览。

flowOverview.png

流程分解:

  1. 初始化 OData 查询变量
{
    "type": "InitializeVariable",
    "inputs": {
      "variables": [
        {
          "name": "MyQuery",
          "type": "string",
          "value": "filter=isRead eq false&$select=categories&$top=10"
        }
      ]
    },
    "runAfter": {}
  }

2. ##### 发送 HTTP 请求

{
    "type": "OpenApiConnection",
    "inputs": {
      "parameters": {
        "Uri": "https://graph.microsoft.com/v1.0/me/mailFolders/Inbox/messages?$@{variables('MyQuery')}",
        "Method": "GET",
        "ContentType": "application/json"
      },
      "host": {
        "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
        "connection": "shared_office365",
        "operationId": "HttpRequest"
      }
    },
    "runAfter": {
      "Initialize_variable_for_OData_query": [
        "Succeeded"
      ]
    }
  }

3. ##### 仅筛选未分类的消息

{
    "type": "Query",
    "inputs": {
      "from": "@body('Send_an_HTTP_request')?['value']",
      "where": "@empty(item()?['categories'])"
    },
    "runAfter": {
      "Send_an_HTTP_request": [
        "Succeeded"
      ]
    }
  }

4. ##### 对于每条未分类的消息

{
    "type": "Foreach",
    "foreach": "@outputs('Filter_array_only_uncategorized_message')['body']",
    "actions": {
      "Assigns_an_Outlook_category": {
        "type": "OpenApiConnection",
        "inputs": {
          "parameters": {
            "messageId": "@items('For_each_uncategorized_message')?['id']",
            "category": "FollowUP" //Your category name.
          },
          "host": {
            "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
            "connection": "shared_office365",
            "operationId": "AssignCategory"
          }
        }
      }
    },
    "runAfter": {
      "Filter_array_only_uncategorized_message": [
        "Succeeded"
      ]
    }
  }

5. ##### 分配 Outlook 类别

{
    "type": "OpenApiConnection",
    "inputs": {
      "parameters": {
        "messageId": "@items('For_each_uncategorized_message')?['id']",
        "category": "FollowUP"
      },
      "host": {
        "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365",
        "connection": "shared_office365",
        "operationId": "AssignCategory"
      }
    }
  }

与我之前创建的另一个相比,旧流程消耗了更多的 API。

FilterNonCategoryEmail.gif