![](/img/trans.png)
[英]How to send message to 1:1 chat or to group chat in Microsoft Teams via microsoft graph API C#
[英]How to create a 1:1 Teams-Chat via Graph | Graph API | C#
我正在為我的組織開發一個控制台應用程序,它將獲取電子郵件 ID 列表並從某個電子郵件 ID 向他們發送一對一的聊天消息。
發件人和收件人 - 都來自同一組織。
為了在我的本地機器上進行測試,我在 Azure AD 中使用我的個人電子郵件(不同於組織電子郵件)注冊了一個應用程序,這些是權限。 所有這些都只是Application Permissions
而我沒有設置任何Delegated Permissions
。
這是創建聊天的代碼。
private static async void CreateChat(string token)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
GraphServiceClient graphClient = new GraphServiceClient(httpClient);
var chat = new Chat
{
ChatType = ChatType.OneOnOne,
Members = new ChatMembersCollectionPage()
{
new AadUserConversationMember
{
Roles = new List<String>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{"user@odata.bind", "https://graph.microsoft.com/v1.0/users('$my_org_name@myorg.com')"}
}
},
new AadUserConversationMember
{
Roles = new List<String>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{"user@odata.bind", "https://graph.microsoft.com/v1.0/users('$receiver_org_name@myorg.com')"}
}
}
}
};
var chatResult = graphClient.Chats
.Request()
.AddAsync(chat);
}
我在聊天結果中看到了這個:
為了測試,我是否應該僅使用我的組織電子郵件在 Azure AD 中注冊我的應用程序。
我正在關注示例 3:使用用戶主體名稱創建一對一聊天
這是我的代碼並且有效,但我使用我的組織帳戶而不是我的私人帳戶。
///GetUser
GraphServiceClient graphServiceClient = this.GetAuthenticatedGraphClient(scopes);
try
{
var result = await graphServiceClient.Users[userEmail].Request().GetAsync();
return result;
}
catch (Exception ex)
{
if (raiseError == false)
return null;
else
throw;
///Create Chat
foreach (var curUser in userList)
graphUsers.Add(curUser, base.GetUser(curUser, false, scopes));
var chat = new Chat
{
ChatType = ChatType.OneOnOne,
Members = new ChatMembersCollectionPage()
{
new AadUserConversationMember
{
Roles = new List<string>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{ "user@odata.bind", "https://graph.microsoft.com/v1.0/users('" + userId + "')" }
}
},
new AadUserConversationMember
{
Roles = new List<string>()
{
"owner"
},
AdditionalData = new Dictionary<string, object>()
{
{ "user@odata.bind", "https://graph.microsoft.com/v1.0/users('"+ userList[0] +"')" }
}
}
}
};
return graphClient.Chats.Request().AddAsync(chat);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.