PowerAutomate: Teams で非表示にした Chat/GroupChat/Meeting を一覧表示して「再表示」の手助けをする

# 背景

会社にて、非表示化したチャットをどうやって見つければいいのか？というお話があったので、作ってみた

## 非表示化ってなに？

チャットがうざい場合に、ミュートとは別に、非表示にするって技があります。

で、一度これやっちゃうと履歴から消えちゃうので、表示しようとすると意外に困る。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/e1e8edd2-c979-9a04-6632-1bfa30652762.png)

# 概要

- Graph API を使って、非表示となっているチャット一覧を取得
- で、表示させれば、「再表示」させられる

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/1d7c31a4-42c7-26e1-7798-8c3d5f248e67.png)

実際にはこんな感じで表示される。
あとは、戻したいやつに飛んで、「再表示」で解決

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/bb2948df-4403-c3d8-25fe-5e34f0427e53.png)


```uri: Graph API
https://graph.microsoft.com/beta/me/chats?$filter=viewpoint/isHidden&$expand=members&$select=id,topic,chatType,webUrl,viewpoint,members
```

https://learn.microsoft.com/ja-jp/graph/api/chat-list?view=graph-rest-1.0&tabs=http

# 詳細

## 変数定義

- apply to array を使いたかったんで、変数を事前定義

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/9bae2cf4-4408-f632-fe07-ad736e648e02.png)

<summary>コピー用サンプルコード　without trigger<details>

```json: initial chatsInformation
{"id":"6389ec6d-4c29-46f4-942e-ebd3e4b5626f","brandColor":"#770BD6","connectionReferences":{"shared_teams_1":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_teams/connections/shared-teams-864099d5-54a7-4002-93b6-565ecf481128"}},"shared_teams":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_teams/connections/shared-teams-23cd8ea9-6749-430b-b65f-d763ce64ee43"}}},"connectorDisplayName":"Variables","icon":"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzJweCIgaGVpZ2h0PSIzMnB4Ii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yMzQgMC41NDcgMC4yMzQgMC41ODggMCAxLjI1LTAuNTk2IDEuOTg0LTEuNzg2bDAuNDA2LTAuNjU4IDAuMTU1LTAuMjU5eiIvPg0KICA8ZWxsaXBzZSB0cmFuc2Zvcm09Im1hdHJpeCguMDUzNiAtLjk5ODYgLjk5ODYgLjA1MzYgNS40OTI1IDMyLjI0NSkiIGN4PSIxOS43NTciIGN5PSIxMy4yMjUiIHJ4PSIuNzc4IiByeT0iLjc3OCIvPg0KICA8ZWxsaXBzZSB0cmFuc2Zvcm09Im1hdHJpeCguMDUzNiAtLjk5ODYgLjk5ODYgLjA1MzYgLTcuNTgzOSAzMC42MjkpIiBjeD0iMTIuMzY2IiBjeT0iMTkuMzE1IiByeD0iLjc3OCIgcnk9Ii43NzgiLz4NCiA8L2c+DQo8L3N2Zz4NCg==","isTrigger":false,"operationName":"Initialize_variable_ChatsInformation","operationDefinition":{"type":"InitializeVariable","inputs":{"variables":[{"name":"ChatsInformation","type":"array"}]},"runAfter":{},"metadata":{"operationMetadataId":"41041bde-57e9-42d0-8209-6331211f391a"}}}
```

</details></summary>

## Graph API で取得して整形するだけ

工夫したところは、topic 名が取れない場合には、グループメンバーの [displayName] で参加者を分かるようにしてるところ。
故に、会社によっては、FirstName だけにしたり、人数が多い場合には省略する必要があるところ

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/3f4ba4a8-90c5-f647-e69b-f4e3d5c03a93.png)

<summary>コピー用サンプルコード<details>

