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

649 views Asked by At

I am trying to use the Direct Line v3.0 NuGet package to send a message to my bot. I am following the sample on Github, but I'm not getting the behavior I expect.

Here is the sample code:

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);
        }
    }
}

And here is my code:

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);

I'm logging all the messages my bot receives. I can talk to the bot at its endpoint on Azure using the Bot Emulator, so I have confidence that it's working through the web chat API. However when I run the code above, the bot logs only a conversationUpdate message. The message I send does not get logged, and the value of response is null.

I'm hoping someone can help me find out where I'm going wrong here. Thanks!

1

There are 1 answers

1
Joe Mayo On BEST ANSWER

Look at how the demo instantiates ChannelAccount:

new ChannelAccount(fromUser)

Then look at the ChannelAccount constructor signature:

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

This means that fromUser is passed as id. But look at how you instantiated ChannelAccount:

new ChannelAccount(name: "Proactive-Engine")

That code doesn't pass an id, it passes a name. So, you can change it like this:

new ChannelAccount("Proactive-Engine")

If your chatbot needs the name, then instantiate like this:

new ChannelAccount("MyChatbotID", "MyChatbotName")