Exemplo n.º 1
0
 public function handleKeywordActions($replytomessage = null)
 {
     $bReplyToStop = true;
     // LOG IT for testing
     $this->_writeLog("ReplyToMessage is " . print_r($replytomessage, 1));
     // See if this matches a keyword and handle special cases
     $keyword = new Application_Model_Keyword($this->message, true);
     $this->_writeLog("Keyword folderid: {$keyword->folderid}");
     // just hacking this for now
     if (!in_array(strtolower($this->message), array('stop', 'end', 'quit', 'info', 'help', 'cancel', 'stopall'))) {
         // if it didn't load a folderid, it is probably not a valid keyword
         // Now check if there is a replytomessageid and this is a response to
         // a previous outbound
         if (empty($keyword->folderid)) {
             if ($replytomessage) {
                 $this->_writeLog("Originating Keyword ID: {$replytomessage->keywordid}");
                 $keyword = new Application_Model_Keyword($replytomessage->keywordid);
             }
         }
         // If there is a replytomessage, get the create user. This will be for all campaign replies, as well
         // as multi level conversations. If there is no replytomessage we should ALWAYS have a keyword, so
         // get the user that currently owns that keyword and send the reply back as them.
         $creatorid = !empty($replytomessage->createuser) ? $replytomessage->createuser : $keyword->createuser;
     } else {
         $creatorid = 0;
     }
     // Prepare the reply messages if there is one
     $keyword->response = $keyword->replyheader ? "{$keyword->replyheader}:" : '';
     $keyword->response .= "{$keyword->replybody}\n{$keyword->replyfooter}";
     if (trim($keyword->response) === "reply STOP 2 stop") {
         $bReplyToStop = false;
     }
     // 04/13/2013 tmp solution to stop double msg
     // Handle special actions
     $keyword->handleSpecialActions($this);
     // LOG IT for testing
     $this->_writeLog("Keyword ID is {$keyword->id}");
     // If we have an id then this is a keyword
     if ($keyword->id) {
         // LOG IT for testing
         $this->_writeLog("Opted Out Flag is {$keyword->optedOut}");
         // LOG IT for testing
         $this->_writeLog("Message Creator is {$creatorid}");
         $message = new Application_Model_Message(new Application_Model_User((int) $creatorid));
         //            $smsoutbound = new Application_Model_Smsoutbound($message);
         // Set the message keyword and folder id's. These will be used for tracking a conversation as well as handling optouts
         $message->keywordid = $keyword->id;
         $message->folderid = $keyword->folderid;
         // This would only be set from a SpecialAction model
         if (!$this->optedOut) {
             $this->_writeLog("non-optout");
             // Subscribe this sender, but only if they are not already a subscriber
             if (!$keyword->hasSubscriberPhone($this->device_address)) {
                 // Depth for this message is 1 since we are an optin
                 $this->depth = 1;
                 // Sign them up for this keyword
                 $keyword->addSubscriber($this->device_address);
                 /* --------------------- */
                 if ($keyword->notifybysms) {
                     $dt = date('Y-m-d H:i:s');
                     $confirm = "87365:" . $this->device_address . " has opted in to keyword " . $keyword->keyword . " on {$dt}";
                     $this->alertMessage($keyword->notifybysms, $confirm);
                 }
                 if ($keyword->notifybyemail) {
                     $dt = date('Y-m-d H:i:s');
                     $confirm = "87365:" . $this->device_address . " has opted in to keyword " . $keyword->keyword . " on {$dt}";
                     $this->alertnewaccount($keyword->notifybyemail, "Lead notification", $confirm);
                 }
                 /* --------------------- */
                 $this->_writeLog("add subscriber: {$this->device_address}, response: {$keyword->response}");
                 // If there is an autoreply, send it now
                 if ($keyword->response) {
                     if ($bReplyToStop) {
                         // update message exp time
                         if ($keyword->offerexp) {
                             $message->sendNow($this->updateExp($keyword->response, $keyword->offerexp), array($this->device_address), $this->inbound_address);
                         } else {
                             $message->sendNow($keyword->response, array($this->device_address), $this->inbound_address);
                         }
                     }
                 }
             } else {
                 if (!$keyword->usecustomresponse) {
                     // If there is an alternate autoresponder
                     if ($keyword->usealt) {
                         $keyword->response = $keyword->replyheader ? "{$keyword->replyheader}:" : '';
                         $keyword->response .= "{$keyword->replybodyalt}\n{$keyword->replyfooter}";
                     } else {
                         // Otherwise provide a default one.
                         $keyword->response = 'You are already opted in. Thanks for your message.';
                         // TODO: ACTUALLY WE WANT THE 1ST ONE TO KEEP GOING OUT, BUT ALTERNATE RESPONSES
                         // NEED TO BE SET UP FOR ALL EXISTING KEYWORDS 1ST SINCE IT IS A NEW FEATURE
                     }
                 }
                 $this->_writeLog("already opted in: {$keyword->response}");
                 if ($keyword->replybodyalt) {
                     $message->sendNow($keyword->replybodyalt, array($this->device_address), $this->inbound_address);
                 } else {
                     $keyword->replybodyalt = 'You are already opted in. Thanks for your message.';
                     $message->sendNow($keyword->replybodyalt, array($this->device_address), $this->inbound_address);
                 }
             }
         } else {
             /* Taking out for now so stop messages can send their default responses
                Actually, I think this might be unnecessary, because if a custom message is set
                it will override the reponse and be used below...
                if (!$keyword->usecustomresponse) {
                $keyword->response = 'Your message has been received.';
                } */
             $this->_writeLog("OptOut {$keyword->response}");
             if ($bReplyToStop) {
                 $message->sendNow($keyword->response, array($this->device_address), $this->inbound_address);
             }
         }
     }
 }