# Dynamic Queue Agents

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="/files/GlKA15DRfbqbE0AkRfX2" 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="/files/UvzYx95g7RguJ0r7VaO5" 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="/files/zlhtmcR3UHzeJhqORaOE" 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="/files/4kWOq4ZHpZxxktHm4jNm" 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: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

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

The question should be specific, self-contained, and written in natural language.
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.
