/** * @param Request $request * @return array */ public function getUser(Request $request) { $return = []; try { $userModel = new User(); $user = $userModel->findByPhone("+1" . $request->route('phone')); // format data based on user role switch ($user->role) { case 'expert': // fetch expert's responses and associated submissions $responseModel = new Response(); $responses = $responseModel->where('user_id', $user->id)->with('submission.user')->get(); $data = $user->toArray(); $data['photos'] = $responses; $return = $this->formatExpertUser($data); break; case 'enthusiast': // fetch enthusiast's submissions and associated responses $submissionModel = new Submission(); $submissions = $submissionModel->where('user_id', $user->id)->with('response.user')->get(); $data = $user->toArray(); $data['photos'] = $submissions; $return = $this->formatEnthusiastUser($data); break; } } catch (\Exception $e) { // return some dummy data $return = $this->dummyResponse(); } return $this->respondOK($return); }
/** * @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; }
/** * Send a potential squirrel photo to an expert. * * @param EnthusiastPictureReceived $event */ public function handle(EnthusiastPictureReceived $event) { // extract data from event $incomingText = $event->getIncomingText(); $enthusiast = $event->getUser(); // retrieve all expert users $experts = $this->userModel->getAllExperts(); // create submission $submission = $this->submissionCreationService->saveSubmission($incomingText, $enthusiast); // build message $message = "#" . $submission->id . " Is this a squirrel? Respond: \"" . $submission->id . " Yes\" or \"" . $submission->id . " No\""; // send text to experts with the picture foreach ($experts as $expert) { $this->twilio->sendMMS($expert->phone, $message, $submission->photo_url); } }