【问题标题】:Sending a message to my bot using the Direct Line v3.0 NuGet package使用 Direct Line v3.0 NuGet 包向我的机器人发送消息
【发布时间】:2017-09-09 23:58:07
【问题描述】:

我正在尝试使用 Direct Line v3.0 NuGet package 向我的机器人发送消息。我在 Github 上关注 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 上的端点与 bot 对话,因此我确信它正在通过 Web 聊天 API 工作。但是,当我运行上面的代码时,机器人只记录一条 conversationUpdate 消息。我发送的消息没有被记录,response 的值是null

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

【问题讨论】:

    标签: c# botframework direct-line-botframework


    【解决方案1】:

    看看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")
    

    【讨论】:

    • 就是这样。感觉很愚蠢,但有时事情就是这样。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-09-21
    • 2019-06-27
    • 2018-05-17
    • 1970-01-01
    • 2021-04-03
    • 2019-07-26
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多