mirror of
https://github.com/itflow-org/itflow
synced 2026-12-02 09:09:01 +00:00
Text messaging Receive only: database 4.3.6 adds sms_providers.sms_provider_send, and a provider set to Receive only in its window takes texts in as tickets, replies and codes and texts nothing out (no Text reply, ticket number, STOP or HELP answers, texts to clients or test texts), needs no SID or secret, cancels the texts waiting to go when switched, and is shown on Text Messaging, Text Messages, Notifications and Tickets settings and in the reply box of a ticket that came in by text, with the number to call; 26.12.3 changelog
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
* Going out: smsQueue() puts a text in sms_messages, smsSendQueued() sends one and the
|
||||
* Text Messages job (cron/sms_queue.php, smsQueueRun()) sends the rest, a second apart, as
|
||||
* the service allows. A number that replied STOP gets nothing more until it replies START.
|
||||
* A provider set to Receive only (database 4.3.6, sms_provider_send = 0) texts nothing:
|
||||
* smsReady() is false, so nothing is queued, and every text below still comes in.
|
||||
*
|
||||
* Coming in: the service posts each text to api/sms/<provider>.php, which hands it to
|
||||
* smsInbound(). What happens to it, in order:
|
||||
@@ -56,7 +58,7 @@ function smsProviderKinds(): array
|
||||
'Skyetel' => [
|
||||
'icon' => 'fas fa-sms',
|
||||
'portal_url' => 'https://portal.skyetel.com',
|
||||
'about' => 'Your Skyetel number texts clients, and texts to it come back into ITFlow.',
|
||||
'about' => 'Texts to your Skyetel number come into ITFlow - as tickets, replies and codes - and, unless it is Receive only, ITFlow texts clients from it.',
|
||||
'username_label' => 'SID',
|
||||
'secret_label' => 'Secret',
|
||||
'endpoint' => 'skyetel',
|
||||
@@ -100,15 +102,34 @@ function smsProvider(bool $fresh = false): ?array
|
||||
return $provider;
|
||||
}
|
||||
|
||||
/* Whether texts can go out - a provider that is on, with its keys and number */
|
||||
/* Whether a provider sends texts - not when it is Receive only (database 4.3.6). Before the update, every one sends */
|
||||
function smsSends(?array $provider): bool
|
||||
{
|
||||
return $provider !== null && (!array_key_exists('sms_provider_send', $provider) || intval($provider['sms_provider_send']) === 1);
|
||||
}
|
||||
|
||||
/* Whether texts can go out - a provider that is on and sends, with its keys and number */
|
||||
function smsReady(): bool
|
||||
{
|
||||
$provider = smsProvider();
|
||||
return $provider && isset(smsProviderKinds()[$provider['sms_provider_name']])
|
||||
return $provider && smsSends($provider) && isset(smsProviderKinds()[$provider['sms_provider_name']])
|
||||
&& trim((string) $provider['sms_provider_username']) !== '' && trim((string) $provider['sms_provider_secret']) !== ''
|
||||
&& smsNumberIsPhone((string) $provider['sms_provider_number']);
|
||||
}
|
||||
|
||||
/* Whether texting is on but set to Receive only - texts come in, nothing goes out */
|
||||
function smsReceiveOnly(): bool
|
||||
{
|
||||
$provider = smsProvider();
|
||||
return $provider !== null && !smsSends($provider);
|
||||
}
|
||||
|
||||
/* Why no text can go - for what an agent or admin is told */
|
||||
function smsNotSendingWhy(): string
|
||||
{
|
||||
return smsReceiveOnly() ? "Texts don't go out - Text Messaging is set to Receive only" : "Texting isn't set up - Admin > Text Messaging";
|
||||
}
|
||||
|
||||
/* The address the provider posts texts to - what to paste into its portal */
|
||||
function smsInboundUrl(array $provider): string
|
||||
{
|
||||
@@ -150,19 +171,30 @@ function smsProviderCheck(array $in, ?array $before): array
|
||||
if ($after['sms_provider_secret'] === '' && $before) {
|
||||
$after['sms_provider_secret'] = (string) $before['sms_provider_secret'];
|
||||
}
|
||||
// Send and receive (1), or Receive only (0) - after the 4.3.6 database update
|
||||
$sends = true;
|
||||
if (smsColumnExists('sms_providers', 'sms_provider_send')) {
|
||||
$sends = (string) ($in['send'] ?? '1') !== '0';
|
||||
$after['sms_provider_send'] = $sends ? 1 : 0;
|
||||
}
|
||||
|
||||
// The keys are for sending - Receive only keeps any typed in, for when it sends
|
||||
if ($after['sms_provider_username'] === '') {
|
||||
$errors['username'] = "Its {$kind['username_label']}, from $name's portal.";
|
||||
if ($sends) {
|
||||
$errors['username'] = "Its {$kind['username_label']}, from $name's portal - or pick Receive only, which needs none.";
|
||||
}
|
||||
} elseif (mb_strlen($after['sms_provider_username']) > 250 || preg_match('/\s/', $after['sms_provider_username'])) {
|
||||
$errors['username'] = "That isn't a {$kind['username_label']} - it has no spaces.";
|
||||
}
|
||||
if ($after['sms_provider_secret'] === '') {
|
||||
$errors['secret'] = "Its {$kind['secret_label']}, shown when the {$kind['username_label']} is made.";
|
||||
if ($sends) {
|
||||
$errors['secret'] = "Its {$kind['secret_label']}, shown when the {$kind['username_label']} is made.";
|
||||
}
|
||||
} elseif (mb_strlen($after['sms_provider_secret']) > 500) {
|
||||
$errors['secret'] = "That is too long to be a {$kind['secret_label']}.";
|
||||
}
|
||||
if (trim((string) ($in['number'] ?? '')) === '') {
|
||||
$errors['number'] = "The $name number texts go from - SMS-enabled in $name's portal.";
|
||||
$errors['number'] = "Your $name number - SMS-enabled in $name's portal.";
|
||||
} elseif ($after['sms_provider_number'] === '') {
|
||||
$errors['number'] = "That isn't a phone number - with its dial code, like +1 412 555 0100.";
|
||||
}
|
||||
@@ -204,6 +236,9 @@ function smsProviderChangeLines(array $before, array $after): array
|
||||
if (intval($before['sms_provider_active']) !== intval($after['sms_provider_active'])) {
|
||||
$lines[] = intval($after['sms_provider_active']) ? 'turned on' : 'turned off';
|
||||
}
|
||||
if (isset($after['sms_provider_send']) && intval($before['sms_provider_send'] ?? 1) !== intval($after['sms_provider_send'])) {
|
||||
$lines[] = intval($after['sms_provider_send']) ? 'set to send and receive' : 'set to receive only';
|
||||
}
|
||||
return $lines;
|
||||
}
|
||||
|
||||
@@ -539,7 +574,7 @@ function smsQueue($to, string $text, array $meta = []): array
|
||||
|
||||
$provider = smsProvider();
|
||||
if (!smsReady()) {
|
||||
return ['ids' => [], 'error' => "Texting isn't set up - Admin > Text Messaging"];
|
||||
return ['ids' => [], 'error' => smsNotSendingWhy()];
|
||||
}
|
||||
$number = preg_replace('/\D/', '', (string) $to);
|
||||
if (!smsNumberIsPhone($number)) {
|
||||
@@ -597,7 +632,7 @@ function smsSendQueued(int $id): array
|
||||
$provider = smsProvider();
|
||||
$kind = $provider ? (smsProviderKinds()[$provider['sms_provider_name']] ?? null) : null;
|
||||
if (!$kind || !smsReady()) {
|
||||
return ['ok' => false, 'error' => "Texting isn't set up"];
|
||||
return ['ok' => false, 'error' => smsNotSendingWhy()];
|
||||
}
|
||||
|
||||
// Development mode sends no texts - they wait in the queue (functions/dev_mode.php)
|
||||
@@ -1341,6 +1376,32 @@ function smsTicketRecipient(array $ticket): ?array
|
||||
return ['number' => $number, 'name' => $name, 'opted_out' => smsOptedOut($number)];
|
||||
}
|
||||
|
||||
/*
|
||||
* The number that texted in on a ticket, while Text Messaging is Receive only - the reply box says replies aren't
|
||||
* texted, and the number to call. '' otherwise
|
||||
*/
|
||||
function smsTicketTextedInNumber(array $ticket): string
|
||||
{
|
||||
global $mysqli;
|
||||
if (!smsReceiveOnly()) {
|
||||
return '';
|
||||
}
|
||||
$last = mysqli_fetch_row(mysqli_query($mysqli, "SELECT sms_message_number FROM sms_messages WHERE sms_message_ticket_id = " . intval($ticket['ticket_id']) . " AND sms_message_direction = 'in' ORDER BY sms_message_id DESC LIMIT 1"));
|
||||
return smsNumberIsPhone((string) ($last[0] ?? '')) ? (string) $last[0] : '';
|
||||
}
|
||||
|
||||
/*
|
||||
* Texts waiting to go, cancelled - when the provider is set to Receive only, or deleted, they would otherwise go late or
|
||||
* never. How many
|
||||
*/
|
||||
function smsCancelWaiting(string $why): int
|
||||
{
|
||||
global $mysqli;
|
||||
mysqli_query($mysqli, "UPDATE sms_messages SET sms_message_status = 'cancelled', sms_message_error = '" . escapeSql($why) . "'
|
||||
WHERE sms_message_direction = 'out' AND sms_message_status = 'queued'");
|
||||
return mysqli_affected_rows($mysqli);
|
||||
}
|
||||
|
||||
/*
|
||||
* Text a reply on a ticket to who it goes to (smsTicketRecipient()) - now, the rest by the job
|
||||
* when it is long. ['ok' => went, 'queued' => waiting to go, 'to' => who, for reading, 'error'].
|
||||
@@ -1349,7 +1410,7 @@ function smsTicketReplySend(array $ticket, int $reply_id, string $html, int $use
|
||||
{
|
||||
$recipient = smsTicketRecipient($ticket);
|
||||
if (!$recipient) {
|
||||
return ['ok' => false, 'queued' => false, 'to' => '', 'error' => smsReady() ? "there's no mobile number on its contact to text" : "texting isn't set up"];
|
||||
return ['ok' => false, 'queued' => false, 'to' => '', 'error' => smsReady() ? "there's no mobile number on its contact to text" : lcfirst(smsNotSendingWhy())];
|
||||
}
|
||||
$to = ($recipient['name'] !== '' ? $recipient['name'] . ' at ' : '') . smsNumberText($recipient['number']);
|
||||
$text = smsTextFromHtml($html);
|
||||
@@ -1676,7 +1737,7 @@ function smsOptInAsk(int $contact_id, int $user_id): array
|
||||
{
|
||||
global $mysqli;
|
||||
if (!smsReady()) {
|
||||
return ['ok' => false, 'error' => "texting isn't set up"];
|
||||
return ['ok' => false, 'error' => lcfirst(smsNotSendingWhy())];
|
||||
}
|
||||
$contact = mysqli_fetch_assoc(mysqli_query($mysqli, "SELECT contact_id, contact_name, contact_client_id, contact_mobile, contact_mobile_country_code FROM contacts WHERE contact_id = $contact_id AND contact_archived_at IS NULL"));
|
||||
$number = $contact ? smsContactNumber($contact) : '';
|
||||
|
||||
Reference in New Issue
Block a user