public static SkillResult GetWeather(SkillParams skillParams) { string location = skillParams["location"]; string engString = string.Empty; string url = "https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{{location}}')&format=json"; url = url.Replace("{{location}}", location); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "GET"; HttpWebResponse webResp = (HttpWebResponse)req.GetResponse(); StreamReader resp = new StreamReader(webResp.GetResponseStream()); string rawJson = resp.ReadToEnd(); dynamic js = JsonConvert.DeserializeObject <dynamic>(rawJson); if (js.query.results != null) { string temp = js.query.results.channel.item.condition.temp; string unit = js.query.results.channel.units.temperature; string condition = js.query.results.channel.item.condition.text; engString = temp + " degrees " + (unit == "F" ? "Fahrenheit" : "Celsius") + " and the condition is " + condition; } else { engString = " unknown."; } return(new SkillResult(engString)); }
/// <summary> /// Process any skills that the response from the engine might have invoked. /// </summary> /// <param name="responseStatement"></param> /// <returns></returns> public string ProcessSkills(string responseStatement) { if (responseStatement.Contains(SkillsRepository.SkillRequestToken)) { int start = responseStatement.IndexOf("{{"); int end = responseStatement.IndexOf("}}") - start + 2; string rawSkill = responseStatement.Substring(start, end); if (SkillParams.ValidateRawSkillString(rawSkill)) { SkillResult result = SkillsRepository.Instance.ExecuteSkill(new SkillParams(rawSkill)); if (result.Type == ResultType.Simple) { responseStatement = responseStatement.Replace(rawSkill, result.SimpleResult); } else if (result.Type == ResultType.KeyValuePairs) { // Response is in form of key value pairs. // 1. Remove skill invocation raw skill string responseStatement = responseStatement.Replace(rawSkill, string.Empty); // 2. Replace response key placeholders with values foreach (KeyValuePair <string, string> kv in result.KeyValues) { responseStatement = responseStatement.Replace("{{key:" + kv.Key + "}}", kv.Value); } } } } return(responseStatement); }
/// <summary> /// Entry point for executing the method that invokes the third party script /// </summary> /// <param name="skillParams"></param> /// <returns></returns> public static SkillResult RunScriptedSkill(SkillParams skillParams) { var scriptsPath = ConfigurationManager.AppSettings["scriptsPath"]; if (ValidateParams(skillParams) && scriptsPath != null) { var processStartInfo = new ProcessStartInfo { FileName = skillParams["interpreter"], Arguments = string.Concat(scriptsPath, skillParams["scriptname"], " ", skillParams["args"]), RedirectStandardOutput = true, UseShellExecute = false }; var process = Process.Start(processStartInfo); var output = process.StandardOutput.ReadToEnd(); process.WaitForExit(); ScriptedSkillResponse js = JsonConvert.DeserializeObject <ScriptedSkillResponse>(output); // Build dictionary from key values in response Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (KeyValue kv in js.keyvalues) { dict[kv.key] = kv.value; } SkillResult result = new SkillResult(dict); return(result); } return(new SkillResult("")); }
/// <summary> /// Execute the skill code to perform skill action. /// </summary> /// <param name="skillParams"></param> /// <returns>Result from the executed skill</returns> public SkillResult ExecuteSkill(SkillParams skillParams) { SkillResult result = null; if (this.SkillsIndex.ContainsKey(skillParams.SkillName)) { result = this.SkillsIndex[skillParams.SkillName].Invoke(skillParams); } return(result != null ? result : new SkillResult(string.Empty)); }
/// <summary> /// Validate parameters if they qualify for scripted skills invocation. /// </summary> /// <param name="skillParams"></param> /// <returns></returns> public static bool ValidateParams(SkillParams skillParams) { bool retVal = false; if (skillParams.ContainsKey("interpreter") && skillParams.ContainsKey("scriptname")) { retVal = true; } return(retVal); }
/// <summary> /// Process any skills that the response from the engine might have invoked. /// </summary> /// <param name="responseStatement"></param> /// <returns></returns> public string ProcessSkills(string responseStatement) { if (responseStatement.Contains(SkillsRepository.SkillRequestToken)) { int start = responseStatement.IndexOf("{{"); int end = responseStatement.IndexOf("}}") - start + 2; string rawSkill = responseStatement.Substring(start, end); if (SkillParams.ValidateRawSkillString(rawSkill)) { SkillResult result = SkillsRepository.Instance.ExecuteSkill(new SkillParams(rawSkill)); responseStatement = responseStatement.Replace(rawSkill, result.SimpleResult); } } return(responseStatement); }
public static SkillResult GetTime(SkillParams skillParams) { string time = DateTime.Now.ToString("hh:mm tt"); return(new SkillResult(time, time)); }