Can a Bot receive image as message or attachment from a user

让人想犯罪 __ 提交于 2019-12-13 11:20:08

问题


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


来源:https://stackoverflow.com/questions/50488713/can-a-bot-receive-image-as-message-or-attachment-from-a-user

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!