1
votes

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;
1
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 - Nicolas R
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. - anonymous1110
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) - Nicolas R
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 - anonymous1110
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 - Nicolas R

1 Answers

2
votes

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;
    }

}