public static bool IsValidUserAccount(bool showErrorMessage = true, string errorMessage = "Failed Azure authentication.")
 {
     try
     {
         string _key = WatsonAccount.GetInstance().GetKey();
         if (_key == null || string.IsNullOrEmpty(_key.Trim()))
         {
             throw new Exception("Empty key value");
         }
         TokenOptions options = new TokenOptions();
         options.IamApiKey = _key;
         string accessToken = new TokenManager(options).GetToken();
         if (string.IsNullOrEmpty(accessToken))
         {
             throw new Exception("Invalid access key!");
         }
         if (!IsValidEndpoint(_key, WatsonAccount.GetInstance().GetEndpoint()))
         {
             throw new Exception("Invalid endpoint!");
         }
         Console.WriteLine("Token: {0}\n", accessToken);
     }
     catch
     {
         Console.WriteLine(errorMessage);
         if (showErrorMessage)
         {
             MessageBox.Show(errorMessage);
         }
         return(false);
     }
     return(true);
 }
 public static WatsonAccount GetInstance()
 {
     if (instance == null)
     {
         instance = new WatsonAccount();
     }
     return(instance);
 }
 private void LogOutWatsonAccountButton_Click(object sender, RoutedEventArgs e)
 {
     WatsonAccount.GetInstance().Clear();
     WatsonAccountStorageService.DeleteUserAccount();
     watsonVoiceComboBox.Visibility = Visibility.Collapsed;
     watsonVoiceLoginBtn.Visibility = Visibility.Visible;
     changeWatsonAcctBtn.Visibility = Visibility.Hidden;
     logoutWatsonAcctBtn.Visibility = Visibility.Hidden;
     RadioWatsonVoice.IsEnabled     = false;
     RadioDefaultVoice.IsChecked    = true;
     WatsonRuntimeService.IsWatsonAccountPresentAndValid = false;
 }
Example #4
0
        public static void SaveUserAccount(WatsonAccount account)
        {
            if (!Directory.Exists(GetELearningLabStoragePath()))
            {
                Directory.CreateDirectory(GetELearningLabStoragePath());
            }

            Dictionary <string, string> user = new Dictionary <string, string>()
            {
                { "endpoint", account.GetRegion() },
                { "key", account.GetKey() }
            };
            XElement   root = new XElement("user", from kv in user select new XElement(kv.Key, kv.Value));
            FileStream file = File.Open(GetAccessKeyFilePath(), FileMode.OpenOrCreate);

            root.Save(file);
            file.Close();
        }
        public static void SaveStringToWaveFile(string text, string filePath, WatsonVoice voice)
        {
            if (!IsWatsonAccountPresentAndValid)
            {
                return;
            }
            string _key           = WatsonAccount.GetInstance().GetKey();
            string _endpoint      = EndpointToUriConverter.watsonRegionToEndpointMapping[WatsonAccount.GetInstance().GetRegion()];
            Text   synthesizeText = new Text {
                _Text = text
            };
            TokenOptions options = new TokenOptions();

            options.IamApiKey = _key;
            string endpoint = _endpoint;
            var    _service = new SynthesizeWatsonVoice(options, endpoint);

            var synthesizeResult = _service.Synthesize(synthesizeText, "audio/wav", voice: "en-US_" + voice.VoiceName);

            StreamUtility.SaveStreamToFile(filePath, synthesizeResult);
        }
Example #6
0
        private void ConfirmButton_Click(object sender, RoutedEventArgs e)
        {
            string _endpoint = "";
            string _key      = "";

            try
            {
                _key = key.Text.Trim();
                string region = ((ComboBoxItem)endpoint.SelectedItem).Content.ToString().Trim();
                _endpoint = EndpointToUriConverter.watsonRegionToEndpointMapping[region];
            }
            catch
            {
                MessageBox.Show("Key or Region cannot be empty!", "Invalid Input");
                return;
            }

            bool isValidAccount = WatsonRuntimeService.IsValidUserAccount(_key, _endpoint,
                                                                          "Invalid Watson Account.\nIs your Watson account expired?\nAre you connected to Wifi?");

            if (isValidAccount)
            {
                // Delete previous user account
                WatsonAccount.GetInstance().Clear();
                WatsonAccountStorageService.DeleteUserAccount();
                // Create and save new user account
                string _region = ((ComboBoxItem)endpoint.SelectedItem).Content.ToString().Trim();
                WatsonAccount.GetInstance().SetUserKeyAndRegion(_key, _region);
                WatsonAccountStorageService.SaveUserAccount(WatsonAccount.GetInstance());
                WatsonRuntimeService.IsWatsonAccountPresentAndValid = true;
                SwitchViewToPreviousPage();
            }
            else
            {
                MessageBox.Show("Invalid Watson Account.\nIs your Watson account expired?\nAre you connected to Wifi?");
            }
        }
Example #7
0
        public static void LoadUserAccount()
        {
            if (!WatsonAccount.GetInstance().IsEmpty())
            {
                return;
            }
            Dictionary <string, string> user = new Dictionary <string, string>();

            try
            {
                FileStream file = File.Open(GetAccessKeyFilePath(), FileMode.Open);
                XElement   root = XElement.Load(file);
                foreach (XElement el in root.Elements())
                {
                    user.Add(el.Name.LocalName, el.Value);
                }
                string key      = user.ContainsKey("key") ? user["key"] : null;
                string endpoint = user.ContainsKey("endpoint") ? user["endpoint"] : null;
                if (key != null && endpoint != null)
                {
                    WatsonAccount.GetInstance().SetUserKeyAndRegion(key, endpoint);
                    WatsonRuntimeService.IsWatsonAccountPresentAndValid =
                        WatsonRuntimeService.IsValidUserAccount(errorMessage: "Invalid Watson Account." +
                                                                "\nIs your Watson account expired?\nAre you connected to Wifi?");
                }
                else
                {
                    WatsonRuntimeService.IsWatsonAccountPresentAndValid = false;
                    File.Delete(GetAccessKeyFilePath());
                }
            }
            catch (Exception e)
            {
                WatsonRuntimeService.IsWatsonAccountPresentAndValid = false;
                Logger.Log(e.Message);
            }
        }
 public static bool IsWatsonAccountPresent()
 {
     return(!WatsonAccount.GetInstance().IsEmpty());
 }