Can a Bot receive image as message or attachment f

2019-09-26 10:01发布

问题:

I want a user to be able to send image as message to Bot. is this possible?. I've searched online for solutions and I'm tired. please can someone share me a link at least?.

回答1:

Yes

From the nodejs documentation here

// Create your bot with a function to receive messages from the user

var bot = new builder.UniversalBot(connector, function (session) {
    var msg = session.message;
    if (msg.attachments && msg.attachments.length > 0) {
     // Echo back attachment
     var attachment = msg.attachments[0];
        session.send({
            text: "You sent:",
            attachments: [
                {
                    contentType: attachment.contentType,
                    contentUrl: attachment.contentUrl,
                    name: attachment.name
                }
            ]
        });
    } else {
        // Echo back users text
        session.send("You said: %s", session.message.text);
    }
});

c# documentation is here



回答2:

The answer is yes you can providing the channel you are using allows for attachments. Channels have limits on things like size and file types so this will vary depending on what channel you are using. So if you cannot get a PDF to work try with an image. If an image doesn't work, try with a smaller image.

Users will upload a file via the channel interface like this in the emulator:

There is no special code needed to receive images in your bot. Images will be present in the Activity as Activity.Attachments This is a List of the attachments, or in your case images. This could have easily been inferred from Rajesh's answer, but for completeness here is an example of doing something with a file received:

In RootDialog.cs

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Net;
using System.Threading.Tasks;

namespace Bot_Application15.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

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

            foreach (var file in activity.Attachments)
            {
                //where the file is hosted
                var remoteFileUrl = file.ContentUrl;
                //where we are saving the file
                var localFileName = @"C:\Users\{UserName}\pictures\test" + file.Name;

                using ( var webClient = new WebClient())
                {
                    webClient.DownloadFile(remoteFileUrl, localFileName);
                }
            }
            await context.PostAsync($"File received");

            context.Wait(MessageReceivedAsync);
        }
    }
}


回答3:

Yes , it is possible in your MessageRecivedAsync Method of Bot Dialogue .

    private async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> result)
    {
        var message = await result;
        if (message.Attachments != null && message.Attachments.Any())
        {
            // Do something with the attachment
        }
        else
        {
            await context.PostAsync("Please upload a picture");
            context.Wait(this.MessageReceived);
        }
    }