/// <summary> /// Invoke when a file upload decline consent activity is received from the channel. /// </summary> /// <param name="turnContext">The context object for this turn.</param> /// <param name="fileConsentCardResponse">The declined response object of File Card.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects /// or threads to receive notice of cancellation.</param> /// <returns>A task representing asynchronous operation.</returns> protected override async Task OnTeamsFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { var(fileName, notificationId) = this.teamsFileUpload.ExtractInformation( fileConsentCardResponse.Context); await this.teamsFileUpload.CleanUp( turnContext, fileName, notificationId, cancellationToken); var reply = MessageFactory.Text(this.localizer.GetString("PermissionDeclinedText")); reply.TextFormat = "xml"; await turnContext.SendActivityAsync(reply, cancellationToken); }
protected override async Task OnTeamsFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { JToken context = JObject.FromObject(fileConsentCardResponse.Context); var reply = ((Activity)turnContext.Activity).CreateReply(); reply.TextFormat = "xml"; reply.Text = $"Declined. We won't upload file <b>{context["filename"]}</b>."; await turnContext.SendActivityAsync(reply, cancellationToken); }
protected override async Task OnTeamsFileConsentAcceptAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { try { JToken context = JObject.FromObject(fileConsentCardResponse.Context); string filePath = Path.Combine("Files", context["filename"].ToString()); long fileSize = new FileInfo(filePath).Length; var client = _clientFactory.CreateClient(); using (var fileStream = File.OpenRead(filePath)) { var fileContent = new StreamContent(fileStream); fileContent.Headers.ContentLength = fileSize; fileContent.Headers.ContentRange = new ContentRangeHeaderValue(0, fileSize - 1, fileSize); await client.PutAsync(fileConsentCardResponse.UploadInfo.UploadUrl, fileContent, cancellationToken); } await FileUploadCompletedAsync(turnContext, fileConsentCardResponse, cancellationToken); } catch (Exception e) { await FileUploadFailedAsync(turnContext, e.ToString(), cancellationToken); } }
protected virtual async Task <InvokeResponse> OnTeamsFileConsentAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { switch (fileConsentCardResponse.Action) { case "accept": await OnTeamsFileConsentAcceptAsync(turnContext, fileConsentCardResponse, cancellationToken).ConfigureAwait(false); return(CreateInvokeResponse()); case "decline": await OnTeamsFileConsentDeclineAsync(turnContext, fileConsentCardResponse, cancellationToken).ConfigureAwait(false); return(CreateInvokeResponse()); default: throw new InvokeResponseException(HttpStatusCode.BadRequest, $"{fileConsentCardResponse.Action} is not a supported Action."); } }
protected virtual Task OnTeamsFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { throw new InvokeResponseException(HttpStatusCode.NotImplemented); }
protected override Task OnTeamsFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { Record.Add(MethodBase.GetCurrentMethod().Name); return(Task.CompletedTask); }
/// <summary> /// Handles file consent response asynchronously. /// </summary> /// <param name="turnContext">The turn context.</param> /// <param name="query">The query object of file consent user's response.</param> /// <returns>Task tracking operation.</returns> public override async Task <InvokeResponse> HandleFileConsentResponseAsync(ITurnContext turnContext, FileConsentCardResponse query) { var reply = turnContext.Activity.CreateReply(); reply.TextFormat = "xml"; reply.Text = $"<b>Received user's consent</b> <pre>{JObject.FromObject(query).ToString()}</pre>"; await turnContext.SendActivityAsync(reply).ConfigureAwait(false); JToken context = JObject.FromObject(query.Context); if (query.Action.Equals("accept")) { try { string filePath = "Files\\" + context["filename"]; string fileUploadUrl = query.UploadInfo.UploadUrl; long fileSize = new FileInfo(filePath).Length; using (WebClient client = new WebClient()) { client.Headers.Add("Content-Length", fileSize.ToString()); client.Headers.Add("Content-Range", $"bytes 0-{fileSize - 1}/{fileSize}"); using (Stream fileStream = File.OpenRead(filePath)) using (Stream requestStream = client.OpenWrite(new Uri(fileUploadUrl), "PUT")) { fileStream.CopyTo(requestStream); } } await this.FileUploadCompletedAsync(turnContext, query).ConfigureAwait(false); } catch (Exception e) { await this.FileUploadFailedAsync(turnContext, e.ToString()).ConfigureAwait(false); } } if (query.Action.Equals("decline")) { reply.Text = $"Declined. We won't upload file <b>{context["filename"]}</b>."; await turnContext.SendActivityAsync(reply).ConfigureAwait(false); } return(null); }
public async Task <InvokeResponse> HandleFileConsentAcceptResponse(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken = default) { var consentAcceptResponse = new InvokeResponse { Status = (int)HttpStatusCode.OK }; var responseContext = JsonConvert.DeserializeObject <FileConsentContext>(fileConsentCardResponse.Context?.ToString()); if (responseContext is null) { return(consentAcceptResponse); } var candidate = await _candidateService.GetById(responseContext.CandidateId, cancellationToken); if (candidate is null) { return(consentAcceptResponse); } IMessageActivity reply; try { // Upload the file contents to the upload session we got from the invoke value // See https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession#upload-bytes-to-the-upload-session var bytes = Encoding.UTF8.GetBytes(candidate.Summary); using (var stream = new MemoryStream(bytes)) { using (var content = new StreamContent(stream)) { content.Headers.ContentRange = new ContentRangeHeaderValue(0, bytes.LongLength - 1, bytes.LongLength); content.Headers.ContentType = new MediaTypeHeaderValue(MediaTypeNames.Application.Octet); var client = _clientFactory.CreateClient(); var response = await client.PutAsync(fileConsentCardResponse.UploadInfo.UploadUrl, content, cancellationToken); response.EnsureSuccessStatusCode(); } } if (!string.IsNullOrEmpty(turnContext.Activity.ReplyToId)) { await turnContext.DeleteActivityAsync(turnContext.Activity.ReplyToId, cancellationToken); } // Send the user a link to the uploaded file var fileInfoCard = new FileInfoCard { FileType = fileConsentCardResponse.UploadInfo.FileType, UniqueId = fileConsentCardResponse.UploadInfo.UniqueId }; reply = MessageFactory.Attachment(fileInfoCard.ToAttachment(fileConsentCardResponse.UploadInfo.Name, fileConsentCardResponse.UploadInfo.ContentUrl)); } catch (Exception ex) { reply = MessageFactory.Text($"There was an error uploading the file: {ex.Message}"); } await turnContext.SendActivityAsync(reply, cancellationToken); return(consentAcceptResponse); }
protected override Task <InvokeResponse> OnTeamsFileConsentAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { Record.Add(MethodBase.GetCurrentMethod().Name); return(base.OnTeamsFileConsentAsync(turnContext, fileConsentCardResponse, cancellationToken)); }
protected override Task <InvokeResponse> OnFileConsentAcceptAsync( ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) => _invokeActivityHandler.HandleFileConsentAcceptResponse(turnContext, fileConsentCardResponse, cancellationToken);
public async Task <InvokeResponse> HandleFileConsentDeclineResponse(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken = default) { if (!string.IsNullOrEmpty(turnContext.Activity.ReplyToId)) { await turnContext.DeleteActivityAsync(turnContext.Activity.ReplyToId, cancellationToken); } return(new InvokeResponse { Status = (int)HttpStatusCode.OK }); }
protected override Task OnTeamsFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { return(Task.CompletedTask); }
protected virtual Task <InvokeResponse> OnFileConsent(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { switch (fileConsentCardResponse.Action) { case "accept": return(OnFileConsentAcceptAsync(turnContext, fileConsentCardResponse, cancellationToken)); case "decline": return(OnFileConsentDeclineAsync(turnContext, fileConsentCardResponse, cancellationToken)); default: return(Task.FromResult <InvokeResponse>(null)); } }
protected virtual Task <InvokeResponse> OnFileConsentDeclineAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { return(Task.FromResult <InvokeResponse>(null)); }
protected virtual Task OnTeamsFileConsentAcceptAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { throw new NotImplementedException(); }
protected virtual async Task <InvokeResponse> OnTeamsFileConsentAsync(ITurnContext <IInvokeActivity> turnContext, FileConsentCardResponse fileConsentCardResponse, CancellationToken cancellationToken) { switch (fileConsentCardResponse.Action) { case "accept": await OnTeamsFileConsentAcceptAsync(turnContext, fileConsentCardResponse, cancellationToken).ConfigureAwait(false); return(CreateInvokeResponse()); case "decline": await OnTeamsFileConsentDeclineAsync(turnContext, fileConsentCardResponse, cancellationToken).ConfigureAwait(false); return(CreateInvokeResponse()); default: throw new NotImplementedException(); } }