jeudi 2 août 2018

Can't get Twilio to respond with a text message to 'add'/'remove'

The newly created user gets added to the database but I can't get it to send a text using the $response. Can someone explain why it won't respond with the text message? The headers in the response are wrong, but why? I can't add a return $response after $this->registerParticipant($request) - it will loop infinitely it seems.

class ParticipantController extends Controller
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

protected $twilio;
protected $accountSid;
protected $twilioNumber;

public function __construct()
{
    $this->accountSid = env('TWILIO_ACCOUNT_SID');
    $this->twilioNumber = env('TWILIO_NUMBER');
    $authToken = env('TWILIO_AUTH_TOKEN');

    $this->twilio = new Client($this->accountSid, $authToken);
}

public function incomingMessage(Request $request)
{
    $participant_id = $request->input('From');
    $isParticipant = $this->isParticipant($participant_id);
    if($isParticipant)
    {
        $activeParticipant = Participant::where('participant_id', $participant_id)->first();

        $mediaCount = (int)$request->input('NumMedia');
        $isMedia = $this->isImage($mediaCount);
        $userCommand = $request->input('Body');
        $isDogHealth = $this->isDogHealth($userCommand);

        if($isDogHealth)
        {
            $submission = new Submission;
            $submission->participant_id = $activeParticipant;
            $submission->health_grade = $request->input('Body');
            $submission->save();
        }
        else if($isMedia)
        {
            $this->storeImage();
            // image associated with submission by date
            // if possible date of exif data, or pull true exif data into new column
        }
        else 
        {
            // unsubscribe or resubscribe user
            $message = $request->input('Body');
            $updateMessage = $this->updateSubscriber($activeParticipant, $message);

            $response = new Twiml();
            $response->message($outputMessage);

            return response($response)
                ->header('Content-Type', 'text/xml'); 
        }
    }
    else 
    {
        $mediaCount = (int)$request->input('NumMedia');
        $isMedia = $this->isImage($mediaCount);
        if($isMedia)
        {
            // store media
            // say invalid message
            // ask if user wants to subscribe
            // yes create new user and attribute image to user
            // no create new user and attribute image to unsusbcribed user
        }
        $this->registerParticipant($request);
    }


}

public function registerParticipant($request)
{
    $participant_id = $request->input('From');
    $message = $request->input('Body');
    $outputMessage = $this->createParticipant($participant_id, $message);

    $response = new Twiml();
    $response->message($outputMessage);

    return response($response)
        ->header('Content-Type', 'text/xml');
}

private function createParticipant($participant_id, $message)
{
    $participant = new Participant;
    $participant->participant_id = $participant_id;
    $participant->subscribed = false;
    $participant->save();

    return $this->generateOutputMessage($participant, $message);
}

private function generateOutputMessage($participant, $message)
{
    $subscribe = 'add';
    $message = trim(strtolower($message));

    if (!$this->isValidCommand($message)) {
        return $this->messageText();
    }

    $isSubscribed = starts_with($message, $subscribe);
    $participant->subscribed = $isSubscribed;
    $participant->save();

    return $isSubscribed
        ? $this->messageText('add')
        : $this->messageText('remove');
}

private function isValidCommand($command)
{
    return starts_with($command, 'add') || starts_with($command, 'remove');
}

private function messageText($messageType = 'unknown') {

    switch($messageType) 
    {
        case 'add':
            return "Thank you for subscribing to notifications!";
            break;
        case 'remove':
            return "The number you texted from will no longer receive notifications. To start receiving notifications again, please text 'add'.";
            break;
        default:
            return "I'm sorry, that's not an option I recognize. Please, let me know if I should 'add' or 'remove' this number from notifications.";
            break;
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire