【发布时间】:2018-04-10 18:46:21
【问题描述】:
我一直在努力从 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
但这是机器人本身使用的代码,我不想在机器人之外使用它。
谢谢
【问题讨论】:
-
用户在发送此消息之前是否至少与机器人聊天过一次?
-
是的,我有 ChannelAccount id 和收件人姓名以及发件人地址..
-
找到了解决方案,如果您使用代码在机器人外部发送消息,只需添加此行。MicrosoftAppCredentials.TrustServiceUrl(URL);
标签: c# botframework