Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active July 23, 2025 13:57
Show Gist options
  • Select an option

  • Save lgaetz/b350b9fa9ac18730150b387a1525aa4e to your computer and use it in GitHub Desktop.

Select an option

Save lgaetz/b350b9fa9ac18730150b387a1525aa4e to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
if (!isset($argv[2])){
echo "
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
*
* Script: lgaetz-dnd.php
*
* Latest version: https://gist.github.com/lgaetz/b350b9fa9ac18730150b387a1525aa4e
*
* Usage: Run at bash prompt of FreePBX system running FreePBX 13+ with first argument as
* action (show,set,unset,toggle), and the second argument as extension number
*
* # lgaetz-dnd.php <action> <ext#> [debug]
*
*
* License: GNU/GPL3+
*
* History:
* 2017-11-18 First commit by lgaetz
* 2018-05-29 Added extension validation and debug option
* 2025-07-23 No changes - confirmed working in 17
*
***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** ***** *****
";
exit;
}
include '/etc/freepbx.conf';
$FreePBX = FreePBX::Create();
$debug=false;
if (isset($argv[3])) {
$debug=true;
}
// Check to see if extension is valid
$user=$FreePBX->Core->getUser($argv[2]);
if ($user['extension']!=$argv[2]) {
echo "Error ".$argv[2]." is not a valid extension, exiting.";
exit;
}
// get current dnd status
$status=$FreePBX->Donotdisturb->getStatusByExtension($argv[2]);
switch (strtolower($argv[1])) {
case "show":
if($status=="YES") {
echo"DND Enabled\n";
} else {
echo"DND Disabled\n";
}
break;
case "set":
if($status=="YES") {
output("DND already enabled for ".$argv[2]);
} else {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"YES");
output("Enabling DND for ".$argv[2]);
}
break;
case "unset":
if($status=="YES") {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"");
output("Disabling DND for ".$argv[2]);;
} else {
output("DND already disabled for ".$argv[2]);
}
break;
case "toggle":
if($status=="YES") {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"");
output("Disabling DND for ".$argv[2]);
} else {
$device=$FreePBX->Donotdisturb->setStatusByExtension($argv[2],"YES");
output("Enabling DND for ".$argv[2]);
}
break;
default:
echo "invalid action\n";
exit;
}
function output($string) {
global $debug;
if ($debug) {
echo $string."\n";
}
}
@Dirk6665
Copy link

Awesome script - thank you!

@lgaetz
Copy link
Author

lgaetz commented Nov 5, 2024

Report of issues spiking CPU. This might be mitigated by adding this line before the include

$restrict_mods = array('core' => true,'donotdisturb' => true);
include '/etc/freepbx.conf';

not tested

@TomServo138
Copy link

TomServo138 commented Jan 9, 2025

I used this Code to write an auto-timeout (e.g., 2h) when DND is enabled on a given line. Since I couldn't find any info on how to do that (and had to figure out), thought I document one such solution here.

  1. Front-run the *78 DND On handler with your own pre-handler code. I used *50 Key Code to point to custom handler that included the following dialplan code:
   same => n,TrySystem(/etc/asterisk/DNDTimeout2h.sh ${CALLERID(num)} &)
   same => n,Goto(app-dnd-on,*78,1) 

  1. Then include simple BASH script to handle:
#!/bin/bash
(sleep 120m) && (/usr/bin/php /etc/asterisk/lgaetz-dnd.php unset "$1" > /dev/null 2>&1)

This is quick & dirty implementation for my specific need. Production use obviously would need add'l safeguards.

@lgaetz
Copy link
Author

lgaetz commented Jan 9, 2025

@TomServo138 Very clever, I like that idea a lot. The only changes I would propose are:

  1. Call macro-user-callerid dialplan first using either Macro or GoSub depending on fpbx version so that the AMPUSER variable gets defined
  2. Change the system command to use ${AMPUSER} instead of caller id number.

This will catch the edge cases where the caller's caller id number doesn't match the extension number, which could happen when using shadow extensions for webrtc clients, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment