简体   繁体   中英

Handling Message Attachment on Microsoft Bot Framework

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;

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM