> For the complete documentation index, see [llms.txt](https://prosbcdocs.telcobridges.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://prosbcdocs.telcobridges.com/telecom-references/apis-and-integration/routing-script-tutorial.md).

# Routing script tutorial

[Hide TOC](/telecom-references/apis-and-integration/routing-script-tutorial.md)

### Introduction

Working with scripts requires you to define a class that will help you select one of your previously created routes. There are 3 mandatory things to define for your script to work:

1. Define your new script routing class, which can contain one or many of these methods (route\_order,route\_match,route\_remap)
2. The 'init\_routes' method
3. The 'route' method

| ![NoteIcon](https://docs.telcobridges.com/w/images/b/b6/NoteIcon.png) | For more information about the parameters that can be used within a method, consult the [Routing Script Tutorial: A mini development guide](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/routing-script-tutorial-development-guide/README.md) |
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

### Script routing class

The scripting class is used to define on which call/NAP parameters the call needs to match the route or to remap the call parameter. You also need to define all new methods required by your script routing class. It is highly recommended to derive your new class for the 'base\_routing' class, because it provides you with more functionality.

### What I need to define a script routing class

There are 3 methods that can be used:

1. route\_order
2. route\_match
3. route\_remap

Please note that 'route\_order' will be called before 'route\_match', both of which will be called before 'route\_remap'.

#### 'route\_order' method

'route\_order' allows to order routes using one of 3 possible arguments. It is only possible to call 'route\_order' once.

1. &#x20;:route\_field\_name - The field name of the **route** to order with. The value of the route field should be an **integer** so that it can be compared.
2. &#x20;:proc - A user-supplied **proc** to call instead of trying to order internally. It should accept two arguments (route list, nap list) and return the sorted route list.
3. &#x20;:method - A user-supplied **method** to call instead of trying to order internally. It should accept two arguments (route list, nap list) and return the sorted route list.

base\_routing pre-implemented ordering method:

* 'order\_by\_asr', this method will order the routes according to the [ASR](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/concepts/asr/README.md) (answer seizure ratio). This method requires adding a custom **NAP** parameter called ':asr\_type'. Its value can be: global, last\_24h, current\_hour, last\_hour. It can be used in your custom class like this:

  route\_order :method => :order\_by\_asr

#### 'route\_match' method

'route\_match' allows to match a call to one route using one of 4 possible arguments. It is possible to call 'route\_match' multiple times to reduce the number of matching routes. Since 'route\_match' will return the first matching route it find, there is a possibility that there is more than one route match, therefore it is important to order the routes or prioritize them using 'route\_order'.

**:call\_field\_name**

This option will compare the specified call attribute with the route attribute. For example, :call\_field\_name => :called will match the called number attribute.

* A route that has empty value is considered "match everything".
* A route that has a specific value will only match if the call has the same value
* A route with a regular expression value will match if the regular expression from the route matches the value from the call
  * For example: /555000./ will match any number with 7 digits starting with 555000
* This function supports a special case when matching SIP :calling or :called attributes: The format 'number\@sip\_host:sip\_ort' can be used for matching calling/called number, the domain, and the port.
  * For example: <1234@abc.com>:5060 will match only calls with number 1234, on SIP host 'abc.com' on port 5060.
  * For example: @abc.com will match any call on sip\_host 'abc.com' on any port

**:route\_field\_name**

This option will specify an alternate route attribute name to match with the call attribute :call\_field\_name (in case the route attribute name differs from the call attribute name)

**:proc**

A user-supplied proc to call instead of trying to do the match internally. It should accept three arguments (route, call, nap list) and return a boolean to indicate the match

**:method**

A user-supplied method to call instead of trying to do the match internally. It should accept three arguments (route, call, nap list) and return a boolean to indicate the match

**base\_routing pre-implemented matching method**

* 'match\_nap\_availability', this method will verify the availability through the nap status.

  route\_match :method => :match\_nap\_availability
* 'match\_asr\_threshhold', this method will verify will match any route who's destination NAP has a higher ASR than the threshold stored for that nap. This method requires adding 2 custom **NAP** parameters called. The first custom parameter is 'asr\_threshhold\_type'. Its values can be: global, last\_24h, current\_hour, last\_hour. The second custom parameter is 'asr\_threshhold'. Its value needs to be an **integer** between 0 and 100.

  route\_match :method => :match\_asr\_threshhold

#### 'route\_remap' method

'route\_remap' allows to remap the parameter of the route or the call using one of the 4 possible arguments. There are actually 3 types of remapping that can be performed: via the :call\_field\_name/:route\_field\_name arguments; the :proc argument; and/or the :method argument.

**:call\_field\_name**

```
The field name of the call to remap
```

**:route\_field\_name**

The field name of the route to remap with. The default is to use the call field name. If the value of the field is empty, the incoming call's attribute is used. The value of the route field can be a regular expression remap. (e.g., /(555000.)/001\1/ )

**:proc**

A user supplied proc to call instead of trying to do the remap internally. It should accept four arguments (route, call object, nap list, call params) and return a hash of remapped fields

**:method**

A user supplied method to call instead of trying to do the match internally. It should accept four arguments (route, call object, nap list, call params) and return a hash of remapped fields

#### 'init\_routes' method

The 'init\_routes' is a mandatory method that is call every time the script is loaded (i.e., think loading the configuration). It may be to your advantage to perform some pre-processing such as ordering your routes; that way the routes will not be re-ordered at every call that comes in.

#### 'route' method

The 'route' is a mandatory method that is call at every call that comes in. This is where you can perform the dynamic routing part.

### Route retry algorithm

Documentation on route retry algorithm can be found here: [Route\_retry](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/concepts/route-retry/README.md)

### What else?

There are numerous possibilities:

* Add new methods for route selection
* Add new columns in the NAP or route
* Create scripts that include other scripts to help make the routing more modular

#### Examples and tutorials

* [Routing Script class tutorial](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/concepts/routing-class-tutorial/README.md)
* ['route\_order' tutorial](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/route-order-tutorial/README.md)
* ['route\_match' tutorial](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/route-match-tutorial/README.md)
* ['route\_remap' tutorial](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/api/route-remap-tutorial/README.md)
* Pre-built routing scripts are also available through the Web Portal in the script routing menu

***

Back to the [Scriptable Routing Engine](https://github.com/telcobridges-main/tmedia-wiki/tree/main/reference/concepts/scriptable-routing-engine/README.md) page.

### Useful links

* [Tmedia Routing](https://github.com/telcobridges-main/tmedia-wiki/tree/main/tmedia/platforms/tmedia-routing/README.md)


---

# 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 dynamically by asking a question.

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

```
GET https://prosbcdocs.telcobridges.com/telecom-references/apis-and-integration/routing-script-tutorial.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