```json: initial chatsInformation

{"id":"d6a6d1d0-0347-4f6f-bf23-1d806dc899c6","brandColor":"#8C3900","connectionReferences":{"shared_teams_1":{"connection":{"id":"/providers/Microsoft.PowerApps/apis/shared_teams/connections/shared-teams-864099d5-54a7-4002-93b6-565ecf481128"}}},"connectorDisplayName":"Control","icon":"data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzIiIGhlaWdodD0iMzIiIHZlcnNpb249IjEuMSIgdmlld0JveD0iMCAwIDMyIDMyIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPg0KIDxwYXRoIGQ9Im0wIDBoMzJ2MzJoLTMyeiIgZmlsbD0iIzhDMzkwMCIvPg0KIDxwYXRoIGQ9Im04IDEwaDE2djEyaC0xNnptMTUgMTF2LTEwaC0xNHYxMHptLTItOHY2aC0xMHYtNnptLTEgNXYtNGgtOHY0eiIgZmlsbD0iI2ZmZiIvPg0KPC9zdmc+DQo=","isTrigger":false,"operationName":"Scope","operationDefinition":{"type":"Scope","actions":{"Apply_to_each_chats":{"type":"Foreach","foreach":"@body('Select')","actions":{"Condition_topic_is_empty":{"type":"If","expression":{"and":[{"equals":["@items('Apply_to_each_chats')['topic']","@null"]}]},"actions":{"Select_members":{"type":"Select","inputs":{"from":"@items('Apply_to_each_chats')['members']","select":"@item()['displayName']"},"runAfter":{},"metadata":{"operationMetadataId":"a62f6786-7752-498b-9c1f-5a52227a2403"}},"Append_to_array_variable_as_members":{"type":"AppendToArrayVariable","inputs":{"name":"ChatsInformation","value":{"type":"ColumnSet","columns":[{"type":"Column","width":"stretch","items":[{"type":"ActionSet","actions":[{"type":"Action.OpenUrl","title":"@join(body('Select_members'), ' ')","url":"@{items('Apply_to_each_chats')['webUrl']}","iconUrl":"https://static2.sharepointonline.com/files/fabric-cdn-prod_20210930.001/assets/brand-icons/product/png/teams_16x1.png"}]}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"@{items('Apply_to_each_chats')['chatType']}","wrap":true}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"@{formatDateTime(addHours(items('Apply_to_each_chats')['lastMessageReadDateTime'], 9), 'yyyy/MM/dd HH:mm')}","wrap":true}]}],"spacing":"None"}},"runAfter":{"Select_members":["Succeeded"]},"metadata":{"operationMetadataId":"5e040bdf-fba8-4984-8279-7d6a7ae50de9"}}},"runAfter":{},"else":{"actions":{"Append_to_array_variable_as_topic":{"type":"AppendToArrayVariable","inputs":{"name":"ChatsInformation","value":{"type":"ColumnSet","columns":[{"type":"Column","width":"stretch","items":[{"type":"ActionSet","actions":[{"type":"Action.OpenUrl","title":"@items('Apply_to_each_chats')['topic']","url":"@{items('Apply_to_each_chats')['webUrl']}","iconUrl":"https://static2.sharepointonline.com/files/fabric-cdn-prod_20210930.001/assets/brand-icons/product/png/teams_16x1.png"}]}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"@{items('Apply_to_each_chats')['chatType']}","wrap":true}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"@{formatDateTime(addHours(items('Apply_to_each_chats')['lastMessageReadDateTime'], 9), 'yyyy/MM/dd HH:mm')}","wrap":true}]}],"spacing":"None"}},"runAfter":{},"metadata":{"operationMetadataId":"6c74f594-3da9-48ac-869c-37138f7e02b9"}}}},"metadata":{"operationMetadataId":"8425def1-6c04-4acd-ba73-50bf0fd00c9b"}}},"runAfter":{"Select":["Succeeded"]},"metadata":{"operationMetadataId":"a233a234-a4b1-4490-9dab-8a6f64983e8d"}},"Select":{"type":"Select","inputs":{"from":"@body('Send_a_Microsoft_Graph_HTTP_request_with_me')['value']","select":{"id":"@item()['id']","chatType":"@item()['chatType']","webUrl":"@item()['webUrl']","isHidden":"@item()['viewpoint/isHidden']","lastMessageReadDateTime":"@item()['viewpoint/lastMessageReadDateTime']","topic":"@item()['topic']","members":"@item()['members']"}},"runAfter":{"Send_a_Microsoft_Graph_HTTP_request_with_me":["Succeeded"]},"metadata":{"operationMetadataId":"94034178-0e32-44da-835a-81aac3611e01"}},"Compose_message_card":{"type":"Compose","inputs":{"type":"AdaptiveCard","$schema":"http://adaptivecards.io/schemas/adaptive-card.json","version":"1.4","msteams":{"width":"full"},"body":[{"type":"TextBlock","text":"非表示チャット一覧 @{length(body('Select'))} 件: Topic から飛んで再表示可能","wrap":true},{"type":"Container","items":[{"type":"ColumnSet","columns":[{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"Topic","wrap":true}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"ChatType","wrap":true}]},{"type":"Column","width":"stretch","items":[{"type":"TextBlock","text":"lastMessageReadDateTime","wrap":true}]}]}],"style":"accent","bleed":true},{"type":"Container","items":"@variables('ChatsInformation')","spacing":"None"}]},"runAfter":{"Apply_to_each_chats":["Succeeded"]},"metadata":{"operationMetadataId":"7ad145ba-5fed-4ebe-8cb0-3c26fbbdcda7"}},"Post_card_in_a_chat_or_channel":{"type":"OpenApiConnection","inputs":{"host":{"connectionName":"shared_teams_1","operationId":"PostCardToConversation","apiId":"/providers/Microsoft.PowerApps/apis/shared_teams"},"parameters":{"poster":"Flow bot","location":"Chat with Flow bot","body/recipient":"@{outputs('Compose_recipient')};","body/messageBody":"@outputs('Compose_message_card')"},"authentication":{"type":"Raw","value":"@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"}},"runAfter":{"Compose_message_card":["Succeeded"]},"metadata":{"operationMetadataId":"4be482b4-83de-4904-93b8-db80d515e4e1"}},"Compose_recipient":{"type":"Compose","inputs":"@triggerOutputs()['headers']['x-ms-user-email-encoded']","runAfter":{},"metadata":{"operationMetadataId":"cc9e7237-1c71-4d03-8074-2f5d8686b5bc"}},"Send_a_Microsoft_Graph_HTTP_request_with_me":{"type":"OpenApiConnection","inputs":{"host":{"connectionName":"shared_teams_1","operationId":"HttpRequest","apiId":"/providers/Microsoft.PowerApps/apis/shared_teams"},"parameters":{"Uri":"https://graph.microsoft.com/beta/me/chats?$filter=viewpoint/isHidden&$expand=members&$select=id,topic,chatType,webUrl,viewpoint,members","Method":"GET","ContentType":"application/json"},"authentication":{"type":"Raw","value":"@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"}},"runAfter":{"Compose_recipient":["Succeeded"]},"metadata":{"operationMetadataId":"056e1ec7-13b3-447a-a480-54008c08ee7b"}}},"runAfter":{"Initialize_variable_ChatsInformation":["Succeeded"]},"metadata":{"operationMetadataId":"38756829-612f-4973-ac88-2e3437ec4744"}}}

```

</details></summary>


# あとがき

久々に Automate 触った気がする。
New Designer にもコピー機能が追加されてきてるけど、Nest/Expand するやつにはまだ使えないのが悲しい・・

今回のコピー用サンプルコードも、結局 Old Designer でコピーしているので、そのうち使えなくなるのかも :cry: