bugfix> c# > 投稿
public virtual async Task MotgaAtt(IDialogContext context, IAwaitable<IMessageActivity> argument)
        {
            var message = await argument;
            if (message.Attachments != null && message.Attachments.Any())
            {
                var attachment = message.Attachments.First();
                using (HttpClient httpClient = new HttpClient())
                {
                    var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
                    var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
                    await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
                }
            }
            context.Done<object>(new object());
        }

ボットユーザーがアップロードした添付ファイルを取得して、データベースに挿入できるようにします。作成した添付変数を使用しようとしましたが、ユーザーがアップロードした正確なファイルではないようです。アップロードしたファイルを取得するにはどうすればよいですか?ありがとう

回答 1 件
  • 公式サンプルをご覧ください:https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-ReceiveAttachment

    チャネルに応じて、特定のトークンを指定する必要があります。

    public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
    {
        var message = await argument;
        if (message.Attachments != null && message.Attachments.Any())
        {
            var attachment = message.Attachments.First();
            using (HttpClient httpClient = new HttpClient())
            {
                // Skype & MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
                if ((message.ChannelId.Equals("skype", StringComparison.InvariantCultureIgnoreCase) || message.ChannelId.Equals("msteams", StringComparison.InvariantCultureIgnoreCase)) 
                    && new Uri(attachment.ContentUrl).Host.EndsWith("skype.com"))
                {
                    var token = await new MicrosoftAppCredentials().GetTokenAsync();
                    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                }
                var responseMessage = await httpClient.GetAsync(attachment.ContentUrl);
                var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
                await context.PostAsync($"Attachment of {attachment.ContentType} type and size of {contentLenghtBytes} bytes received.");
            }
        }
        else
        {
            await context.PostAsync("Hi there! I'm a bot created to show you how I can receive message attachments, but no attachment was sent to me. Please, try again sending a new message including an attachment.");
        }
        context.Wait(this.MessageReceivedAsync);
    }
    
    

あなたの答え