1

im trying to handle messages that have attachments, im getting a The remote server returned an error: (400) Bad Request. error. How can i handle this the right way?

MessageController

        if (activity.Type == ActivityTypes.Message)
        {
            try
            {
                ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
                // Check in QnA Dialog
                await Conversation.SendAsync(activity, () => new QnADialog());
            }
            catch (Exception ex)
            {

                throw;
            }

        }
        else
        {
            HandleSystemMessage(activity);
        }
        var response = Request.CreateResponse(HttpStatusCode.OK);
        return response;
5
  • What is the link between your 1st sentence and the code extract? Can you add more details about your implementation, where you got the problem. Here I can't see any Attachment specificity Commented Oct 25, 2017 at 8:55
  • I'm trying to send a message with an attachment (.txt, .jpg etc). I want my code to handle gracefully a message with an attachment. Commented Oct 25, 2017 at 9:00
  • And? Have you tried anything? Sending an attachment is easy, but here your code sample seems to be linked to QnaMaker, why? And you are not showing the part of code creating a message (where you would add your attachment) Commented Oct 25, 2017 at 9:02
  • im trying a simple chatbot project that connects to a qnamaker, my problem is i accidentally found an error when i tried sending a file on the bot emulator. the error says (400) Bad Request. on the catch phrase Commented Oct 25, 2017 at 9:09
  • Ok, now I understand. So your problem is that you have a QnA bot and it's not properly managing the fact of receiving messages with attachments. I will answer to handle this, give me 5 minutes Commented Oct 25, 2017 at 9:11

1 Answer 1

2

You can directly check the attachments count on the activity object and do the following for example:

if (activity.Type == ActivityTypes.Message)
{
    try
    {
        if (activity.Attachments.Count > 0)
        {
            var replyNoAttachmentAllowed = activity.CreateReply("This QnA bot cannot handle attachments, please send only text");
            await context.PostAsync(replyNoAttachmentAllowed);
        }
        else
        {
            // Check in QnA Dialog
            await Conversation.SendAsync(activity, () => new QnADialog());
        }
    }
    catch (Exception ex)
    {

        throw;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.