Exemple #1
0
 public Services(SmsMessage smsMessage)
 {
     if (smsMessage != null)
     {
         CloudTable table = cloudTable("games");
         table.CreateIfNotExists();
         TableQuery<Game> query = new TableQuery<Game>()
             .Where(TableQuery.CombineFilters(
                 TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, smsMessage.From),
                 "and",
                 TableQuery.GenerateFilterCondition("Status", QueryComparisons.Equal, "Live"))
             );
         IEnumerable<Game> games = table.ExecuteQuery(query);
         if (games.Count() > 0)
         {
             Game = games.First();
             table = cloudTable("characters");
             TableQuery<Character> characterQuery = new TableQuery<Character>()
                         .Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, Game.Character));
             IEnumerable<Character> characters = table.ExecuteQuery(characterQuery);
             if (characters.Count() > 0)
                 Character = characters.First();
         }
     }
 }
 public IHttpActionResult ReceiveSMS(SmsMessage smsMessage)
 {
     Services services = new Services(smsMessage);
         services.SaveMessage(smsMessage);
         var twilio = new TwilioRestClient(ConfigurationManager.AppSettings["TwilioAccountKey"],
             ConfigurationManager.AppSettings["TwilioAuthToken"]);
         if (smsMessage.Body.ToLower().StartsWith("play"))
         {
            services.CreateGame(smsMessage);
             twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                 smsMessage.From,
                 smsMessage.Body.Substring(smsMessage.Body.ToLower().IndexOf("play") + 4) +
                 ", I'm ready for your first question!");
         }
         else
         {
             if (services.Game == null)
             {
                 twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Sorry, I can't find your game.");
                 return Ok();
             }
             if (services.Character == null)
             {
                 twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Sorry, I can't find the character your game started with.");
                 return Ok();
             }
             if (smsMessage.Body.ToLower().StartsWith("male"))
             {
                 Respond(services.gender(Gender.male), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("female"))
             {
                 Respond(services.gender(Gender.female), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("hair"))
             {
                 if (services.hair(smsMessage.Body.Substring(5)))
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Yes.");
                 }
                 else
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "No.");
                 }
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("eyes"))
             {
                 if (services.eyes(smsMessage.Body.Substring(5)))
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Yes.");
                 }
                 else
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "No.");
                 }
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("hat"))
             {
                 Respond(services.hat(), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("beard"))
             {
                 Respond(services.beard(), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("glasses"))
             {
                 Respond(services.glasses(), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("moustache"))
             {
                 Respond(services.moustache(), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("bald"))
             {
                 Respond(services.bald(), smsMessage, twilio);
                 services.Update();
             }
             else if (smsMessage.Body.ToLower().StartsWith("is it"))
             {
                 if (services.guess(smsMessage.Body.Substring(6)))
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Congratulations, you guessed right in " + services.Game.Turns.ToString() + ".");
                 }
                 else
                 {
                     twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Sorry, you guessed wrong.  The answer was " + services.Game.Character);
                 }
                 services.Update();
             }
             else
             {
                 twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
                         smsMessage.From,
                         "Sorry, I didn't understand your message.");
             }
         }
         return Ok();
 }
 private void Respond(bool result, SmsMessage smsMessage, TwilioRestClient twilio)
 {
     if(result)
      {
          twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
              smsMessage.From,
              "Yes.");
      }
      else
      {
          twilio.SendMessage(ConfigurationManager.AppSettings["TwilioNumber"],
              smsMessage.From,
              "No.");
      }
 }
Exemple #4
0
 public void CreateGame(SmsMessage smsMessage)
 {
     CloudTable table = cloudTable("characters");
     TableQuery<Character> query = new TableQuery<Character>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Default"));
     IEnumerable<Character> characters = table.ExecuteQuery(query);
     var randomCharacter = characters.ElementAt((new Random()).Next(0, characters.Count()));
     table = cloudTable("games");
     Game = new Game()
     {
         PartitionKey = smsMessage.From,
         RowKey = Guid.NewGuid().ToString(),
         Character = randomCharacter.RowKey,
         Turns = 0,
         Status = "Live"
     };
     TableOperation insertOperation = TableOperation.InsertOrMerge(Game);
     table.Execute(insertOperation);
 }
Exemple #5
0
 public void SaveMessage(SmsMessage smsMessage)
 {
     CloudTable table = cloudTable("messages");
     smsMessage.PartitionKey = smsMessage.From;
     smsMessage.RowKey = smsMessage.SmsSid;
     TableOperation insertOperation = TableOperation.InsertOrMerge(smsMessage);
     table.Execute(insertOperation);
 }