public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Try and authenticate the user. VoiceAccount va = null; if (e.Request.Query.ContainsKey("key")) { //This is the voice token key VoiceToken vc = Program.AuthenticateVoiceToken(e.Request.Query["key"]); if (vc == null) { throw new Exception("RPWS voice token not valid."); } va = RpwsQuota.RpwsVoiceAuth.GetAccount(vc.rpws_uid); } if (e.Request.Query.ContainsKey("token")) { //This is the RPWS token RpwsMe me = await RpwsAuth.RpwsRequests.AuthenticateUser(e.Request.Query["token"]); if (me == null) { throw new Exception("RPWS token is not valid."); } va = RpwsQuota.RpwsVoiceAuth.GetAccount(me.uuid); //That might've failed. If it did, create a "fake" account if (va == null) { va = new VoiceAccount { b_has_sent_welcome_email = false, credits = Program.config.defaultCredits, last_token_awarded_time = DateTime.UtcNow.Ticks, total_failed_requests = 0, total_ok_requests = 0, _id = me.uuid }; } } //If it failed, stop if (va == null) { throw new Exception("Not authenticated!"); } //Award new tokens for an updated result RpwsQuota.RpwsVoiceAuth.AwardNewCredits(va); //Write await Program.QuickWriteJsonToDoc(e, va); }
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Grab the token from the URL if (!e.Request.Query.ContainsKey("token")) { throw new Exception("Missing token!"); } string rpws_token = e.Request.Query["token"]; //Look the token up in the database and see if we already have it. var collec = Program.GetVoiceTokensCollection(); var tokens = collec.Find(x => x.rpws_token == rpws_token).ToArray(); //If we already have a token for this, return the token if (tokens.Length == 1) { await Program.QuickWriteToDoc(e, tokens[0]._id, "text/plain"); return; } //Generate a new token. Request RPWS data RpwsMe me = await RpwsAuth.RpwsRequests.AuthenticateUser(rpws_token); if (me == null) { throw new Exception("Failed to authenticate with RPWS."); } //Create voice token string voiceToken = GenerateVoiceToken(collec); VoiceToken vt = new VoiceToken { last_cache_refresh_time = DateTime.UtcNow.Ticks, rpws_email = me.email, rpws_name = me.name, rpws_token = rpws_token, rpws_uid = me.uuid, _id = voiceToken }; //Insert collec.Insert(vt); //Log Console.WriteLine($"Generated voice token for RPWS user {me.uuid} ({me.email}), {voiceToken}"); //Write the token await Program.QuickWriteToDoc(e, voiceToken, "text/plain"); }
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Authenticate this user using the hostname used. We must use the hostname because that's the most I can change from the Pebble configuration. UrlParams urlParams = RpwsAuth.DecodeUrlParams.DecodeUrl(e.Request.Host.Host); //Check the voice token against our database VoiceToken me = Program.AuthenticateVoiceToken(urlParams.accessToken); if (me == null) { throw new Exception("Invalid access token!"); } //Grab their user account VoiceAccount account = RpwsQuota.RpwsVoiceAuth.GetAccount(me.rpws_uid); //Award new tokens RpwsQuota.RpwsVoiceAuth.AwardNewCredits(account); //Send the welcome email if we haven't /*if(!account.b_has_sent_welcome_email) * RpwsAuth.EmailService.SendWelcomeEmail(me); * account.b_has_sent_welcome_email = true;*/ //Save account and log Console.WriteLine($"Voice request by {account._id}, credits remaining: {account.credits}"); RpwsQuota.RpwsVoiceAuth.SaveAccount(account); //Stop request if needed if (account.credits < 1) { throw new Exception("Quota reached."); } //Pass this into the HTTPDecoder Tuple <RequestData, List <Stream> > payload = VoiceService.HttpDecoder.DecodeHttpData(e).Result; RequestData requestConfig = payload.Item1; //Now, convert this to base64 request string audioData = VoiceService.SpeexWithHeaderByteConverter.CreateBase64Payload(payload.Item2); //Now, form a Google request and send it. GoogleReply textData = await VoiceService.GoogleRequester.DoRequest(audioData, urlParams.languageRegion, Program.config.googleApiKey); //Now, convert to a fake Nuance response List <GoogleReply_Result> results = new List <GoogleReply_Result>(); bool ok = true; if (textData != null) { results = textData.results; ok = false; } else { ok = textData.results.Count != 0; } //Save account.credits -= 0.5f; if (ok) { account.credits -= 0.5f; } RpwsQuota.RpwsVoiceAuth.SaveAccount(account); //Now, form a reply that the Pebble will be happy with. NuanceResponse reply = VoiceService.FakeNuanceResponse.ConvertResponse(results, requestConfig); //Write this out await VoiceService.NuanceResponder.RespondWithData(e, JsonConvert.SerializeObject(reply)); }
public static void SendWelcomeEmail(VoiceToken vt) { SendEmail(vt.rpws_name, vt.rpws_email, "Welcome to RPWS Voice!", $"Hi there!\n\nYou just used the new RPWS voice feature for the first time. RPWS uses Google speech to text, a paid voice dictation service, to do this. To keep RPWS free, you'll have a limited number of dictations available.\n\nYou were given {Program.config.defaultCredits} credits to spend on voice dictation. You'll be given {Program.config.dailyCreditsGiven} credits each day. If you run out of credits, you won't be able to use dictation until the next day. This daily quota is not final and may be changed in the future. Currently, there are no plans to add a paid option.\n\nThanks for using RPWS!"); }