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); }
// Welcome user after Sign in, check if Azure AD Tenant has acces and ask for voucher comment private async Task <DialogTurnResult> LoginAndWelcomeStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken) { // Get the token from the previous step. Note that we could also have gotten the // token directly from the prompt itself. There is an example of this in the next method. var tokenResponse = (TokenResponse)stepContext.Result; if (tokenResponse?.Token != null) { // Pull in the data from the Microsoft Graph. var client = new GraphClient(tokenResponse.Token); var me = await client.GetMeAsync(); // Check if Azure AD Tenant ID matches configured Tenant ID or end Session if (JwtTokenUtil.GetAzureTenantIdFromToken(tokenResponse.Token) != botConfiguration.AllowedTenantId) { await stepContext.Context.SendActivityAsync(MessageFactory.Text("Ich darf nicht mit Fremden sprechen - Sorry!"), cancellationToken); return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken)); } // Instantiate new voucher request and add it to stepContext Values var voucherRequest = new VoucherRequest(); voucherRequest.UserGivenName = me.GivenName; voucherRequest.UserPrincipalName = me.UserPrincipalName; voucherRequest.BandwidthDownKbps = botConfiguration.BandwidthDownKbps; voucherRequest.BandwidthUpKbps = botConfiguration.BandwidthUpKbps; stepContext.Values[constVoucherRequest] = voucherRequest; // Say Hello :-) await stepContext.Context.SendActivityAsync($"Moin { voucherRequest.UserGivenName }! Freut mich, dass ich dir heute bei der Erstellung eines Gäste-WiFi-Voucher helfen darf\U0001F605! Du kannst dich jederzeit mit einer freundlichen Verabschiedung wie \"Ciao\" wieder abmelden."); // Ask for voucher comment var promptOptions = new PromptOptions { Prompt = MessageFactory.Text("Für wen bzw. für was genau benötigst du den Voucher?") }; return(await stepContext.PromptAsync(nameof(TextPrompt), promptOptions, cancellationToken)); } await stepContext.Context.SendActivityAsync(MessageFactory.Text("Die Anmeldung an deiner OdID hat leider nicht funktioniert. Bitte schreib' mir in den nächsten Minuten einfach noch einmal. Vielleicht helfe ich dir dann\U0001F607"), cancellationToken); return(await stepContext.EndDialogAsync(cancellationToken : cancellationToken)); }