/// <summary> /// Get the cards and pack info from cardcast /// </summary> /// <param name="packCode"></param> /// <param name="questions"></param> /// <param name="answers"></param> public static bool getPackCards(ref string packCode, out cardcast_pack packData, ref List<cardcast_question_card> questions, ref List<cardcast_answer_card> answers) { packData = new cardcast_pack(); //some basic checks on packCode. packData.packCode = packCode.ToUpper(); Regex r = new Regex("^[A-Z0-9]*$"); if (!r.IsMatch(packCode)) { Roboto.log.log("Pack failed regex match and was dropped - " + packCode, logging.loglevel.high); return false; } else { try { //call the pack info API JObject packInfoResponse = sendPOST(packCode); //get the various bits we need. if (packInfoResponse == null) { Roboto.log.log("Null response from cardcast API - " + packCode, logging.loglevel.high); return false; } else { try { //get the message details packData.name = packInfoResponse.SelectToken("name").Value<string>(); packData.description = packInfoResponse.SelectToken("description").Value<string>(); } catch (Exception e) { Roboto.log.log("Error parsing message - " + e.ToString(), logging.loglevel.high); return false; } } } catch (System.Net.WebException e) { Roboto.log.log("Exception getting pack info " + e.ToString(), logging.loglevel.high); return false; } //now get the cards JObject packCardsResponse = sendPOST(packCode + "/cards"); if (packCardsResponse == null) { Roboto.log.log("Null response from cardcast Cards API - " + packCode, logging.loglevel.high); return false; } else { try { //get the questions foreach (JToken token in packCardsResponse.SelectTokens("calls[*]")) { cardcast_question_card c = new cardcast_question_card(); string text = ""; //text is broken up into chunks, reassemble. bool first = true; int nrAnswers = -1; foreach (JToken textPartToken in token.SelectTokens("text[*]")) { text += (first?"":"__") + textPartToken.Value<string>(); first = false; nrAnswers++; } c.question = text; c.nrAnswers = nrAnswers; questions.Add(c); } //get the answers foreach (JToken token in packCardsResponse.SelectTokens("responses[*]")) { cardcast_answer_card c = new cardcast_answer_card(); string text = ""; //text is broken up into chunks, reassemble. bool first = true; foreach (JToken textPartToken in token.SelectTokens("text[*]")) { text += (first ? "" : "__") + textPartToken.Value<string>(); first = false; } c.answer = text; answers.Add(c); } } catch (Exception e) { Roboto.log.log("Error parsing message - " + e.ToString(), logging.loglevel.high); return false; } } return true; } }
/// <summary> /// Get the cards and pack info from cardcast /// </summary> /// <param name="packCode"></param> /// <param name="questions"></param> /// <param name="answers"></param> public static bool getPackCards(ref string packCode, out cardcast_pack packData, ref List <cardcast_question_card> questions, ref List <cardcast_answer_card> answers) { packData = new cardcast_pack(); //some basic checks on packCode. packData.packCode = packCode.ToUpper(); Regex r = new Regex("^[A-Z0-9]*$"); if (!r.IsMatch(packCode)) { Roboto.log.log("Pack failed regex match and was dropped - " + packCode, logging.loglevel.high); return(false); } else { try { //call the pack info API JObject packInfoResponse = sendPOST(packCode); //get the various bits we need. if (packInfoResponse == null) { Roboto.log.log("Null response from cardcast API - " + packCode, logging.loglevel.high); return(false); } else { try { //get the message details packData.name = packInfoResponse.SelectToken("name").Value <string>(); packData.description = packInfoResponse.SelectToken("description").Value <string>(); } catch (Exception e) { Roboto.log.log("Error parsing message - " + e.ToString(), logging.loglevel.high); return(false); } } } catch (System.Net.WebException e) { Roboto.log.log("Exception getting pack info " + e.ToString(), logging.loglevel.high); return(false); } //now get the cards JObject packCardsResponse = sendPOST(packCode + "/cards"); if (packCardsResponse == null) { Roboto.log.log("Null response from cardcast Cards API - " + packCode, logging.loglevel.high); return(false); } else { try { //get the questions foreach (JToken token in packCardsResponse.SelectTokens("calls[*]")) { cardcast_question_card c = new cardcast_question_card(); string text = ""; //text is broken up into chunks, reassemble. bool first = true; int nrAnswers = -1; foreach (JToken textPartToken in token.SelectTokens("text[*]")) { text += (first?"":"__") + textPartToken.Value <string>(); first = false; nrAnswers++; } c.question = text; c.nrAnswers = nrAnswers; questions.Add(c); } //get the answers foreach (JToken token in packCardsResponse.SelectTokens("responses[*]")) { cardcast_answer_card c = new cardcast_answer_card(); string text = ""; //text is broken up into chunks, reassemble. bool first = true; foreach (JToken textPartToken in token.SelectTokens("text[*]")) { text += (first ? "" : "__") + textPartToken.Value <string>(); first = false; } c.answer = text; answers.Add(c); } } catch (Exception e) { Roboto.log.log("Error parsing message - " + e.ToString(), logging.loglevel.high); return(false); } } return(true); } }