> For the complete documentation index, see [llms.txt](https://docs.mikopbx.com/mikopbx/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.mikopbx.com/mikopbx/english/faq/cases/dynamic-queue-agents.md).

# Dynamic Queue Agents

The "Dynamic Queue Agents" application in MikoPBX is a feature designed to enhance the flexibility and efficiency of call center operations.

From time to time there is a need to add and remove employees from the queue. Until now, this was only possible to do through the web interface of the telephone exchange.

In this article I will tell you how to implement the ability to connect and disconnect from a queue by dialing a service extension number.

1. Create a new queue with a four-digit extension number. For example, **2001**. (see documentation “[Call Queue](/mikopbx/english/manual/telephony/call-queues.md)”)

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FHLXqxkByQES38o3jp0IQ%2FnewCallQueue.png?alt=media&amp;token=8a982fdd-206d-44da-947f-fe5e1908fe06" alt=""><figcaption><p>"Call queue" section</p></figcaption></figure>

2. Describe the new “[Dialplan Application](/mikopbx/english/manual/modules/dialplan-applications.md)”

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FNrOZjBErG4lnGPyR87Uu%2FNEWDialplan.png?alt=media&amp;token=9dff900f-c127-4426-96c5-69abe9d4e74e" alt=""><figcaption><p>"Application dialplans" section</p></figcaption></figure>

3. Assign a “**Extension to call the application**” - **999XXXXX**, you can redefine the first three digits with your own combination

Code type - “**PHP AGI script**”

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F1D97rKWZ1WzifbRpv0rl%2FNewDialplanSettings.png?alt=media&amp;token=2f3d6173-5188-4f78-8969-4a288243e93d" alt=""><figcaption><p>Configuration for dialplan</p></figcaption></figure>

4. In the "**Programme code**" tab, paste the following content:

```php
<?php

use MikoPBX\Common\Models\CallDetailRecordsTmp;
use MikoPBX\Common\Models\CallQueueMembers;
use MikoPBX\Common\Models\CallQueues;
use MikoPBX\Common\Models\Extensions;
use MikoPBX\Core\Asterisk\AGI;
use MikoPBX\Core\Asterisk\Configs\QueueConf;

require_once 'Globals.php';

$agi = new AGI();
$agi->answer();

$extension = $agi->get_variable("CHANNEL(peername)", true);
if(empty($extension)){
    $extension = $agi->get_variable("CALLERID(num)", true);
}
$q_exten   = substr($agi->request['agi_extension'], 4);
$add_agent = substr($agi->request['agi_extension'], 3, 1);

$agi->noop("$extension $q_exten $add_agent");

$res = Extensions::findFirst("number='{$extension}'");
if(!$res){
    // Error, such extension number does not exist.
    $agi->hangup();
}
$res = CallQueues::findFirst("extension='{$q_exten}'");
if(!$res){
    // Error, such queue does not exist.
    $agi->hangup();
}
$a_count = CallQueueMembers::maximum([ "queue='{$res->uniqid}'", 'column' => 'priority'] ) + 1;
$member  = CallQueueMembers::findFirst("queue='{$res->uniqid}' AND extension='{$extension}'");

if($add_agent === '1' && !$member){
    $member = new CallQueueMembers();
    $member->extension  = $extension;
    $member->queue      = $res->uniqid;
    $member->priority   = $a_count;
    $member->save();
    QueueConf::queueReload();
}

if($add_agent == false && $member){
    $member->delete();
    QueueConf::queueReload();
}

$linkedid = $agi->get_variable("CDR(linkedid)", true);
$res = CallDetailRecordsTmp::find("linkedid='{$linkedid}'");
foreach ($res as $data){
    $data->delete();
}
sleep(2);
$agi->hangup();
```

Press "**Save settings**"!

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FxdrSoL1ZuclNa59r89m9%2FProgrammeCodeForDA.png?alt=media&amp;token=1f05f5d5-80bb-4a68-9206-67f77f6d3951" alt=""><figcaption><p>Programme code for Dynamic Agents</p></figcaption></figure>

Now "Dynamic queue agents" application is ready to use :tada:.

{% hint style="success" %}
Dial **99912001** to join queue 2001 .

Dial **99902001** to disconnect from queue 2001.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.mikopbx.com/mikopbx/english/faq/cases/dynamic-queue-agents.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
