from(); } else { $num = Phone::least()->phone; } return Phone::dirty($num); } public static function send($from, $to = null, $message = null, $media = null) { $break = false; $ret = []; $reason = ''; if (is_array($from)) { $to = $from['to']; $message = $from['message']; if (isset($from['break'])) { $break = $from['break'] ? true : false; } if( isset( $from['reason'] ) ){ $reason = $from['reason']; } if( isset( $from['media'] ) ){ $media = $from['media']; } $from = $from['from']; } // If there is no cs working text everyone // https://github.com/crunchbutton/crunchbutton/issues/4475#issuecomment-73421357 if( is_array($to) && count( $to ) == 0 && ( $reason == Crunchbutton_Message_Sms::REASON_SUPPORT || $reason == Crunchbutton_Message_Sms::REASON_SUPPORT_WARNING ) ){ $to = Crunchbutton_Support::getUsers(); if( count( $to ) == 0 ){ $to = Crunchbutton_Support::getUsers( true ); } } // @todo: remove all from things elsewhere on the site $from = null; if (!$to || !$message) { return false; } if (c::env() == 'travis') { $to = '_PHONE_'; } elseif (c::getEnv() != 'live') { $to = c::admin()->testphone ? c::admin()->testphone : '_PHONE_'; } if (!is_array($to)) { $to = [$to]; } $message = trim($message); foreach ($to as $user) { $tz = null; if (is_array($user)) { if ($user['tz']) { $tz = $user['tz']; } $t = $user['num']; } else { $t = $user; } $t = Phone::dirty($t); if (!$t) { continue; } // dont message yourself if (c::admin()->id_admin && Phone::dirty(c::admin()->txt) == $t) { continue; } // dont message our own numbers if (in_array($t, Phone::numbers())) { continue; } $tfrom = $from ? $from : self::number($t); if ($tz) { $messages = preg_replace_callback('/%DATETIMETZ:(.*)%/', function($matches){ $date = new DateTime($matches[1], new DateTimeZone(c::config()->timezone)); $date = $date->format('n/j g:iA T'); return $date; }, $messages); } if ($break) { $messages = explode("\n", wordwrap($message, 160, "\n")); } else { $messages = [$message]; } foreach ($messages as $msg) { if (!$msg) { continue; } try { Log::debug([ 'action' => 'sending sms', 'to' => $t, 'from' => $tfrom, 'msg' => $msg, 'getEnv' => c::getEnv(), 'type' => 'sms' ]); $ret[] = c::twilio()->account->messages->sendMessage($tfrom, $t, $msg, $media ? $media : null); Phone_Log::log($t, $tfrom, 'message', 'outgoing', $reason); } catch (Exception $e) { Log::error([ 'action' => 'sending sms', 'to' => $t, 'from' => $tfrom, 'msg' => $msg, 'type' => 'sms' ]); } } } return $ret; } public function greeting( $name = null ){ if( $name ){ return $name . ', ' . "\n"; } return ''; // find a way to never have to use a greeting in SMS; always use first name #4372 // $greetings = [ 'Hey, ', 'Hi, ', 'Heya, ', 'Hey! ', 'Hola ', 'Bonjour ', 'Hi! ', 'Ola, ' ]; // return $greetings[ array_rand( $greetings, 1 ) ] . "\n"; } }