Martin Odongo
Martin Odongo

Reputation: 29

Send message to Chat Bot when while loop is executing

I am trying to send a message in the middle of a while loop using my bot. Is that possible before breaking? From the code bellow, I am looping and automatically posting message to bot depending on the response state i get from the service.... that is when the value changes. Up to until i get to level 5, I am not breaking. Is there a way i can continue conversation before the loop is actually done?

int prev = 2;

do
{
    StatusOption(reply);
    context.Wait(StatusSelected);

    {
        int ride_status = prev;
        context.UserData.TryGetValue<int>("ride_status", out ride_status);
        string trip_status = CheckTripStatus(res[3].ToString());
        prev = ride_status;
        int current = Convert.ToInt32(trip_status);

        if (prev != current)
        {
            if (current == 2)
            {
                reply.Text = "Your driver is coming to pick you.";

                await context.PostAsync(reply);
                context.UserData.SetValue<int>("ride_status", current);
            }
            else if (current == 3)
            {

                reply.Text = "Your driver has arrived.";
                await context.PostAsync(reply);
                context.UserData.SetValue<int>("ride_status", current);
            }
            else if (current == 4)
            {
                reply.Text = "Your trip has started.";
                await context.PostAsync(reply);
                context.UserData.SetValue<int>("ride_status", current);
            }
            else if (current == 5)
            {
                reply.Text = "Your trip has ended.";
                await context.PostAsync(reply);
                context.UserData.SetValue<int>("ride_status", current);
                break;
            }
            else
            {
                StatusOption(reply);
                context.Wait(StatusSelected);
            }
        }
        Thread.Sleep(5000);
    }

}

while (prev <= 5) ;

Upvotes: 0

Views: 424

Answers (1)

Eric Dahlvang
Eric Dahlvang

Reputation: 8292

Conceptually, it is not a good design practice to consume a context thread in a long running loop. Check out the Azure Functions Bot Proactive Template. It adds a message that contains a ConversationReference to a Microsoft.WindowsAzure.Storage.Queue A separate function is triggered when anything is added to the queue, and calls the bot to send a message. This is a more scaleable and manageable design.

When someone requests a ride, you could add their ConversationReference to the queue. Instead of automatically triggering the response, have the function be triggered by changes in the ride status.


Another option, not using functions, would be to store the ConversationReference somewhere, and expose another WebApi endpoint in your bot's project. When the status changes, call this endpoint and use the ConversationReference to proactively send the rider the status message. Some proactive messaging documentation can be found here: https://learn.microsoft.com/en-us/azure/bot-service/dotnet/bot-builder-dotnet-proactive-messages?view=azure-bot-service-3.0 (code examples: https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-proactiveMessages ) Note: these are just examples, and are using static variables. Nonetheless, they are a good starting point outlining how to do proactive messaging.

Upvotes: 1

Related Questions