CS Auto-Reply #5042

This commit is contained in:
Pererinha 2015-04-03 14:53:28 -03:00
parent 7626abf299
commit 44a1b97377
6 changed files with 82 additions and 10 deletions

View File

@ -0,0 +1 @@
INSERT INTO `config` (`id_site`, `key`, `value`) VALUES (NULL,'auto-reply-text',"Hey, thanks for texting, we'll be with ya in a second! -Crunchbutton");

View File

@ -0,0 +1 @@
ALTER TABLE support_message CHANGE COLUMN `type` `type` enum('sms','note','auto-reply') NOT NULL DEFAULT 'sms';

View File

@ -19,6 +19,7 @@ class Crunchbutton_Message_Sms extends Crunchbutton_Message {
const REASON_GIFT_CARD = 'gift card';
const REASON_GIFT_CARD_REDEEMED = 'gift card redeemed';
const REASON_SETTLEMENT_FAIL = 'settlement fail';
const REASON_AUTO_REPLY = 'auto reply';
public static function number($t = null) {
if ($t) {

View File

@ -8,6 +8,8 @@ class Crunchbutton_Support extends Cana_Table_Trackchange {
const TYPE_TICKET = 'TICKET';
const TYPE_COCKPIT_CHAT = 'COCKPIT_CHAT';
const CONFIG_AUTO_REPLY_KEY = 'auto-reply-text';
const STATUS_OPEN = 'open';
const STATUS_CLOSED = 'closed';
@ -349,7 +351,62 @@ class Crunchbutton_Support extends Cana_Table_Trackchange {
$messageParams[ 'phone' ] = $params[ 'phone' ];
$messageParams[ 'body' ] = $params[ 'body' ];
$messageParams[ 'media' ] = $params[ 'media' ];
$this->addMessage( $messageParams );
// CS Auto-Reply #5042
$support = $this;
Cana::timeout( function() use( $support ) {
$support->autoReply();
} );
}
public function autoReply(){
$body = $this->autoReplyMessage();
if( $this->shoudAutoReply() && $body ){
$messageParams[ 'type' ] = Crunchbutton_Support_Message::TYPE_AUTO_REPLY;
$messageParams[ 'from' ] = Crunchbutton_Support_Message::TYPE_FROM_SYSTEM;
$messageParams[ 'visibility' ] = Crunchbutton_Support_Message::TYPE_VISIBILITY_EXTERNAL;
$messageParams[ 'phone' ] = $params[ 'phone' ];
$messageParams[ 'body' ] = $body;
$message = $this->addMessage( $messageParams );
if( $message->id_support_message ){
$message->notify();
}
}
}
public function autoReplyMessage(){
$message = Crunchbutton_Config::q( 'SELECT * FROM config WHERE `key` = "' . Crunchbutton_Support::CONFIG_AUTO_REPLY_KEY . '" ORDER BY RAND() LIMIT 1' );
if( $message->value ){
return $message->value;
}
return false;
}
public function lastAutoReplyByPhone( $phone ){
$support_message = Crunchbutton_Support_Message::q( 'SELECT sm.* FROM support s
INNER JOIN support_message sm ON sm.id_support = s.id_support
WHERE s.phone = "' . $phone . '"
AND sm.type = "' . Crunchbutton_Support_Message::TYPE_AUTO_REPLY . '"
ORDER BY id_support_message DESC LIMIT 1' )->get( 0 );
if( $support_message->id_support_message ){
return $support_message;
}
return false;
}
public function shoudAutoReply(){
$last = $this->lastAutoReplyByPhone( $this->phone );
if( !$last ){
return true;
} else {
$now = new DateTime( 'now', new DateTimeZone( c::config()->timezone ) );
if( Crunchbutton_Util::intervalMoreThan24Hours( $now->diff( $last->date() ) ) ){
return true;
}
}
return false;
}
public function addAdminReply($body, $guid = null){

View File

@ -4,6 +4,7 @@ class Crunchbutton_Support_Message extends Cana_Table {
const TYPE_SMS = 'sms';
const TYPE_NOTE = 'note';
const TYPE_AUTO_REPLY = 'auto-reply';
const TYPE_FROM_CLIENT = 'client';
const TYPE_FROM_REP = 'rep';
const TYPE_FROM_SYSTEM = 'system';
@ -108,6 +109,17 @@ class Crunchbutton_Support_Message extends Cana_Table {
$support = $this->support();
$phone = $support->phone;
if (!$phone) return;
// Auto reply messages
if( $this->type == Crunchbutton_Support_Message::TYPE_AUTO_REPLY && $this->body ){
Crunchbutton_Message_Sms::send([
'to' => $phone,
'message' => $this->body,
'reason' => Crunchbutton_Message_Sms::REASON_AUTO_REPLY
]);
return true;
}
if( $this->admin()->id_admin ){
$rep_name = $this->admin()->firstName();
} else {