Power AppsのアプリからTeamsへメッセージを投稿するPostMessageToConversationアクション(チャットまたはチャネルでメッセージを投稿する)にはいくつかの投稿者・投稿先のパターンが用意されています。
このパターンごとにそれぞれどのような数式を設定する必要があり、どのような結果になるのかを紹介します。
PostMessageToConversationアクションの基本的な構文などは以下の記事をご覧ください。
投稿者がユーザー(poster:"User")
チャネルに投稿する
GalleryTeams.Selected.idはチームID、TabListChannels.Selected.idはチャネルIDで、それぞれ以下のアクションで取得・ギャラリーなどの一覧から選択しています。

ボタンのOnSelectなどに以下の数式を設定することでチームにメッセージを投稿することができます。
UpdateContext({
_body:
{
recipient:
{
groupId: GalleryTeams.Selected.id,
channelId: TabListChannels.Selected.id
},
messageBody: "本文",
subject: "件名"
}
});
MicrosoftTeams.PostMessageToConversation("User", "Channel", ParseJSON(JSON(_body)))
ボタンをクリックしたユーザーが投稿したことになっています。

グループチャットに投稿する
コンボボックスに以下の画像のキャプションの数式を設定することで、グループチャットの一覧を取得できます。

チャットのIDを取得できるようにしたうえで、以下の数式によってグループチャットにメッセージを送信できます。
UpdateContext({
_body:
{
recipient: ComboBoxChats.Selected.id,
messageBody: "本文"
}
});
MicrosoftTeams.PostMessageToConversation("User", "Group chat", ParseJSON(JSON(_body)))

ユーザー個人・自分にメッセージを送信する
グループチャットだけでなく、ユーザー個人や自分にメッセージを送ることもできます。
Teamsのウェブ版で受信者とのチャット画面を開き、ハイライトしている部分をコピーします。

この部分がチャットのconversationIDとなるので、グループチャットのrecipientに貼り付けます。

UpdateContext({
_body:
{
recipient: "19:12~71c@unq.gbl.spaces",
messageBody: "本文"
}
});
MicrosoftTeams.PostMessageToConversation("User", "Group chat", ParseJSON(JSON(_body)))
自分に対しても同様にPower Appsからメッセージを投稿することができます。

投稿者がフローボット(poster:"Flow bot")
フローボットはユーザー個人で投稿可能なチャネルとグループチャットに加えて、フローボットとのチャットグループにも投稿が可能です。
チャネルに投稿する
ボタンのOnSelectに以下の数式を設定することでフローボットとしてチャネルに投稿が可能です。
UpdateContext({
_body:
{
recipient:
{
groupId: GalleryTeams.Selected.id,
channelId: TabListChannels.Selected.id
},
messageBody: "本文"
}
});
MicrosoftTeams.PostMessageToConversation("Flow bot", "Channel", ParseJSON(JSON(_body)))
件名は設定できないようです。

グループチャットに投稿する
ユーザーとして投稿と同様にグループチャットに投稿されます。
UpdateContext({
_body:
{
recipient: ComboBoxChats.Selected.id,
messageBody: "本文"
}
});
MicrosoftTeams.PostMessageToConversation("Flow bot", "Group chat", ParseJSON(JSON(_body)))

ユーザー個人のconversationIDを設定すると、1対1のチャットににフローボットが乱入しました。

フローボットとのチャット(Chat with Flow bot)に投稿
以下の式でフローボットとのチャットルームにフローボットからメッセージが送信されます。
UpdateContext({
_body:
{
recipient: User().Email,
messageBody: "本文",
isAlert:true
}
});
MicrosoftTeams.PostMessageToConversation("Flow bot", "Chat with Flow bot", ParseJSON(JSON(_body)))

recipientにはconversationIDではなくメールアドレスを入力します。
isAlertをtrueにするとユーザーに通知され、falseにすると通知されません。
投稿者がPower Virtual Agents(poster:"Chat with bot")
Power Virtual Agentsで作成したボットが紐づけられているチームと同じチームで作成したアプリ・フローからしかボットとチャットをすることができません。
事前準備
事前準備として、Power Virtual AgentsをTeamsで作成します。

ボットの詳細画面を開き、設定→詳細からスキーマ名をコピーします。

アプリの作成
TeamsでPower Appsを開き、ビルド→すべて表示を選択します。

新規→キャンバスアプリから、画面サイズを選択します。

Teamsコネクタを追加します。

ボタンを追加し、以下の数式を設定します。
UpdateContext({
_body:
{
bot: "cr80e_copilot1",
recipient: User().Email,
messageBody: "本文",
attribution: true,
activeChat: "Send",
installedError: "Fail"
}
});
MicrosoftTeams.PostMessageToConversation("Power Virtual Agents", "powerva", ParseJSON(JSON(_body)))
ボットからメッセージが送信されました。

この数式のパラメーターは以下の表の通りです。
プロパティ | 値 | 説明 |
---|---|---|
attribution | true / false | ユーザーに通知するかどうか |
activeChat | "Send" / "Don't send and succeed" / "Don't send and fail" | ボットとのやり取りがアクティブな時の挙動 "送信する" / "送信せず成功にする" / "送信せず失敗にする" |
installedError | "Fall" / "Succeed with status code" | "失敗" / "成功にしてステータスコードを表示" |
コメント