是否可以从外部渠道向微软团队发送消息
Is it possible to send messages to microsoft team from external channel
我有一个 MS 团队机器人。现在,我需要向用户介绍这个。因此,机器人应该能够向一组特定用户发送消息,在 Microsoft Teams 中介绍自己。
我在数据库中有人员名单。现在,是否可以向团队中的所有人发送消息。如果是,如何?
我还没有尝试过,但大概管理员可以为用户安装该应用程序。此时机器人应该收到 conversationUpdate
并且机器人应该能够在事件处理程序中向用户发送消息。
正如@Abhijit-MSFT 在评论中提到的,这里有一个使用 Graph 为用户添加 App 的示例:https://github.com/microsoftgraph/contoso-airlines-teams-sample/blob/283523d45f5ce416111dfc34b8e49728b5012739/project/Models/GraphService.cs#L176
public async Task InstallAppToAllUsers()
{
string appid = "f46ad259-0fe5-4f12-872d-c737b174bcb4"; // Adobe
var graph = GetAuthenticatedClient();
var users = await graph.Users.Request().Select("id,displayName").GetAsync();
foreach (User user in users)
{
if (user.DisplayName.StartsWith("Megan"))
{
// Check if the app is already installed for that user.
// Use $expand to populate the teamsAppDefinition property,
// and $filter to search for the one app we care about
TeamsAppInstallation[] installs = await HttpGetList<TeamsAppInstallation>(
$"/users/{user.Id}/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/teamsAppId eq '{appid}'",
endpoint: graphBetaEndpoint);
if (installs.Length == 0)
{
// install app
await HttpPost($"/users/{user.Id}/teamwork/installedApps",
new TeamsAppInstallation()
{
AdditionalData = new Dictionary<string, object>() { ["teamsApp@odata.bind"] = $"{graphBetaEndpoint}/appCatalogs/teamsApps/{appid}" }
},
endpoint: graphBetaEndpoint);
// Bot will get a notification about the new user and the chat thread ID for that user
}
// If you've forgotten the chat thread ID for that user, you can find it again:
var chats = await HttpGetList<Chat>($"/users/{user.Id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{appid}')", endpoint: graphBetaEndpoint);
string threadId = chats[0].Id;
// Wait a little before installing the next app to avoid throttling
Thread.Sleep(1000);
}
}
}
我有一个 MS 团队机器人。现在,我需要向用户介绍这个。因此,机器人应该能够向一组特定用户发送消息,在 Microsoft Teams 中介绍自己。 我在数据库中有人员名单。现在,是否可以向团队中的所有人发送消息。如果是,如何?
我还没有尝试过,但大概管理员可以为用户安装该应用程序。此时机器人应该收到 conversationUpdate
并且机器人应该能够在事件处理程序中向用户发送消息。
正如@Abhijit-MSFT 在评论中提到的,这里有一个使用 Graph 为用户添加 App 的示例:https://github.com/microsoftgraph/contoso-airlines-teams-sample/blob/283523d45f5ce416111dfc34b8e49728b5012739/project/Models/GraphService.cs#L176
public async Task InstallAppToAllUsers()
{
string appid = "f46ad259-0fe5-4f12-872d-c737b174bcb4"; // Adobe
var graph = GetAuthenticatedClient();
var users = await graph.Users.Request().Select("id,displayName").GetAsync();
foreach (User user in users)
{
if (user.DisplayName.StartsWith("Megan"))
{
// Check if the app is already installed for that user.
// Use $expand to populate the teamsAppDefinition property,
// and $filter to search for the one app we care about
TeamsAppInstallation[] installs = await HttpGetList<TeamsAppInstallation>(
$"/users/{user.Id}/teamwork/installedApps?$expand=teamsAppDefinition&$filter=teamsAppDefinition/teamsAppId eq '{appid}'",
endpoint: graphBetaEndpoint);
if (installs.Length == 0)
{
// install app
await HttpPost($"/users/{user.Id}/teamwork/installedApps",
new TeamsAppInstallation()
{
AdditionalData = new Dictionary<string, object>() { ["teamsApp@odata.bind"] = $"{graphBetaEndpoint}/appCatalogs/teamsApps/{appid}" }
},
endpoint: graphBetaEndpoint);
// Bot will get a notification about the new user and the chat thread ID for that user
}
// If you've forgotten the chat thread ID for that user, you can find it again:
var chats = await HttpGetList<Chat>($"/users/{user.Id}/chats?$filter=installedApps/any(a:a/teamsApp/id eq '{appid}')", endpoint: graphBetaEndpoint);
string threadId = chats[0].Id;
// Wait a little before installing the next app to avoid throttling
Thread.Sleep(1000);
}
}
}