public async Task <VoucherResponse> CreateGuestVoucher(VoucherRequest request) { VoucherResponse response = new VoucherResponse(); String vocherNote = "Req. by " + request.UserPrincipalName + " for purpose " + request.NewVoucherComment; Int32 voucherValidMinutes = request.NewVoucherValidHours * 60; //Authenticate against API await _uniFiApi.Authenticate(); //Create Voucher UniFiSharp.Json.JsonHotspotVoucher createdVoucher = await _uniFiApi.HotspotVoucherAdd(null, request.BandwidthDownKbps, request.BandwidthUpKbps, voucherValidMinutes.ToString(), 1, vocherNote, 1); UniFiSharp.Json.JsonHotspotVoucher voucher = (await _uniFiApi.HotspotVoucherGet(createdVoucher.createTime)).First(); // Verify if voucher was created for current user - as unifi responds with create time as identifier only and pull in data from voucher to response object if (voucher.note.Contains(request.UserPrincipalName) && voucher.createTime == createdVoucher.createTime) { response.VoucherCode = voucher.code.Substring(0, 5) + "-" + voucher.code.Substring(5, 5); response.VoucherComment = voucher.note; // Calculate validity based on Unix Timestamp + create Date + Duration in Minutes response.VoucherValidUntil = new System.DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(voucher.createTime).AddMinutes(voucher.duration); response.BandwidthDownKbps = (Int32)voucher.qosRateMaxDown; response.BandwidthUpKbps = (Int32)voucher.qosRateMaxUp; response.UserGivenName = request.UserGivenName; } else { throw new Exception("Voucher Code received does not match created voucher code"); } return(response); }
private async Task <DialogTurnResult> GenerateAndResponseVoucherStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { // Initialize Context Data var validityResponse = (FoundChoice)stepContext.Result; var voucherRequest = (VoucherRequest)stepContext.Values[constVoucherRequest]; // Confirm message received await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Perfekt, ich kümmere mich jetzt um die Erstellung eines Vouchers für {voucherRequest.NewVoucherComment} und eine Dauer von {validityResponse.Value}. Bitte habe einen kleinen Moment Geduld, solange ich in meiner Voucher-Kiste krame."), cancellationToken); // Calculate Voucher Validity duration switch (validityResponse.Value) { case "4 Stunden": voucherRequest.NewVoucherValidHours = 4; break; case "12 Stunden": voucherRequest.NewVoucherValidHours = 12; break; case "2 Tage": voucherRequest.NewVoucherValidHours = 48; break; case "180 Tage": voucherRequest.NewVoucherValidHours = 4320; break; } // Create Voucher UnifiClient unifiClient = new UnifiClient(botConfiguration.UnifiAPIUsername, botConfiguration.UnifiAPIPassword, botConfiguration.UnifiAPIUri); VoucherResponse voucherResponse = await unifiClient.CreateGuestVoucher(voucherRequest); voucherResponse.HotspotSSID = botConfiguration.HotspotSSID; // Create Adaptive Hotspot Voucher Card var cardAttachment = AdaptiveCardHandler.CreateHotspotVoucherAdaptiveCard(voucherResponse); var reply = MessageFactory.Attachment(cardAttachment); reply.Text = "So, und hier ist schon dein Voucher. Viel Spaß!"; // Send Voucher Code to user await stepContext.Context.SendActivityAsync(reply, cancellationToken); // End Dialog return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken)); }
public static Attachment CreateHotspotVoucherAdaptiveCard(VoucherResponse response) { AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0)); // Define Adaptive Card Container List <AdaptiveElement> adaptiveElements = new List <AdaptiveElement> { new AdaptiveColumnSet() { Columns = new List <AdaptiveColumn>() { new AdaptiveColumn() { Width = AdaptiveColumnWidth.Auto, Items = new List <AdaptiveElement>() { new AdaptiveImage() { Style = AdaptiveImageStyle.Person, Url = new Uri("https://img.favpng.com/1/24/3/wi-fi-wireless-network-icon-clip-art-png-favpng-QhKK6rDubd8RMqLN2zZB4mt83.jpg"), Size = AdaptiveImageSize.Large } } }, new AdaptiveColumn() { Width = AdaptiveColumnWidth.Stretch, Items = new List <AdaptiveElement>() { new AdaptiveTextBlock() { Spacing = AdaptiveSpacing.None, Text = $"Voucher Code: {response.VoucherCode}", Wrap = true, Weight = AdaptiveTextWeight.Bolder }, new AdaptiveTextBlock() { Spacing = AdaptiveSpacing.None, Text = $"Netzwerk-Bezeichnung: {response.HotspotSSID}", Wrap = true, IsSubtle = true }, new AdaptiveTextBlock() { Spacing = AdaptiveSpacing.None, Text = $"Geschwindigkeit: {(response.BandwidthDownKbps/1024).ToString()}Mbit Down / {(response.BandwidthUpKbps/1024).ToString()}Mbit Up", Wrap = true, IsSubtle = true }, new AdaptiveTextBlock() { Spacing = AdaptiveSpacing.None, Text = $"Gültig bis: {response.VoucherValidUntil.ToString("dd.MM.yyyy HH:mm")}", Wrap = true, IsSubtle = true } } } } } }; AdaptiveContainer adaptiveCardContainer = new AdaptiveContainer(); adaptiveCardContainer.Items = adaptiveElements; // Define Adaptive Card Heading AdaptiveTextBlock headerTextBlock = new AdaptiveTextBlock() { Text = $"WiFi Voucher für {response.UserGivenName}", Size = AdaptiveTextSize.Medium, Weight = AdaptiveTextWeight.Bolder }; // Put adaptive card together card.Body.Add(headerTextBlock); card.Body.Add(adaptiveCardContainer); // Create return value Attachment attachment = new Attachment() { ContentType = AdaptiveCard.ContentType, Content = card }; return(attachment); }