> 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/faq/incoming-routing/black-and-white-lists.md).

# Черные и белые списки

Периодически каждая компания сталкивается с проблемой поступления «спам» звонков. В связи с этим возникает потребность блокировки определенных номеров, чтобы звонки с этих номеров не поступали.

## Продвинутый вариант

1. Создайте новое **«Приложение диалплан»**
2. Укажите произвольное название, к примеру **Blacklist**
3. Выберите «**Тип кода**» - «**PHP AGI Скрипт**»
4. Укажите короткий номер для приложения, к примеру 99
5. На вкладке «**Программный код**» вставьте:

   ```php
   <?php
   require_once('Globals.php');
   use MikoPBX\Core\Asterisk\AGI;

   $agi        = new AGI();
   $isTransfer = !empty($agi->get_variable('ISTRANSFER', true));
   $number     = substr($agi->request['agi_callerid'], -10);
   $cliNumber  = isset($argv[2]) && !empty($argv[2]) ? substr($argv[2], -10) : $number;
   $actionFlag = $isTransfer ? 1 : (isset($argv[1]) ? intval($argv[1]) : null);

   $agi->verbose('Action flag: ' . $actionFlag . ', Phone number: ' . $cliNumber, 1);
   $filename = '/storage/usbdisk1/mikopbx/custom_modules/blacklist.txt';
   if(file_exists($filename)){
       $blacklist = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
       $isInBlacklist = in_array($cliNumber, $blacklist);
   }else{
       $isInBlacklist = false;
   }

   if (is_null($actionFlag) || is_null($cliNumber)) {
       if($isInBlacklist){
           $agi->verbose('The number was found in the blacklist. The call will be completed');
           $agi->hangup();
       }
       exit(0);
   }

   if ($actionFlag === 1) {
       if (!$isInBlacklist) {
           $agi->verbose('Adding number to black list (fileWriteContent): ' . $cliNumber, 1);
           $blacklist[] = $cliNumber;
           \MikoPBX\Core\System\Util::fileWriteContent($filename, implode(PHP_EOL, $blacklist) . PHP_EOL);
       } else {
           $agi->verbose('Number already exists in the black list: ' . $cliNumber, 1);
       }
       $filename = '/storage/usbdisk1/mikopbx/custom_modules/blacklist_add.wav';
       if(file_exists("$filename")){
           $agi->exec('Playback', \MikoPBX\Core\System\Util::trimExtensionForFile($filename));
       }
   } elseif ($actionFlag === 0) {
       if ($isInBlacklist) {
           $agi->verbose('Removing number from black list (fileWriteContent): ' . $cliNumber, 1);
           $blacklist = array_filter($blacklist, function($blacklistedNumber) use ($cliNumber) {
               return $blacklistedNumber !== $cliNumber;
           });
           \MikoPBX\Core\System\Util::fileWriteContent($filename, implode(PHP_EOL, $blacklist) . PHP_EOL);
       } else {
           $agi->verbose('Number not found in black list: ' . $cliNumber, 1);
       }
       $filename = '/storage/usbdisk1/mikopbx/custom_modules/blacklist_remove.wav';
       if(file_exists("$filename")){
           $agi->exec('Playback', \MikoPBX\Core\System\Util::trimExtensionForFile($filename));
       }
   } else {
       $agi->verbose('Invalid action flag: ' . $actionFlag . '. Use 1 to add, 0 to remove.', 1);
       exit(1);
   }
   $agi->verbose('Operation completed successfully.', 1);
   ```
