📜 ⬆️ ⬇️

Free library for integration of 1C and Asterisk

Faced the task of integrating 1C and Asterisk. It immediately became clear that Asterisk gives an API for access from outside ( Asterisk Management Interface , abbreviated AMI) - they say, connect from any application and work. What, in fact, is actively used by software developers when integrating with Asterisk.

But with 1C there was a problem ... There was no good and most importantly a free library for connecting 1C and Asterisk. I wanted to correct this situation. Therefore, a free DLL library was written (in terms of 1C - “external component”) for the connection between 1C and Asterisk.

The article will be useful primarily for 1C programmers. It will be a question of library and some aspects of integration 1C and Asterisk.
')

About the library


The ROM-Asterisk library is free. Implements an asynchronous, non-blocking event-based socket. The 1C: Enterprise platform imposes certain requirements when developing external libraries, therefore, the DLL is written in strict accordance with the 1C recommendations. From the features of the library, I would like to note the implementation of filters based on regular expressions inside it.

AMI event filtering features


In Asterisk, only since version 10, the possibility of flexible filtering of AMI events appeared. Prior to version 10, the AMI client is forced to handle an excess amount of events.

I will give an example of an AMI package for an incoming call, to make it clearer:

Event: Dial
Privilege: call,all
SubEvent: Begin
Channel: SIP/202-0000019c
Destination: SIP/200-0000019d
CallerIDNum: 202
CallerIDName: Operator202
ConnectedLineNum: 200
ConnectedLineName: Operator200
UniqueID: 1335616897.790
DestUniqueID: 1335616897.791
Dialstring: 200


Using Asterisk, you can set a filter for Privilege privilege classes: call, dialplan, agent, etc. But, you cannot limit a client to a subscription only for certain types of events within the privilege class - Event: Dial, Bridge, Hangup, etc. And even more so, you can not impose a filter on the value of the event field - ConnectedLineNum: 200.

He wanted Caesar - Caesar.
Suppose a user session 1C needs to track the status of his desktop IP phone / softphone with an internal number of 200 and in the case of an incoming call, open the “Client Card” form.
At the same time, the 1C session will receive absolutely all the “Event: Dial” events that occurred in Asterisk. Then, he should analyze them all and select only those that refer to number 200. Lots of useless work!

Filtering events on the library side


To save the 1C user session from receiving unnecessary events, on the library side, event filtering methods are implemented. I didn’t want to reinvent the bicycle, therefore, the regular expression mechanism based on PCRE was used

Example filter: Dial. {1,}? 200 | Bridge. {1,}? 200 | Hangup. {1,}? 200

Now, the 1C session will receive inside the privileged class call, only Dial, Bridge, Hangup events, and only those that have the number 200.

Setup by Asterisk


Nothing complicated. In the case of using pure Asterisk, you need to edit the file /etc/asterisk/manager.conf

[general]
enabled=yes ; AMI (- no)
port=5038 ; TCP 5038
bindaddr=192.168.1.0 ; . (0.0.0.0 - )
timestampevents=no ;
displayconnects=yes ; AMI
allowmultiplelogin=yes ;


Section that is responsible for the user:

[user1c] ;
secret=passwd1234 ; AMI
deny=0.0.0.0/0.0.0.0 ; ip-
permit=192.168.1.0/255.255.255.0 ;
read=system,call,command,agent ;
write=system,call,command,agent ;


In the case of using FreePBX version 2.9 and higher, the configuration is performed via the web interface.

Use in 1C: Enterprise


The library will work in 1C versions 7.7, 8.1, 8.2. The only limitation is to run in web client mode in version 8.2. This is a necessary measure - I had to sacrifice the web client 8.2 for the sake of compatibility with version 7.7.

Downloading is performed using standard 1C methods: Download External Component or Connect External Component.

Events received by the external component of ROM-Asterisk in the 1C environment will be available in a predefined procedure for processing an External Event.

Component properties:

Connected / - (read) connection status to Asterisk
Filter / Filter - (read) the usual string filter to find the occurrence of a substring
RegularExpression / RegEx - (reading) filter, based on regular expression
Listen / Listen - (reading) Asterisk AMI event listening mode, 1-on, 0-no
Version / Version - (read) version of external component

Methods components:

Connect / Connect (IP, Port) - establish a connection with Asterisk AMI
Disconnect / Disconnect () - disconnect from the Asterisk server
ExecuteCommand / SendCommand (LineCommand) - execute arbitrary Asterisk AMI command
Listening Mode / ListenMode (Flag) - enable / disable listening to Asterisk AMI event mode, 1-enable, 0-disable
SetFilter (SetFilter) - set the normal occurrence of a substring
SetRegularExpression / SetRegEx (LineFilter) - set the filter on the basis of a regular expression

Links


Ready example for version 8.2

Description of properties and methods

Good integration!

Source: https://habr.com/ru/post/143085/


All Articles