# Conference with a regular external subscriber

* Let's say a conference room with the number "**354233**" is configured on the PBX.&#x20;
* When calling an employee(s) to a conference, it is always necessary to connect an external subscriber.&#x20;
* When disconnecting employees from the conference, the external subscriber must be disconnected

This may be required to organize communication with an external (relative to MIKOPBX) conference. It will allow saving on outgoing calls (only one line will be occupied).

1. In the "**Telephony**" section, go to "**Conference**" and create a new conference.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FxPn72moakHl8fDon3Hh1%2FNewConference.png?alt=media&#x26;token=4958dc06-cf68-4922-9969-cc280e392ebe" alt=""><figcaption><p>New Conference</p></figcaption></figure>

2. We will indicate the name of the room "**Z-CONF-354233**"

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2Fy00JXhJzuvFUkVpTfuE0%2FNameOfRoom.png?alt=media&#x26;token=b9068fac-ca88-4a3b-9f0e-30215229d76e" alt=""><figcaption><p>Conference name</p></figcaption></figure>

3. We will indicate the internal number "**354233**"

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F4Mu4XW8EVyxOltkYWzwV%2FNumberOfRoom.png?alt=media&#x26;token=7b6cffaa-6854-47eb-910c-0dd02dc05d06" alt=""><figcaption><p>Conference number</p></figcaption></figure>

4. Let's describe the "**Dialplan Application**" (see [Dialplan Applications](https://docs.mikopbx.com/mikopbx/english/manual/modules/dialplan-applications))

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FS6mZS39XCPs3O2iIsUi2%2FNewDialplan.png?alt=media&#x26;token=0b3add55-158e-4821-bb01-d894d59cf0e7" alt=""><figcaption><p>New Dialplan Application</p></figcaption></figure>

5. Name "**Z-Worker-CONF**"

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FekQzpc6Mi6V61PiqqW4p%2FNameOfDialplan.png?alt=media&#x26;token=331827eb-a85b-4c4d-9e62-1ac921e5147c" alt=""><figcaption><p>Name of dialplan</p></figcaption></figure>

6. The number to call "**2200103**" (can be arbitrary)

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F488YZE7KBhP9HEvnwk8S%2FNumberOfDialplan.png?alt=media&#x26;token=56298ea4-19ab-4b2b-8c61-979ca818f8d8" alt=""><figcaption><p>Number for dialplan</p></figcaption></figure>

7. Specify the 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%2FClnoSQ5hruYrGu0WXY10%2FTypeOfCode.png?alt=media&#x26;token=ccf99a8f-d5ba-4f0d-a429-251170ee3108" alt=""><figcaption><p>Type of script </p></figcaption></figure>

8. Programme code:

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FkjG5rQlcBqTfNf3F50WI%2FCodeForDialplan.png?alt=media&#x26;token=463780b6-f5d4-4b1a-ba20-c57fe858ad85" alt=""><figcaption><p>Code for dialplan</p></figcaption></figure>

```php
<?php

require_once 'Globals.php';

use MikoPBX\Core\System\Util;
use \MikoPBX\Core\Asterisk\AGI;
use \MikoPBX\Core\Asterisk\AsteriskManager;

function checkStartConf(){
    $Z_DTMF         = '354233';
    $Z_PROVIDER_ID  = 'SIP-1601534775';
    $Z_DST          = '37127776675';

    $am = new AsteriskManager();
    $am->connect();

    $confEmpty = true;
    $confChannelFound = false;
    $data = $am->meetMeCollectInfo($Z_DTMF);
    foreach ($data as $channelData){
        $value = $am->GetVar($channelData['Channel'], 'ZDTMF', null, false);
        if(!empty($value)){
            // This is the conference channel.
            $confChannelFound = true;
        }else{
            // There is an end user channel.
            $confEmpty = false;
        }
    }

    if($confChannelFound === false && $confEmpty === false){
        // You need to connect to an external conference.
        $am->Originate(
            'Local/'.$Z_DTMF.'@z-meetme',
            $Z_DST,
            'z-outgoing',
            '1',
            null,
            null,
            null,
            null,
            "__ZDTMF={$Z_DTMF},__ZPROVIDERID={$Z_PROVIDER_ID},__ZDST={$Z_DST}}",
            '0');

    }elseif ($confChannelFound === true && $confEmpty === true){
        $asteriskCmd = Util::which('asterisk');
        Util::mwExec("{$asteriskCmd} -rx 'meetme kick {$Z_DTMF} all'");
    }
}

$action = $argv[1]??'';
if($action === 'start'){
    $pid = Util::getPidOfProcess(basename($argv[0])." start$", getmypid());
    var_dump($pid);
    if(!empty($pid)){
        $killCmd = Util::which('kill');
        Util::mwExec("{$killCmd} {$pid}");
    }
    while (true){
        checkStartConf();
        sleep(3);
    }
}else{
    $agi = new AGI();
    $agi->answer();
    checkStartConf();
    $agi->hangup();
}
```

