今回は、MS Graph APIを使用して、TeamsのChannelへメッセージを投稿する方法について試してみた。
前提として、投稿を行うには、TeamのIDとChannelのIDが必要となる
また、メンション付きメッセージにする場合は、Channel全体へのメンションはChannelのID,個人向けのメンションはADユーザーのObject IDが必要。ユーザーの場合はUserPrincipalNameでは無いので注意が必要。
投稿には、下記の権限が必要。
- ChannelMessage.Send
- Group.ReadWrite.All(下位互換性のためにのみサポート)
まず、投稿用のメッセージを作成する。
var msg = new ChatMessage
{
Subject = "テストメッセージ",
Body = new ItemBody
{
// メンションありの場合はHTMLにする必要あり
ContentType = BodyType.Html,
Content = """
<at id="0">メンション先</at><br/>
!!!テストです!!!<br/>
""",
},
// 重要度
Importance = ChatMessageImportance.High,
// メンションの対象指定
Mentions = new List<ChatMessageMention>() {
new ChatMessageMention() {
Id = 0, // <at id="0">
MentionText = "<メンション先の表示名>",
Mentioned = new ChatMessageMentionedIdentitySet() {
/* Channelにメンションする場合
Conversation = new TeamworkConversationIdentity() {
Id = "<Channel ID>",
DisplayName = "<表示名>",
ConversationIdentityType = TeamworkConversationIdentityType.Channel
}
*/
// ユーザーにメンションする場合
User = new Identity() {
DisplayName = "<表示名>",
Id = "<ユーザーID>", // UserPrincipalNameではなく、ObjectIdが必要!!
}
}
}
}
};次にメッセージを投稿する。
※メッセージの送信元はGraphにログインしたユーザーとなる。
// Chanelにメッセージ投稿
await cli.Teams[<Team ID>]
.Channels[<Channel ID>]
.Messages
.PostAsync(msg);
ちなみに、メンション先の指定である、ChatMessageMention中のMentionTextと本文中の<at id="・・・">[表示名]</at>の[表示名]が合っていないと、例外が発生するようだ。この[表示名]は実際のユーザーの表示名やChannel名と異なっていても、メッセージ上には[表示名]が表示される。(ChatMessageMentionedIdentitySet中のDisplayNameは無視されるようだ・・・)






Users Today : 39
Users Yesterday : 44
Users Last 7 days : 233
Users Last 30 days : 1002
Users This Month : 842
Users This Year : 15668
Total Users : 93472
Views Today : 50
Views Yesterday : 53
Views Last 7 days : 288
Views Last 30 days : 1274
Views This Month : 1050
Views This Year : 21255
Total views : 131190
Who's Online : 0