/**
  * @param IncomingTextObject $incomingText
  * @return User|string
  */
 public function checkRegistration(IncomingTextObject $incomingText)
 {
     // check if number is tied to existing user
     if (!($user = $this->userModel->findByPhone($incomingText->getFrom()))) {
         // user doesn't exist
         if (strtolower($incomingText->getBody()) == "join") {
             // user wants to join
             $this->userModel->create(['phone' => $incomingText->getFrom()]);
             // respond with next step
             return "Welcome to LaraSqrrl! To finish registration, we need to know what to call you. So what's your name?";
         }
         // user doesn't want to join and isn't a user
         return "Whoops, looks like you're not a LaraSqrrl user yet. If you want to join, simply reply with \"Join\".";
     }
     // check if user is in process of registering
     switch ($user->registrationStepsCompleted()) {
         case 1:
             // user has (hopefully) replied with their name, do some validation
             $name = $incomingText->getBody();
             $validate = $user->validateName(['name' => $name]);
             if ($validate) {
                 // valid name provided
                 $user->name = $name;
                 $user->save();
                 return "Nice to meet you " . $user->name . "! Last question: Are you a squirrel expert or an enthusiast? Reply with \"Expert\" or \"Enthusiast\".";
             } else {
                 // invalid name provded
                 return "Whoah, we didn't recognize that name. Names can only include letters, numbers, apostrophes, dashes, and spaces. Please try sending your name again.";
             }
         case 2:
             // user already has name saved, now need to check the role the user sent
             $role = strtolower($incomingText->getBody());
             $validate = $user->validateRole(['role' => $role]);
             if ($validate) {
                 // valid role
                 $user->role = $role;
                 $user->save();
                 // determine response based on role
                 switch ($role) {
                     case 'expert':
                         return "You're all set as a squirrel expert! Whenever an enthusiast sends in a photo, you'll receive that photo to analyze.";
                         break;
                     case 'enthusiast':
                         return "You're all set as a squirrel enthusiast! Whenever you're not sure if you're looking at a squirrel, just text us a photo. Our squirrel experts will help you out!";
                         break;
                     default:
                         return "We didn't understand that. Reply with either \"Expert\" or \"Enthusiast\".";
                         break;
                 }
             }
             break;
     }
     // user has completed registration, return the registered user
     return $user;
 }
예제 #2
0
 /**
  * Validate that an expert's text is a response to an enthusiast photo.
  *
  * @param IncomingTextObject $incomingText
  * @return array
  */
 private function validateText(IncomingTextObject $incomingText)
 {
     // break text into parts
     $text = explode(' ', $incomingText->getBody());
     // check if first string is an integer and an enthusiast user id
     if (!is_numeric($text[0])) {
         // not an integer
         return ['valid' => FALSE, 'message' => "Please respond in the format: \"12345 Yes\", where 12345 is the number you received with the photo."];
     } elseif (!($submission = $this->submissionModel->find($text[0]))) {
         // submission not found from integer provided
         return ['valid' => FALSE, 'message' => "We couldn't find that photo reference number!"];
     }
     // validate second part of text
     if (strtolower($text[1]) !== 'yes' and strtolower($text[1]) !== 'no') {
         return ['valid' => FALSE, 'message' => "Please respond in the format: \"12345 Yes\" or \"12345 No\", where 12345 is the number you received with the photo."];
     }
     return ['valid' => TRUE, 'submission' => $submission, 'answer' => strtolower($text[1])];
 }