9. Edit the file "**/var/spool/cron/crontabs/root**" via the [System files Customization ](https://docs.mikopbx.com/mikopbx/english/manual/system/custom-files)menu

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FJrGvsoFBZnOMpUotiJjn%2FcrontabsRoot.png?alt=media&#x26;token=713f2817-9202-43cb-a7b6-3d173073bc00" alt=""><figcaption><p>/var/spool/cron/crontabs/root file</p></figcaption></figure>

10. Adding the task to the end of the file:

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FcLA6gMjtipUASbFrUD9y%2FCodeForCrontabs.png?alt=media&#x26;token=11c30250-c06c-4e30-a9b5-35ad23bcc717" alt=""><figcaption><p>Code for Crontabs/root</p></figcaption></figure>

```php
*/1 * * * * /usr/bin/php -f /var/lib/asterisk/agi-bin/DIALPLAN-APP-EC12CFAE6783FE82FD34F16E40C7386B.php start > /dev/null 2> /dev/null
```

In this example, "**DIALPLAN-APP-EC12CFAE6783FE82FD34F16E40C7386B**" is the ID of the previously created application. The ID can be peeped in the browser address bar when editing the application.

11. We will describe additional contexts through the [Customization of system files](https://docs.mikopbx.com/mikopbx/english/manual/system/custom-files) menu. We will edit the extensions.conf file.

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FNuELWVzuFh5rThr08jvu%2FEditExtensions.conf.png?alt=media&#x26;token=567dc075-00ed-45a7-bc10-915ccc1dc97f" alt=""><figcaption><p>Extensions.conf file</p></figcaption></figure>

12. Add the following code to the end of the file:

```php
[z-outgoing]
exten => _X!,1,Ringing()
  same => n,Gosub(${ISTRANSFER}dial,${EXTEN},1)
  same => n,Dial(PJSIP/${EXTEN}@${ZPROVIDERID},600,${DOPTIONS}TKU(z-dial-answer)b(dial_create_chan,s,1))
  same => n,ExecIf($["${ISTRANSFER}x" != "x"]?Gosub(${ISTRANSFER}dial_hangup,${EXTEN},1))
  same => n,Set(pt1c_UNIQUEID=${EMPTY_VALUE})
  same => n,ExecIf($["${BLINDTRANSFER}x" != "x"]?AGI(check_redirect.php,${BLINDTRANSFER}))
  same => n,Hangup()

[z-dial-answer]
exten => s,1,NoOp(Answered send DTMF...)
  same => n,Gosub(dial_answer,${EXTEN},1)
  same => n,SendDTMF(${ZDTMF})
  same => n,return
	
[z-meetme]
exten => _X!,1,Answer()
  same => n,Meetme(${EXTEN},qdMTr)
  same => n,Hangup()
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FCtynyaFRzGrIsR7GX2yj%2FcodeForExtensionsConf.png?alt=media&#x26;token=2b0579e4-728b-47a6-a6f5-0390a2becb6b" alt=""><figcaption><p>Code for Extensions.conf</p></figcaption></figure>

{% hint style="info" %}
When calling the conference number **354233**, an external subscriber with the number **37127776675** will automatically connect. As soon as the subscriber answers, the extension number **354233** will be dialed. As soon as all the "internal" participants have left the conference, the external subscriber will be disconnected.
{% endhint %}

{% hint style="info" %}
The task in the crown is needed only for additional "protection", in fact it is a restart of the php script every minute
{% endhint %}
