使用 Direct Line v3.0 NuGet 包向我的机器人发送消息

Sending a message to my bot using the Direct Line v3.0 NuGet package

我正在尝试在 Github 上使用 Direct Line v3.0 NuGet package to send a message to my bot. I am following the sample,但我没有得到预期的行为。

示例代码如下:

DirectLineClient client = new DirectLineClient(directLineSecret);        
var conversation = await client.Conversations.StartConversationAsync();

while (true)
{
    string input = Console.ReadLine().Trim();

    if (input.ToLower() == "exit")
    {
        break;
    }
    else
    {
        if (input.Length > 0)
        {
            Activity userMessage = new Activity
            {
                From = new ChannelAccount(fromUser),
                Text = input,
                Type = ActivityTypes.Message
            };

            await client.Conversations.PostActivityAsync(conversation.ConversationId, userMessage);
        }
    }
}

这是我的代码:

var directLineSecret = "MY_SECRET";
var client = new DirectLineClient(directLineSecret);
var conversation = await client.Conversations.StartConversationAsync();

var testActivity = new Activity
{
    From = new ChannelAccount(name: "Proactive-Engine"),
    Type = ActivityTypes.Message,
    Text = "Hello from the PCE!"
};

var response = await client.Conversations.PostActivityAsync(conversation.ConversationId, testActivity);

我正在记录我的机器人收到的所有消息。我可以使用 Bot Emulator 在 Azure 上的端点与机器人交谈,所以我相信它正在通过网络聊天 API 工作。但是,当我 运行 上面的代码时,机器人只记录一条 conversationUpdate 消息。我发送的消息没有被记录,response 的值为 null

我希望有人能帮我找出我哪里出错了。谢谢!

看demo如何实例化ChannelAccount:

new ChannelAccount(fromUser)

然后看ChannelAccount构造函数签名:

public ChannelAccount(string id = null, string name = null)

这意味着 fromUser 作为 id 传递。但是看看你是如何实例化 ChannelAccount:

new ChannelAccount(name: "Proactive-Engine")

该代码不传递 id,它传递 name。所以,你可以这样改变它:

new ChannelAccount("Proactive-Engine")

如果您的聊天机器人需要 name,则像这样实例化:

new ChannelAccount("MyChatbotID", "MyChatbotName")