اطلاعیه عمومی پیرامون خطوط آسیاتک در ادامه

آموزش استفاده ار API الو021 چاپ


برقراری تماس بین داخلی و شماره موبایل از طریق api

استفاده از api در سیستم‌های تلفنی بسیار کاربرد دارد و نرم‌افزارها به صورت 3D-party یعنی نرم‌افزارهای ثالث عموماً علاقه دارند که از طریق api به سیستم‌های تلفنی متصل شوند. معمولاً سی آر ام ها از این قابلیت استفاده می‌کنند و یک تماس بین داخلی مثلاً  ۱۰۱ با یک شماره موبایل مثلاً ۰۹۱۲ برقرار می‌کند. برای این موضوع که شما بتوانید از قابلیت  api استفاده کنید باید حتماً روی سرویس‌های طلایی و نقره‌ای اطلاعات ami را از بخش پشتیبانی دریافت کنید. اطلاعات ami به صورت یک نام کاربری، رمز عبور و آی‌پی هست. این آی پی همون آی پی سرور شماست. در ادامه از طریق قابلیت ami آستریکس به سرور خودتون متصل میشوید و درخواست call originate  بین داخلی ۱۰۱ و شماره موبایل را برقرار می‌کنید. در ادامه نمونه کد php این api آورده شده و شما به سادگی می‌توانید از طریق آن تماس را برقرار کنید.

 

<?php

/**
* Once this scipt is executed it will connect to the local port you have assigned to
* Asterisk (default: 5038) and send an authentication request. If successfull, it will
* send a second request to originate your call.
*
* The internal SIP line $internalPhoneline will be dialed, and when picked up the
* $target phone will be dialed using your outbound calls context ($context).
*
* Of course, you can modify the commands sent to the asterisk manager interface to suit your needs.
* you can find more about the available options at:
*
* http://www.voip-info.org/wiki/view/Asterisk+manager+API
* http://www.voip-info.org/wiki/view/Asterisk+Manager+API+Action+Originate
*
* Hope this helps!
*
* Licence:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

// Replace with your port if not using the default.
// If unsure check /etc/asterisk/manager.conf under [general];
$ip = "10.9.27.116";
$port = 5038;

// Replace with your username. You can find it in /etc/asterisk/manager.conf.
// If unsure look for a user with "originate" permissions, or create one as
// shown at http://www.voip-info.org/wiki/view/Asterisk+config+manager.conf.
$username = "admin";

// Replace with your password (refered to as "secret" in /etc/asterisk/manager.conf)
$password = "e7d03f88fc6622";

// Internal phone line to call from
$internalPhoneline = "101";

// Context for outbound calls. See /etc/asterisk/extensions.conf if unsure.
$context = "from-internal";

$target = '02191010000';

$socket = stream_socket_client("tcp://$ip:$port");
if ($socket) {
echo "Connected to socket, sending authentication request.\n";

// Prepare authentication request
$authenticationRequest = "Action:Login\r\n";
$authenticationRequest .= "Username:$username\r\n";
$authenticationRequest .= "Secret:$password\r\n";
$authenticationRequest .= "Events: off\r\n\r\n";

// Send authentication request
$authenticate = stream_socket_sendto($socket, $authenticationRequest);
if ($authenticate > 0) {
    // Wait for server response
    sleep(1);

    // Read server response
    $authenticateResponse = fread($socket, 4096);

    // Check if authentication was successful
    if (strpos($authenticateResponse, 'Success') !== false) {
        echo "Authenticated to Asterisk Manager Inteface. Initiating call.\n";

        // get extensions
        // $originateRequest = "Action:GetConfig\r\n";
        // $originateRequest .= "Filename:extensions.conf\r\n";
        // $originateRequest .= "Category:default\r\n";
        // $originateRequest .= "\r\n";

        // get extensions
        // $originateRequest = "Action:CoreShowChannels\r\n";
        // $originateRequest .= "Command:ExtensionStateList\r\n";
        // $originateRequest .= "\r\n";


        // Prepare originate request
        $originateRequest = "Action:Originate\r\n";
        $originateRequest .= "Channel:Local/102@from-internal\r\n";
        // $originateRequest .= "Callerid:test\r\n";
        $originateRequest .= "Variable:CALLERID=test\r\n";
        $originateRequest .= "Exten:$target\r\n";
        $originateRequest .= "Context:$context\r\n";
        $originateRequest .= "Priority:1\r\n";
        // $originateRequest .= "Async:yes\r\n";
        $originateRequest .= "\r\n";
//print_r($originateRequest);exit;
        // Send originate request
        $originate = stream_socket_sendto($socket, $originateRequest);
        if ($originate > 0) {
            // Wait for server response
            sleep(1);

            // Read server response
            $originateResponse = fread($socket, 4096);

            die($originateResponse);
            // Check if originate was successful
            if (strpos($originateResponse, 'Success') !== false) {
                echo "Call initiated, dialing.";
            } else {
                echo "Could not initiate call.\n";
            }
        } else {
            echo "Could not write call initiation request to socket.\n";
        }
    } else {
        echo "Could not authenticate to Asterisk Manager Interface.\n";
    }
} else {
    echo "Could not write authentication request to socket.\n";
}
} else {
echo "Unable to connect to socket.";
}

آیا این پاسخ مفید بود؟

  • 0
« بازگشت