1
votes

In local bot work perfectly fine but on web chat channel 'There was an error sending this message to your bot: HTTP status code Gateway Timeout' error occurred but bot run correctly after 2nd respond

Controller code

public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
    {

        if (activity.Type == ActivityTypes.Message)
        {
            await Conversation.SendAsync(activity, () => new EchoDialog());
        }
         else if (message.Type == ActivityTypes.ConversationUpdate)
        {
            // Handle conversation state changes, like members being added and removed
            // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
            // Not available in all channels
            IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
            if (iConversationUpdated != null)
            {
                ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
                foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
                {
                    // if the bot is added, then   
                    if (member.Id == iConversationUpdated.Recipient.Id)
                    {
                        var reply = ((Activity)iConversationUpdated).CreateReply("Hi, Welcome to Systenics.");
                        await connector.Conversations.ReplyToActivityAsync(reply);
                        await Conversation.SendAsync(message, () => new EchoDialog());
                    }
                }
            }


        }
        else
        {
           await HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
    }

Chat dialog

public async Task StartAsync(IDialogContext context)
    {
        context.Wait(this.ShowOptions);
    }
    public virtual async Task ShowOptions(IDialogContext context, IAwaitable<IMessageActivity> activity)
    {
        var message = await activity;
        var descriptions = new string[] { "Request a Quote", "More Information About Jobs" };
        PromptDialog.Choice(
            context: context,
            resume: ChoiceReceivedAsync,
            options: descriptions,
            prompt: "Please select an option below:",
            retry: "Selected option not available.",
            promptStyle: PromptStyle.Auto
            );
    }

enter image description here

enter image description here

1
have you checked logs? please share logs which will help to resolve this.Sagar Solanki
[12:52:53] -> POST 200 [conversationUpdate] [12:52:53] <- POST 200 Reply[message] Hi, Welcome to Systenics. [12:52:54] <- POST 200 Reply[message] application/vnd.microsoft.card.hero [12:52:54] -> POST 200 [conversationUpdate] [12:52:56] <- POST 200 Reply[message] Click on this link to know more about Jobs https:/... [12:52:56] <- POST 200 Reply[message] Thank You. [12:52:57] -> POST 200 [message] More Information About JobsAkshit Joshi
Is this log from local or form azure?Sagar Solanki
yes log from localAkshit Joshi
You need to check logs from azure, as it already works in local.Sagar Solanki

1 Answers

1
votes

It seems that you’d like to catch the ConversationUpdate activity and create&send a fake message to initialize the dialog in order to prompt for one of a set of choices via PromptDialog.Choice() method. I suggest that you can specify Text property of activity before you send it to initialize the dialog, and then you can do different operations by checking the message activity Text property in the dialog.

Based on your scenario, I create the following sample code, which work for me on Web Chat, you can refer to it.

In MessagesController:

else if (activity.Type == ActivityTypes.ConversationUpdate)
{
    // Handle conversation state changes, like members being added and removed
    // Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
    // Not available in all channels

    IConversationUpdateActivity iConversationUpdated = activity as IConversationUpdateActivity;
    if (iConversationUpdated != null)
    {
        ConnectorClient connector = new ConnectorClient(new System.Uri(activity.ServiceUrl));
        foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
        {
            // if the bot is added, then   
            if (member.Id == iConversationUpdated.Recipient.Id)
            {
                var reply = ((Activity)iConversationUpdated).CreateReply("Hi, Welcome to Systenics.");
                await connector.Conversations.ReplyToActivityAsync(reply);

                //set value to Text property
                activity.Text = "show choices";
                await Conversation.SendAsync(activity, () => new EchoDialog());
            }
        }
    }

}

In dialog:

public async Task StartAsync(IDialogContext context)
{
    context.Wait(MessageReceivedAsync);
}


public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var activity = await result as Activity;

    var mes = activity.Text?.ToString();

    if (mes == "show choices")
    {
        var descriptions = new string[] { "Request a Quote", "More Information About Jobs" };
        PromptDialog.Choice(
            context: context,
            resume: ChoiceReceivedAsync,
            options: descriptions,
            prompt: "Please select an option below:",
            retry: "Selected option not available.",
            promptStyle: PromptStyle.Auto
            );
    }
    else
    {
        if (mes == "More Information About Jobs")
        {
            var replymes = context.MakeMessage();
            replymes.Text = "Click on this link to know more about jobs [https//www.abc.com](https//www.abc.com)";

            await context.PostAsync(replymes);
        }
        else if (mes == "Request a Quote")
        {
            //your code logic here
        }
        else
        {
            await context.PostAsync($"You sent {activity.Text}");
        }


        context.Wait(MessageReceivedAsync);
    }
}

Test result:

enter image description here