我可以使用 bot 框架以 send/update 消息作为 MS Teams 中的用户吗?
Can I use bot framework to send/update message as a user in MS Teams?
是否可以使用机器人框架作为用户而不是机器人作为消息发送者来发送/更新消息(可能在允许机器人执行此类操作的用户进行某种形式的身份验证之后)?
以下是当前情况的说明:
我已经将 A 人的消息发送到 Teams 频道,我想使用机器人框架对消息进行更新,因为 Graph API 不支持更新消息。但是,尽管没有错误,但消息并未更新。
这被放置在网络 api 控制器“/test”中。因此,将通过向 /test.
发送 POST 来触发更新
ServiceClientCredentials service = new CustomLoginCredentials();
var connectorClient = new ConnectorClient(new Uri("https://smba.trafficmanager.net/apac/"), service);
var newActivity = MessageFactory.Text($"hello: updated on {DateTime.Now}");
string destActivityId = "this is the activity id of the existing message sent using person A account on MS Teams";
string conversationId = $"channelidhere;messageid={destActivityId}";
connectorClient.Conversations.UpdateActivityAsync(conversationId, destActivityId, newActivity, default(CancellationToken));
CustomLoginCredentials
public class CustomLoginCredentials:ServiceClientCredentials
{
private string AuthenticationToken { get; set; }
public override void InitializeServiceClient<T>(ServiceClient<T> client)
{
System.Diagnostics.Debug.WriteLine("CustomLoginCredentials:InitializeServiceClient 1");
IPublicClientApplication publicClient = PublicClientApplicationBuilder.Create("clientid")
.WithAuthority(AzureCloudInstance.AzurePublic, "tenantId")
.Build();
AuthenticationResult authenticationResult = publicClient.AcquireTokenByUsernamePassword(new string[] { "https://graph.microsoft.com/.default" }, "personA MS email", new NetworkCredential("", "personA MS email password").SecurePassword).ExecuteAsync().GetAwaiter().GetResult();
AuthenticationToken = authenticationResult.AccessToken;
}
public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null) throw new ArgumentNullException("request");
if (AuthenticationToken == null) throw new InvalidOperationException("Token Provider Cannot Be Null");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
在机器人代表用户发送消息的情况下,将消息归因于该用户有助于参与并展示更自然的交互流程。此功能允许您将来自您的 bot 的消息归因于代表其发送消息的用户。您可以使用代表属性作为用户发送消息 - 请检查 User attribution for bots messages
是否可以使用机器人框架作为用户而不是机器人作为消息发送者来发送/更新消息(可能在允许机器人执行此类操作的用户进行某种形式的身份验证之后)?
以下是当前情况的说明: 我已经将 A 人的消息发送到 Teams 频道,我想使用机器人框架对消息进行更新,因为 Graph API 不支持更新消息。但是,尽管没有错误,但消息并未更新。
这被放置在网络 api 控制器“/test”中。因此,将通过向 /test.
发送 POST 来触发更新ServiceClientCredentials service = new CustomLoginCredentials();
var connectorClient = new ConnectorClient(new Uri("https://smba.trafficmanager.net/apac/"), service);
var newActivity = MessageFactory.Text($"hello: updated on {DateTime.Now}");
string destActivityId = "this is the activity id of the existing message sent using person A account on MS Teams";
string conversationId = $"channelidhere;messageid={destActivityId}";
connectorClient.Conversations.UpdateActivityAsync(conversationId, destActivityId, newActivity, default(CancellationToken));
CustomLoginCredentials
public class CustomLoginCredentials:ServiceClientCredentials
{
private string AuthenticationToken { get; set; }
public override void InitializeServiceClient<T>(ServiceClient<T> client)
{
System.Diagnostics.Debug.WriteLine("CustomLoginCredentials:InitializeServiceClient 1");
IPublicClientApplication publicClient = PublicClientApplicationBuilder.Create("clientid")
.WithAuthority(AzureCloudInstance.AzurePublic, "tenantId")
.Build();
AuthenticationResult authenticationResult = publicClient.AcquireTokenByUsernamePassword(new string[] { "https://graph.microsoft.com/.default" }, "personA MS email", new NetworkCredential("", "personA MS email password").SecurePassword).ExecuteAsync().GetAwaiter().GetResult();
AuthenticationToken = authenticationResult.AccessToken;
}
public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null) throw new ArgumentNullException("request");
if (AuthenticationToken == null) throw new InvalidOperationException("Token Provider Cannot Be Null");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
在机器人代表用户发送消息的情况下,将消息归因于该用户有助于参与并展示更自然的交互流程。此功能允许您将来自您的 bot 的消息归因于代表其发送消息的用户。您可以使用代表属性作为用户发送消息 - 请检查 User attribution for bots messages