/** * Process an enthusiast's incoming text. * * @param IncomingTextObject $incomingText * @param User $user * @return string */ public function process(IncomingTextObject $incomingText, User $user) { // check if a picture was sent in message if ($incomingText->getNumMedia() < 1) { // no picture received return "We're not sure what you're trying to do! All you need to do is send us a photo of a potential squirrel to receive an answer from a qualified squirrel expert."; } // a picture has been receive, fire event event(new EnthusiastPictureReceived($incomingText, $user)); // let user know their potential squirrel photo is in good hands return "You're photo is on its way to our world-renowned squirrel experts for analysis!"; }
/** * 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])]; }
/** * @param IncomingTextObject $incomingText * @param User $enthusiast * @return Submission */ public function saveSubmission(IncomingTextObject $incomingText, User $enthusiast) { // extract media type of photo $mediaTypeArray = explode("/", $incomingText->getMediaType(0)); $mediaType = $mediaTypeArray[count($mediaTypeArray) - 1]; // build path for photo $date = Carbon::now(); $path = '/user_submissions/' . $enthusiast->id . "/" . $date->toDateString() . '/' . $date->format('His') . '.' . $mediaType; // send photo to s3 $s3 = Storage::disk('s3'); $s3->put($path, file_get_contents($incomingText->getMediaUrl(0))); // build photo url $photo_url = 'https://s3.amazonaws.com/' . config('filesystems.disks.s3.bucket') . $path; // save submission $submission = $this->submission->create(['user_id' => $enthusiast->id, 'photo_url' => $photo_url]); // return saved submission return $submission; }
/** * @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; }