我一直在努力从c#console应用程序向我托管在Azure中的skype bot发送直接消息,我继续收到错误:操作返回了无效的状态代码'Unauthorized',但我提供了以下凭据:
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);
这是我以前使用bot模拟器的代码 .
有没有人之前做过这个,请注意我可以使用这个例子发送消息:https://docs.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages
但这是机器人本身使用的代码,我不想在机器人之外使用它 .
谢谢
1 回答
找到解决方案,只需添加此行,如果您使用代码在bot之外发送消息 .
MicrosoftAppCredentials.TrustServiceUrl(URL);