> 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/outbound-routing/number-templates/uniform-distribution-of-outgoing.md).

# Uniform distribution of outgoing

1. Go to the **System file customization** section

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2Fn06cIfMIqk6wJhcxD3gW%2FSystemFileCustomization.png?alt=media&amp;token=4edfbfbf-6903-46c5-b1b5-d2a62131f59e" alt=""><figcaption><p>System file customization section</p></figcaption></figure>

2. Open **modules.conf** for editing

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FOOR18KwpzDhecRg5Ek3s%2FmodulesConf.png?alt=media&amp;token=7cd59158-ba08-43dc-82c0-97c47e7a2c29" alt=""><figcaption><p>Modules.conf file</p></figcaption></figure>

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

```php
load => func_global.so
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2FSnMIWqoAV1ggRymypoft%2FCodeForModulesConf.png?alt=media&amp;token=15e8da92-00f1-4f70-b522-8efbaaa34e6d" alt=""><figcaption><p>Code for Modules.conf</p></figcaption></figure>

4. Open the **extensions.conf** file for editing

<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&amp;token=567dc075-00ed-45a7-bc10-915ccc1dc97f" alt=""><figcaption><p>Extensions.conf file</p></figcaption></figure>

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

```php
[all-outgoing-custom]
exten => _X!,1,NoOp()
    same => n,Set(NEED_RETURN=0)
    ; We describe the list of providers
    same => n,Set(PUSH(rrItem)=SIP-1601534775)
    same => n,Set(PUSH(rrItem)=SIP-1611151795)    
    same => n,Set(PUSH(rrItem)=SIP-1681205343)
    ; Maximum number of simultaneous calls over the line
    same => n,Set(busyLevel=50)
    
    ; Checking if there is a provider in the list.
    same => n,ExecIf($[ "${FIELDNUM(rrItem,\,,${PROVIDER_ID})}" == "0" ]?return)
    ; If the ID of the priority provider is determined, then immediately proceed to verification.
    same => n,GotoIf($[ "${rrElement}x" != "x" ]?check)
    ; Initializing an array
    same => n,Set(rrArray=${EMPTY})
    same => n,Set(i=${SHIFT(rrItem)})
    same => n,While(1)
    same => n,ExecIf($[ "${i}x" == "x" ]?ExitWhile())
    same => n,ExecIf($[ "${rrArray}x" != "x" ]?Set(rrArray=${rrArray},))
    same => n,ExecIf($[ "${GLOBAL(${i})}x" == "x" ]?Set(GLOBAL(${i})=0))
    same => n,Set(rrArray=${rrArray}${i}:${GLOBAL(${i})})
    same => n,Set(i=${SHIFT(rrItem)})
    same => n,EndWhile
    ; Finding an item with a lower weight
    same => n,Set(rrArray=${SORT(${rrArray})})
    same => n,Set(rrElement=${SHIFT(rrArray)})
    same => n,While(1)
    same => n,ExecIf($[ $[${GROUP_COUNT(${rrElement})} < ${busyLevel}] ]?ExitWhile())
    same => n,ExecIf($[ "${rrElement}x" == "x" ]?ExitWhile())
    same => n,NoOp( ${rrElement} - is busy)
    same => n,Set(rrElement=${SHIFT(rrArray)})
    same => n,EndWhile
    same => n,NoOp(--- ${rrElement} ---)
    
    ; Checking whether the provider matches
    same => n(check),ExecIf($[ "${PROVIDER_ID}x" != "${rrElement}x" ]?Set(NEED_RETURN=1))
    same => n,ExecIf($[ "${PROVIDER_ID}x" != "${rrElement}x" ]?return)
    ; Increasing the counter
    same => n,Set(GLOBAL(${rrElement})=$[${GLOBAL(${rrElement})} + 1])
    ; The counter of active calls through the provider
    same => n,Set(GROUP()=${rrElement})
    same => n,return
```

<figure><img src="https://835495363-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsZ8acWnNlSalIHQjMFu1%2Fuploads%2F0wL2y903sfozLYr5veQd%2FCodeForExtensionsConf.png?alt=media&amp;token=a73b12a2-7277-49b6-93a2-2093332e375e" alt=""><figcaption><p>Code for extensions.conf</p></figcaption></figure>

{% hint style="info" %}
In the lines of the form "**Set(PUSH(rrItem)=SIP-1601534775)**", the ids of providers are listed, among which outgoing ones should be evenly distributed. The ID can be copied in the browser's address bar when editing the provider. The "**busyLevel**" variable defines the maximum allowed number of simultaneous calls through the provider.
{% endhint %}

{% hint style="warning" %}
For each provider, you will need to describe the outgoing route. The template in the routes must be the same.
{% 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/outbound-routing/number-templates/uniform-distribution-of-outgoing.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.
