gpt4 book ai didi

c# - 创建对话时机器人框架未经授权

转载 作者:行者123 更新时间:2023-11-30 20:30:56 26 4
gpt4 key购买 nike

我正在尝试通过 Web 作业恢复机器人和用户之间的对话,但收到未经授权的异常。我可以成功回复 MessagesController 类中的对话,但是当我尝试在 Web 作业中运行以下代码时,出现以下异常:

    private static async Task SendAlertToUser(ChannelAccount botAccount, LipSenseUser user, AlertFlags alert, string extra)
{
Console.WriteLine($"Sending alert to user: {user.Name}");
var sb = new StringBuilder(GetTextFromAlert(alert));
if (!string.IsNullOrEmpty(extra))
{
sb.AppendLine();
sb.Append(extra);
}

var userAccount = new ChannelAccount(user.ChannelId, user.Name);
var connector = new ConnectorClient(new Uri(user.ChannelUri));

var message = Activity.CreateMessageActivity();
message.From = botAccount;
message.Recipient = userAccount;

var conversation = await connector.Conversations.CreateDirectConversationAsync(botAccount, userAccount);

message.Conversation = new ConversationAccount(id: conversation.Id);
message.Locale = "en-Us";
message.Text = sb.ToString();

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

异常(exception)是:

Exception:System.UnauthorizedAccessException
Authorization for Microsoft App ID 58c04dd1-1234-5678-9123-456789012345 failed with status code Unauthorized and reason phrase 'Unauthorized'

当我检查连接器的凭据时,我发现一切都正确。我在 MessagesController 中设置了一个断点,并从那里检查了连接器的凭据,一切都是相同的。

此外,当我查看 IntelliTrace 时,我看到以下消息:

Bot Framework IntelliTrace Errors

我的 user.ChannelUri 是“https://facebook.botframework.com ”,这是我在用户初始化对话时从用户那里获取的。这不是正确的 Uri 吗?

我还需要做什么才能发送消息吗?我的 App.config appSettings 如下所示:

<appSettings>
    <add key="BotId" value="MyBotName" />
    <add key="MicrosoftAppId" value="58c04dd1-1234-5678-9123-456789012345" />
    <add key="MicrosoftAppPassword" value="5xyadfasdfaweraeFAKEasdfad" />
    <add key="AzureWebJobsStorage" value="DefaultEndpointsProtocol=https;AccountName=BLAH;AccountKey=THIS IS A KEY" />
</appSettings>

最佳答案

Bot Framework 团队在不同 channel 上的回答:

You need to add a call to:

MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

This is done automatically when you are replying to a message, but for proactive messages from another process you need to do this.

关于c# - 创建对话时机器人框架未经授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44010572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com