Search code examples
c#.netbotframeworkdirect-line-botframework

Botframework DirectLineClient.Conversations.PostActivityAsync returns null


I'm trying to send an activity through DirectLineClient library to my bot :

        var directLineClient = new DirectLineClient($"{secret}");
        directLineClient.BaseUri = new Uri($"https://directline.botframework.com/");

        var conversation = await directLineClient.Conversations.StartConversationAsync().ConfigureAwait(false);
        var activity = new Microsoft.Bot.Connector.DirectLine.Activity();
        activity.From = new Microsoft.Bot.Connector.DirectLine.ChannelAccount();
        activity.From.Name = "Morgan";
        activity.Text = message;
        activity.Type = "message";

        var resourceResponse = await directLineClient.Conversations.PostActivityAsync(conversation.ConversationId, activity).ConfigureAwait(false);
        await ReadBotMessagesAsync(directLineClient, conversation.ConversationId);

resourceResponse is always null.

Edit after Nicolas R answer

I added a method to wait for a response from the bot :

    private static async Task ReadBotMessagesAsync(DirectLineClient client, string conversationId)
    {
        string watermark = null;

        while (true)
        {
            var activitySet = await client.Conversations.GetActivitiesAsync(conversationId, watermark);
            watermark = activitySet?.Watermark;

            foreach (Microsoft.Bot.Connector.DirectLine.Activity activity in activitySet.Activities)
            {
                Console.WriteLine(activity.Text);

                if (activity.Attachments != null)
                {
                    foreach (Microsoft.Bot.Connector.DirectLine.Attachment attachment in activity.Attachments)
                    {
                        Console.WriteLine(attachment.ContentType);
                    }
                }
            }
            if (activitySet.Activities.Count > 0)
            {
                return;
            }
            await Task.Delay(TimeSpan.FromSeconds(1)).ConfigureAwait(false);
        }
    }

But I never get out of ReadBotMessagesAsync.

I precise that I can communicate with my bot through HTTP request (tested with Postman), and it should be sending a response message whenever a message is sent.


Solution

  • Edited after OP precision

    Methods always returns null

    Based on the documentation/samples, it looks like this PostActivityAsync return is never used so the value may not be relevant.

    From the samples:

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

    See example here.

    For those who want more details, because this answer is only limited to the comparison with the sample use, this package is sadly not open-source: https://github.com/Microsoft/BotBuilder/issues/2756

    Remarks (for those who would be using the wrong packages)

    I would not recommend to use this DirectLineClient Nuget package located here: https://www.nuget.org/packages/DirectLineClient as it is not maintained since May 2016 and the Bot Framework has changed a lot since that time.

    Moreover, it is using DirectLine API 1.0, which is not the best practice at the time. See documentation here:

    Important

    This article introduces key concepts in Direct Line API 1.1 and provides information about relevant developer resources. If you are creating a new connection between your client application and bot, use Direct Line API 3.0 instead.