PowerAutomate: 選択したメッセージの投稿相手へチャットする

# 背景
ユーザーアイコンからチャットを開くのが、思ったほどに簡単ではない。
チャットに固定している人でもない限り探すのも面倒。

ってことがあったので、選択したメッセージを投稿した人に簡単にメッセージを送れるように作ってみた

# 概要
1. チャットしたい相手が投稿したメッセージ上で Automate 起動<br>![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/74012685-7249-c934-0bb9-a8e0d00a7fe8.png)

1. [メッセージを入れて送信](#起動メッセージ)<br>![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/41d79903-ee75-33dc-5d22-ce72f8ac3f4d.png)

1. 相手にチャットが送られる<br>![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/493628ce-067d-dd63-6e4e-758c4ce426a4.png)


# 詳細
## 起動メッセージ

<details><summary>Adaptive Card 例</summary>

```json: 起動
{
    "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
    "type": "AdaptiveCard",
    "version": "1.4",
    "body": [
        {
            "type": "TextBlock",
            "wrap": true,
            "text": "メッセージの投稿者へ"
        },
        {
            "type": "Input.Text",
            "placeholder": "送信したいメッセージを入れてください",
            "id": "SendingMessage"
        },
        {
            "type": "Input.Toggle",
            "value": "false",
            "title": "メンション？",
            "id": "DoMention"
        }
    ],
    "actions": [
        {
            "type": "Action.Submit",
            "title": "メッセージ送信"
        }
    ]
}

```
</details>

## 送信メッセージの準備

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/fcef6d0a-8fe2-213d-287b-28e7b2f44873.png)
- User/Application によって以下が違う
    - UserID or ApplicationID
    - ConversationID を作る際、User/Application のどちらのIDを先に書くかが違う

<details><summary>コード例</summary>

```json: メッセージ準備
{
    "id": "647917fe-298a-40b1-a961-61c01452f8bd",
    "brandColor": "#8C3900",
    "connectionReferences": {
        "shared_office365users": {
            "connection": {
                "id": "/providers/Microsoft.PowerApps/apis/shared_office365users/connections/shared-office365user-8adbcca0-19c0-4a73-8fdb-225b07b9092b"
            }
        },
        "shared_teams": {
            "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": "prepare_information",
    "operationDefinition": {
        "type": "Scope",
        "actions": {
            "Compose_IsApplication": {
                "type": "Compose",
                "inputs": "@empty(triggerBody()?['teamsFlowRunContext']?['messagePayload']?['from']?['user']?['id'])",
                "runAfter": {},
                "metadata": {
                    "operationMetadataId": "d5fbd22e-52dc-449b-8011-68b151b74c1f"
                }
            },
            "Compose_UserID": {
                "type": "Compose",
                "inputs": "@if(outputs('Compose_IsApplication'), triggerBody()?['teamsFlowRunContext']?['messagePayload']?['from']?['Application']?['id'], triggerBody()?['teamsFlowRunContext']?['messagePayload']?['from']?['user']?['id'])",
                "runAfter": {
                    "Get_my_profile_(V2)": [
                        "Succeeded"
                    ]
                },
                "description": "From/User/Id or From/Application/Id",
                "metadata": {
                    "operationMetadataId": "192d6eef-9d88-4cf3-8007-9ca2e041b8a0"
                }
            },
            "Compose_ConversationIDforApplication": {
                "type": "Compose",
                "inputs": "19:@{outputs('Get_my_profile_(V2)')?['body/id']}_@{outputs('Compose_UserID')}@unq.gbl.spaces",
                "runAfter": {
                    "Compose_UserID": [
                        "Succeeded"
                    ]
                },
                "metadata": {
                    "operationMetadataId": "c15884ff-7074-41b0-be6b-713a5370e56c"
                }
            },
            "Compose_ConversationIDforUser": {
                "type": "Compose",
                "inputs": "19:@{outputs('Compose_UserID')}_@{outputs('Get_my_profile_(V2)')?['body/id']}@unq.gbl.spaces",
                "runAfter": {
                    "Compose_UserID": [
                        "Succeeded"
                    ]
                },
                "metadata": {
                    "operationMetadataId": "258dbd69-a748-485f-9056-ba66d30975e4"
                }
            },
            "Compose_ConversationID": {
                "type": "Compose",
                "inputs": "@if(outputs('Compose_IsApplication'), outputs('Compose_ConversationIDforApplication'), outputs('Compose_ConversationIDforUser'))",
                "runAfter": {
                    "Compose_ConversationIDforUser": [
                        "Succeeded"
                    ],
                    "Compose_ConversationIDforApplication": [
                        "Succeeded"
                    ]
                },
                "metadata": {
                    "operationMetadataId": "6571229b-f45f-4338-9213-6c9d6e8bbb2e"
                }
            },
            "Create_Mention": {
                "type": "Scope",
                "actions": {
                    "Condition": {
                        "type": "If",
                        "expression": {
                            "and": [
                                {
                                    "equals": [
                                        "@triggerBody()?['cardOutputs']?['doMention']",
                                        "True"
                                    ]
                                },
                                {
                                    "equals": [
                                        "@outputs('Compose_IsApplication')",
                                        false
                                    ]
                                }
                            ]
                        },
                        "actions": {
                            "Get_an_@mention_token_for_a_user": {
                                "type": "OpenApiConnection",
                                "inputs": {
                                    "host": {
                                        "connectionName": "shared_teams",
                                        "operationId": "AtMentionUser",
                                        "apiId": "/providers/Microsoft.PowerApps/apis/shared_teams"
                                    },
                                    "parameters": {
                                        "userId": "@outputs('Compose_UserID')"
                                    },
                                    "authentication": {
                                        "type": "Raw",
                                        "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
                                    }
                                },
                                "runAfter": {},
                                "description": "Application に対しては、<at> を使ってもメンション不可だったのでUserのみ",
                                "metadata": {
                                    "operationMetadataId": "1ad12a31-c3f8-4548-b89d-bb59851c04ac"
                                }
                            }
                        },
                        "runAfter": {},
                        "metadata": {
                            "operationMetadataId": "136f19c9-170d-4d3d-bc38-99ff8beaeff2"
                        }
                    },
                    "Compose_ToMention": {
                        "type": "Compose",
                        "inputs": "@if(bool(triggerBody()?['cardOutputs']?['doMention']), outputs('Get_an_@mention_token_for_a_user')?['body/atMention'], '')",
                        "runAfter": {
                            "Condition": [
                                "Succeeded"
                            ]
                        },
                        "metadata": {
                            "operationMetadataId": "0cbf3646-0d2e-457e-937c-db3fb41f7da9"
                        }
                    }
                },
                "runAfter": {
                    "Compose_UserID": [
                        "Succeeded"
                    ]
                },
                "metadata": {
                    "operationMetadataId": "94c7f0bd-7958-48ab-946d-ffb3f858f712"
                }
            },
            "Get_my_profile_(V2)": {
                "type": "OpenApiConnection",
                "inputs": {
                    "host": {
                        "connectionName": "shared_office365users",
                        "operationId": "MyProfile_V2",
                        "apiId": "/providers/Microsoft.PowerApps/apis/shared_office365users"
                    },
                    "parameters": {},
                    "authentication": {
                        "type": "Raw",
                        "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
                    }
                },
                "runAfter": {
                    "Compose_IsApplication": [
                        "Succeeded"
                    ]
                }
            }
        },
        "runAfter": {},
        "metadata": {
            "operationMetadataId": "b5d44b73-e740-48f7-a0b5-12cb3e211d76"
        }
    }
}
```
</details>

