我有一个基于BotFramework 3.5构建的机器人,并作为WebApp托管在Azure上。我没有遇到机器人需要响应用户输入的情况的实施问题。但是,有必要教他按一些时间表开始对话。 为了达到目标,我创建了一个WebJob,它基本上是一个简单的控制台应用程序。以下是用于启动从bot到用户的消息的代码:
var botAccount = new ChannelAccount(id: from);
var userAccount = new ChannelAccount(id: to);
var conversation = new ConversationAccount(false, conversationId);
var connector = new ConnectorClient(serviceUrl);
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = conversation;
message.Text = text;
message.Locale = locale;
await connector.Conversations.SendToConversationAsync((Activity)message);
from, to, serviceUrl, conversationId
- 取自之前的对话,所以我希望它们有效。但是在SendToConversationAsync
抛出异常时:
System.UnauthorizedAccessException: Authorization for Microsoft App ID 3a26a4d4-f75a-4feb-b3e0-37a7fa24e5fc failed with status code Unauthorized and reason phrase 'Unauthorized' ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 401 (Unauthorized)
app.config文件包含与原始bot API相同的值,包括AppId和AppSecret。我在同一主题上看到了一些问题,但没有找到答案。
我错过了什么吗?它是从控制台应用程序代表bot发送消息的有效方法吗?
答案 0 :(得分:18)
根据您的说明,我按照此tutorial开始使用连接器,并按照tutorial进行发送和接收活动。
根据你的代码,我创建了我的控制台应用程序,我可以重现同样的问题,然后我找到了关于类似问题的git issue。经过一些试验,我可以按照预期在我身边工作,你可以参考它:
MicrosoftAppCredentials.TrustServiceUrl("{ServiceUrl}", DateTime.Now.AddDays(7));
var account=new MicrosoftAppCredentials("MicrosoftAppIdKey", "MicrosoftAppPasswordKey");
var connector = new ConnectorClient(new Uri("{ServiceUrl}"),account);
或强>
实施DelegatingHandler
public class MyDelegatingHandler : DelegatingHandler
{
private string _token;
public MyDelegatingHandler(string token)
{
_token = token;
}
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", _token);
return base.SendAsync(request, cancellationToken);
}
}
然后,您需要按如下方式构建ConnectorClient
:
var account=new MicrosoftAppCredentials("{MicrosoftAppIdKey}", "{MicrosoftAppPasswordKey}");
var jwtToken=await account.GetTokenAsync();
var connector = new ConnectorClient(new Uri("{serviceUrl}"),handlers:new MyDelegatingHandler(jwtToken));
这是我的控制台应用程序代码段,您可以参考它:
try
{
var userAccount = new ChannelAccount() { Id = "default-user", Name = "user" };
var botAccount = new ChannelAccount() { Id = "934493jn5f6f348f", Name = "console-Bot" };
string url = "{serviceUrl}";
MicrosoftAppCredentials.TrustServiceUrl(url, DateTime.Now.AddDays(7));
var account = new MicrosoftAppCredentials("{MicrosoftAppIdKey}", "{MicrosoftAppPasswordKey}");
var connector = new ConnectorClient(new Uri(url), account);
IMessageActivity message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount() { Id = "{conversationId}" };
message.Text = "Message sent from console application!!!";
message.Locale = "en-us";
var response = await connector.Conversations.SendToConversationAsync((Activity)message);
Console.WriteLine($"response:{response.Id}");
}
catch (Exception e)
{
Console.WriteLine($"exception:{e.Message}\r\n{e.StackTrace}");
}
<强>结果强>
答案 1 :(得分:0)
尝试创建这样的新会话:
var conversationId = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);