Print
Category: FreeSwitch
Hits: 640

โมดูล mod_xml_curl ทำให้ freeswitch server สามารถดึงคอนฟิกไฟล์จากที่เก็บไว้บน web server มาใช้งานได้ทันทีทันใด โดยไม่ต้อง restart freeswitch server หรือ reloadxml   โมดูล mod_xml_curl สามารถทำงานกับคอนฟิกต่างๆ ดังนี้

บทความในตอนนี้ เราจะคอนฟิกเฉพาะส่วน directory เท่านั้น โดยจะใช้ freeswitch-1.10.x บน Debian-11.x ที่เราติดตั้งในบทความตอนที่ 7 ที่ผ่านมา

 

1. เปิดใช้งาน mod_xml_curl   ไปที่ folder /etc/freeswitch/autoload_configs

edit ไฟล์ modules.conf.xml   เปิดใช้งาน module โดยการตัด comment ด้านหน้า (<!--) และ comment ด้านหลัง (-->) ออก เหลือดังนี้

<load module="mod_xml_curl"/>

 

2. คอนฟิกโมดูล mod_xml_curl   ไปที่ folder /etc/freeswitch/autoload_configs 

edit ไฟล์ xml_curl.conf.xml (ให้ดึงคอนฟิกจาก local web server)

======================================================================

<configuration name="xml_curl.conf" description="cURL XML Gateway">

  <bindings>

       <binding name="directory">

             <param name="method" value="GET"/>

             <param name="gateway-url" value="http://localhost/xml_curl/get_directory.php" bindings="directory"/>                 

       </binding>

  </bindings>

</configuration>

=====================================================================

แล้ว restart freeswitch

#systemctl restart freeswitch

 

3. ติดตั้ง web server, php (บน server เดียวกับ fressswitch)

#apt install apache2 libapache2-mod-php7.4 php7.4-fpm

#mkdir /var/www/html/xml_curl

edit ไฟล์ /var/www/html/xml_curl/get_directory.php ดังนี้

=====================================================================

<?php

 

    $users = [

        [

            'number' => '2000',

            'password' => 'ASDF2000',

            'name' => 'User 2000',

            'myVar' => '1234',

        ],

        [

            'number' => '2001',

            'password' => 'ASDF2001',

            'name' => 'User 2001',

            'myVar' => '1234',

        ],

        [

            'number' => '2002',

            'password' => 'ASDF2002',

            'name' => 'User 2002',

            'myVar' => '1234',

        ],

        [

            'number' => '2003',

            'password' => 'ASDF2003',

            'name' => 'User 2003',

            'myVar' => '1234',

        ],

        [

            'number' => '2004',

            'password' => 'ASDF2004',

            'name' => 'User 2004',

            'myVar' => '1234',

        ]

    ];

 

?>

 

<document type="freeswitch/xml">

    <section name="directory">

        <domain name="192.168.1.54">

            <params>

                <param name="dial-string" value="{presence_id=${dialed_user}@${dialed_domain}}${sofia_contact(${dialed_user}@${dialed_domain})}"/>

            </params>

            <groups>

                <group name="default">

                    <users>

                        <?php

                            foreach($users as $user) {

                        ?>

                        <user id="<?php echo $user['number']; ?>">

                            <params>

                                <param name="password" value="<?php echo $user['password']; ?>"/>

                            </params>

                            <variables>

                                <variable name="accountcode" value="<?php echo $user['number']; ?>"/>

                                <variable name="user_context" value="default"/>

                                <variable name="effective_caller_id_name" value="<?php echo $user['name']; ?>"/>

                                <variable name="effective_caller_id_number" value="<?php echo $user['number']; ?>"/>

                                <variable name="outbound_caller_id_name" value="<?php echo $user['name']; ?>"/>

                                <variable name="outbound_caller_id_number" value="<?php echo $user['number']; ?>"/>

                                <variable name="mySuperVariable" value="<?php echo $user['myVar']; ?>" />

                            </variables>

                        </user>

                        <?php } ?>

                    </users>

                </group>

            </groups>

        </domain>

    </section>

</document>

====================================================================

 

4. แก้ไข dialplan 

แก้ไข dialplan เพื่อรองรับ extensions กลุ่มใหม่ ที่เราเพิ่มผ่าน web server (ไฟล์ get_directory.php)

edit ไฟล์ /etc/freeswitch/dialplan/default.xml

แก้ส่วน Local_Extension

จาก "^(10[01][0-9])$"

เป็น "^(2[0-9][0-9][0-9])$"

แล้ว reloadxml (เข้า fs_cli แล้ว relaodxml)

หลังจากนั้นเราสามารถเพิ่ม extension 2000 ถึง 2999 ได้ทันทีทันใดโดยไม่ต้อง restart freeswitch หรือ reloadxml เลย

 

5. ทดสอบ 

คอนฟิก IP phone 2 ตัว แล้วลองโทรหากัน

 

6. เชื่อมต่อกับ database

เราจะเห็นว่าไฟล์ get_directory.php นั้นสามารถเขียนโปรแกรมเพิ่มเติมให้ดึงข้อมูล users จาก database ได้ ซึ่งจะมีประโยชน์มากในการจัดการระบบที่มี users จำนวนมากๆ แล้วเราจะทำในตอนต่อๆ ไป