question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Why am I getting error: "There was an error sending this message to your bot: HTTP status code GatewayTimeout"?

See original GitHub issue

Bot Info

  • Bot handle: sbcbot
  • App ID: 5b1d7f0b-6905-4afa-a8a7-ada6e5fc4284
  • SDK Platform: .NET
  • Active Channels: Skype, Web chat
  • Deployment Environment: IIS

Issue Description

I have developed a bot which is using the qna maker to answer some questions. I have deployed it on the IIS server. But when I try to chat with it on the bot framework’s web chat it says “couldn’t send retry”, with issue “There was an error sending this message to your bot: HTTP status code InternalServerError”. the bot works perfectly on localhost without the MAID and Password, but doesn’t work with them in the Webconfig. I’ve tried validating the credentials using curl, and they are correct. I am not able to use ngrok as it gives me an error “Failed to start ngrok: panic: runtime error: invalid memory address or nil pointer dereference…”. It doesn’t give any reply on skype chat as well. My SDK and emulator are up to date. I doubt there is something wrong with the settings in the IIS, but there are no turorials/blogs that can show the whole process step by step. This is an issue as I am new to IIS. Any help would be really appreciated. Additionally, I have added “POST” verb to the website.

Code Example

I am not sure if there’s something wrong with my code, but here’s the main part

public class MessagesController : ApiController
    {
        public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
        {
            if (activity.Type == ActivityTypes.Message)
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                var responseString = String.Empty;
                var responseMsg = "";

                //De-serialize the response
                QnAMakerResult QnAresponse;

                // Send question to API QnA bot
                if (activity.Text.Length > 0)
                {
                    var query = "hi"; //User Query
                    var knowledgebaseId = "knowledge base id created";
                    var qnamakerSubscriptionKey = "subscription key";

                    //Build the URI
                    Uri qnamakerUriBase = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0");
                    var builder = new UriBuilder($"{qnamakerUriBase}/knowledgebases/{knowledgebaseId}/generateAnswer");

                    //Add the question as part of the body
                    var postBody = $"{{\"question\": \"{activity.Text}\"}}";

                    //Send the POST request
                    using (WebClient client = new WebClient())
                    {
                        //Set the encoding to UTF8
                        client.Encoding = System.Text.Encoding.UTF8;

                        //Add the subscription key header
                        client.Headers.Add("Ocp-Apim-Subscription-Key", qnamakerSubscriptionKey);
                        client.Headers.Add("Content-Type", "application/json");
                        responseString = client.UploadString(builder.Uri, postBody);
                    }

                    try
                    {
                        QnAresponse = JsonConvert.DeserializeObject<QnAMakerResult>(responseString);
                        responseMsg = QnAresponse.Answer;
                    }
                    catch
                    {
                        throw new Exception("Unable to deserialize QnA Maker response string.");
                    }
                }

                // return our reply to the user
                if (responseMsg == Convert.ToString("No good match found in the KB"))    
                {
                    await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
                }
                else
                {
                    Activity reply = activity.CreateReply(responseMsg);
                    await connector.Conversations.ReplyToActivityAsync(reply);
                }                                                                        

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

        private Activity HandleSystemMessage(Activity message)
        {
            if (message.Type == ActivityTypes.DeleteUserData)
            {
                // Implement user deletion here
                // If we handle user deletion, return a real message
            }
            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
            }
            else if (message.Type == ActivityTypes.ContactRelationUpdate)
            {
                // Handle add/remove from contact lists
                // Activity.From + Activity.Action represent what happened
            }
            else if (message.Type == ActivityTypes.Typing)
            {
                // Handle knowing tha the user is typing
            }
            else if (message.Type == ActivityTypes.Ping)
            {
            }

            return null;
        }
    }

Expected Behavior

When the user starts conversation by typing “Hi”, the bot should post a reply.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
EricDahlvangcommented, Jan 25, 2018

I see you’re using the connector client to send a reply. Try calling MicrosoftAppCredentials.TrustServiceUrl(activity.ServiceUrl); before connector.Conversations.ReplyToActivityAsync(..)

What error occurs when the appid and password are in the webconfig?

For the ngrok issue, how are you starting ngrok? Does it work from the command line? https://github.com/Microsoft/BotFramework-Emulator/issues/301

0reactions
bibsgcommented, Jan 31, 2018

@EricDahlvang Hi, I tried the above command in ngrok, but still shows an error in the session status. It keeps looping between the messages “reconnecting (x509: certificate signed by unknown authority)”, “reconnecting (invalid character ‘<’ looking for beginning of value)”, and “reconnecting (dial tcp [2600:1f16:59e:b200:21d7:7eb7:fd6f:f0a9]:443: connectex: A socket operation was attempted to an unreachable network.)”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

There was an error sending this message to your bot: HTTP ...
In the code I've shared with you we get the reply to the user from an external service. I'm using Application Insights to...
Read more >
There was an error sending this message to your bot: HTTP ...
The bot code works fine in the emulator but getting error in the portal. So how to resolve this problem? The id's and...
Read more >
There was an error sending this message to your bot
hi, I just published my bot which is remotely hosted on a server and created a web chat channel but I get the...
Read more >
Create a Bot with Bot Framework Composer - GitHub Pages
Prepare to develop a bot; Create a bot; Add a dialog to get the weather ... the user a question, and the second...
Read more >
502 Bad Gateway - HTTP - MDN Web Docs
... (HTTP) 502 Bad Gateway server error response code indicates that ... in networking and a 502 error is usually not something you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found