我一直在努力从 c# 控制台应用程序向托管在 Azure 中的 Skype 机器人发送直接消息,但我不断收到错误消息:操作返回了无效的状态代码“未授权”,但我提供了以下凭据:

Web.Config 文件

  <appSettings>
    <add key="BotId" value="myBotId" />
    <add key="MicrosoftAppId" value="AppId" />
    <add key="MicrosoftAppPassword" value="Password" />
  </appSettings>

以上我都确定了!所以不可能那样。

我的控制台应用程序代码:
var userAccount = new ChannelAccount(name: "User", id: "default-user");
var botAccount = new ChannelAccount(name: "Bot", id: "id");
var connector = new ConnectorClient(new Uri("https://f2d92691.ngrok.io"));

IMessageActivity message = Activity.CreateMessageActivity();

if (!string.IsNullOrEmpty(conversationId) && !string.IsNullOrEmpty(channelId))
{
    message.ChannelId = channelId;
}
else
{
    conversationId = (await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount)).Id;
}

message.From = botAccount;
message.Recipient = userAccount;
message.Conversation = new ConversationAccount(id: conversationId);
message.Text = messageText;
message.Locale = "en-Us";

await connector.Conversations.SendToConversationAsync((Activity)message);

这是我用来使用机器人模拟器的代码。

以前有没有人做过这个,请注意我可以使用这个例子发送消息:https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages

但这是在机器人本身中使用的代码,我不想在机器人之外使用它。

谢谢

最佳答案

找到解决方案,如果您使用代码在机器人外部发送消息,只需添加此行。MicrosoftAppCredentials.TrustServiceUrl(URL);

关于c# - Microsoft Botframework : Direct Conversation with Bot Channel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49737956/

10-13 09:32