## メッセージ送信
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/120072/01f86d11-0e20-a4b9-e6cf-85f906fb6955.png)

```json: 送信内容
<p>
<blockquote>@{triggerBody()?['teamsFlowRunContext'
    ]?['messagePayload'
    ]?['body'
    ]?['content'
    ]
    }</blockquote><br><a href=@{triggerBody()?['teamsFlowRunContext' ]?['messagePayload' ]?['linkToMessage' ]
    }>元メッセージ</a>
<br></p>
@{triggerBody()?['cardOutputs'
]?['sendingMessage'
]
}
@{outputs('Compose_ToMention')
}
```

<details><summary>コード例</summary>

```json: メッセージ送信
{
    "id": "40110dae-c821-4020-bf9b-11bedcf1aa0a",
    "brandColor": "#8C3900",
    "connectionReferences": {
        "shared_office365users": {
            "connection": {
                "id": "/providers/Microsoft.PowerApps/apis/shared_office365users/connections/shared-office365user-8adbcca0-19c0-4a73-8fdb-225b07b9092b"
            }
        },
        "shared_teams": {
            "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": "send_message",
    "operationDefinition": {
        "type": "Scope",
        "actions": {
            "Post_message_in_a_chat_or_channel": {
                "type": "OpenApiConnection",
                "inputs": {
                    "host": {
                        "connectionName": "shared_teams",
                        "operationId": "PostMessageToConversation",
                        "apiId": "/providers/Microsoft.PowerApps/apis/shared_teams"
                    },
                    "parameters": {
                        "poster": "User",
                        "location": "Group chat",
                        "body/recipient": "@outputs('Compose_ConversationID')",
                        "body/messageBody": "<p><blockquote>@{triggerBody()?['teamsFlowRunContext']?['messagePayload']?['body']?['content']}</blockquote><br><a href=@{triggerBody()?['teamsFlowRunContext']?['messagePayload']?['linkToMessage']}>元メッセージ</a>\n<br></p>\n@{triggerBody()?['cardOutputs']?['sendingMessage']}\n@{outputs('Compose_ToMention')}"
                    },
                    "authentication": {
                        "type": "Raw",
                        "value": "@json(decodeBase64(triggerOutputs().headers['X-MS-APIM-Tokens']))['$ConnectionKey']"
                    }
                },
                "runAfter": {},
                "metadata": {
                    "operationMetadataId": "2cb00491-f733-41e0-b54a-228f4a38eed7"
                }
            }
        },
        "runAfter": {
            "prepare_information": [
                "Succeeded"
            ]
        },
        "metadata": {
            "operationMetadataId": "19ee1cdc-ea8f-4da5-8338-640a6a10d806"
        }
    }
}
```
</details>

# あとがき
Teams から起動出来るのは便利なんだけど、色々作ると起動が面倒なので、整理方法が欲しい
win + . でアイコン追加するべきなのかも