6. Сохраните изменения и скопируйте идентификатор приложения из адресной строки браузера, в этом примере "`DIALPLAN-APP-1C34052A"`.
7. Перейдите в раздел **Система** → **Кастомизация системных файлов**, откройте для редактирования файл **extensions.conf**
8. Выберите режим **«Добавлять в конец файла»**, внизу в черное поле для редактирования вставьте следующие строки:

   ```php
   [internal-transfer](+)
   exten => _99,1,Set(__ISTRANSFER=transfer_)
    	same => n,Goto(internal,${EXTEN},1)
    
   [applications](+)
   exten => _1*X.,1,AGI(DIALPLAN-APP-1C34052A.php,1,${EXTEN:3})
   exten => _0*X.,1,AGI(DIALPLAN-APP-1C34052A.php,0,${EXTEN:3})	
    	
   [add-trim-prefix-clid-custom]
   exten => _[0-9*#+a-zA-Z][0-9*#+a-zA-Z]!,1,NoOp(start check blacklist)
   	same => n,AGI(DIALPLAN-APP-1C34052A.php)
   	same => n,Return()
   ```

   , где «**`DIALPLAN-APP-1C34052A`**» - это идентификатор приложения, найти можно в адресной строке браузера при редактирвоании приложения
9. Сохраните изменения!

{% hint style="info" %}
Черный список телефонов будет храниться в файле:\
«**`/storage/usbdisk1/mikopbx/custom_modules/balcklist.txt`**»

Также можно на АТС загрузить файлы оповещения о добавлении в черный список:

`/storage/usbdisk1/mikopbx/custom_modules/blacklist_add.wav`

`/storage/usbdisk1/mikopbx/custom_modules/blacklist_remove.wav`\
\
Для добавления номера в черный список просто **выполните переадресацию на номер 99** (слепую, без консультаци&#x438;**)**\
\
**Наберите** `1*74950000300` **для добавления номера в черный список**\
**Наберите** `0*74950000300` для удаления номера из черного списка
{% endhint %}

## Упрощенная реализация

**Подойдет для старых версий MikoPBX**

1. Перейдите в раздел **Система** → **Кастомизация системных файлов**.

<figure><img src="https://3704471835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPK4TuzRBnP7rt8htho-887967055%2Fuploads%2FmRoCXGlTdLZsCYDSh3T2%2FcustomizationFiles.png?alt=media&amp;token=ec18b151-274a-4a56-9e51-17f693db0bb7" alt=""><figcaption><p>Раздел "Кастомизация системных файлов"</p></figcaption></figure>

2. Откройте для редактирования конфигурационный файл **extensions.conf**.

<figure><img src="https://3704471835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPK4TuzRBnP7rt8htho-887967055%2Fuploads%2Fgit-blob-0a1dd07707eb967d811042c18a85b2c567eb1916%2FextensionsConf%20(1).png?alt=media" alt=""><figcaption><p>Редактирование конфигурационного файла "Extensions.conf"</p></figcaption></figure>

3. Установите режим "Добавлять в конец файла" и вставьте следующий код:

```php
[add-trim-prefix-clid-custom]
exten => _X!,1,NoOp(...)
    ; Черный список номеров. Вызов будет завершен. 
    same => n,ExecIf($["${CALLERID(num)}" == "72942293042"]?Hangup())
    same => n,ExecIf($["${CALLERID(num)}" == "72942293043"]?Hangup())
    same => n,ExecIf($["${CALLERID(num)}" == "72942293044"]?Hangup())
    same => n,return
```

<figure><img src="https://3704471835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPK4TuzRBnP7rt8htho-887967055%2Fuploads%2FltzFOCX2ip9UWXw0GsE7%2FCodeForExtensions.png?alt=media&amp;token=2ba00fde-a3eb-4c26-b8df-cacd78194fdb" alt=""><figcaption><p>Код для файла "extnsions.conf"</p></figcaption></figure>

Белый список номеров иногда требуется описать для конкретных провайдеров:

```php
[ID-ПРОВАЙДЕРА-incoming-custom]
exten => _X!,1,NoOp(...)
    ; Белый список номеров. 
    same => n,ExecIf($["${CALLERID(num)}" == "72942291111"]?return)
    same => n,ExecIf($["${CALLERID(num)}" == "72942291112"]?return)
    same => n,ExecIf($["${CALLERID(num)}" == "72942291113"]?return)
    same => n,Hangup()
```

<figure><img src="https://3704471835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPK4TuzRBnP7rt8htho-887967055%2Fuploads%2F4Pgam0oSFZgfUup0Ge3K%2FCodeForExtensions2.png?alt=media&amp;token=2fcf5575-0cfd-4756-b3cc-092b35d52bcf" alt=""><figcaption></figcaption></figure>

* **ID-ПРОВАЙДЕРА** - значение, которое вы можете найти в адресной строке в момент конфигурации провайдера

<figure><img src="https://3704471835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MPK4TuzRBnP7rt8htho-887967055%2Fuploads%2FYjP21diTWeV0LJbGvgoy%2FProviderID.png?alt=media&amp;token=d5953dd8-6dfa-4c45-a1d0-b1f82041440d" alt=""><figcaption><p>Как найти ID провайдера:</p></figcaption></figure>


---

# 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/faq/incoming-routing/black-and-white-lists.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.
