コード例 #1
0
        public static NuanceResponse ConvertResponse(List <GoogleReply_Result> googleData, RequestData request)
        {
            NuanceResponse r = new NuanceResponse();

            //First, write the "audio_transfer_info" data.
            r.audio_transfer_info = new NuanceResponseTransferInfo();
            //This is misisng the packages meta. We're gonna hope the Pebble app doesn't care about this.
            //Set the context to the one we were told by the app.
            r.NMAS_PRFX_SESSION_ID = request.cmdDict.application_session_id;
            //Add the one transcription to the transcriptions.
            GoogleReply_Result trans = new GoogleReply_Result();

            if (googleData.Count > 0)
            {
                trans = googleData[0];
            }
            else
            {
                trans = new GoogleReply_Result();
                trans.alternatives = new List <GoogleReply_Alternative>();
            }
            r.transcriptions = new string[trans.alternatives.Count];
            r.confidences    = new int[trans.alternatives.Count];
            for (int i = 0; i < trans.alternatives.Count; i++)
            {
                r.transcriptions[i] = trans.alternatives[i].transcript;
                r.confidences[i]    = 100;//trans.alternatives[i].confidence;
            }
            //Now, add each word in each alternatives.
            r.words = new NuanceResponseWord[trans.alternatives.Count][];
            for (int alt = 0; alt < trans.alternatives.Count; alt++)
            {
                var      thisTrans = trans.alternatives[alt];
                string[] words     = thisTrans.transcript.Split(' ');
                float    conf      = thisTrans.confidence;
                r.words[alt] = new NuanceResponseWord[words.Length];
                for (int word = 0; word < words.Length; word++)
                {
                    var w = new NuanceResponseWord();

                    w.word       = words[word];
                    w.confidence = conf.ToString();

                    r.words[alt][word] = w;
                }
            }

            return(r);
        }
コード例 #2
0
        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));
        }