使用Asterisk manager API實現自動撥號

原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://strugglelinux.blog.51cto.com/1009905/609161

最近在做公司的呼叫中心,一時心血來潮想實現自動呼叫功能,現在只做了個示例,用到的時候再擴展。

在實現自動呼叫的示例中我使用的是Asterisk manager API中的Originate方法,該方法在phpagi的中。具體定義如下:(英文我就不解釋了,我英文很水

  1. /**
  2. * Originate Call
  3. *
  4. * @link http://www.voip-info.org/wiki-Asterisk+Manager+API+Action+Originate
  5. * @param string $channel Channel name to call
  6. * @param string $exten Extension to use (requires 'Context' and 'Priority')
  7. * @param string $context Context to use (requires 'Exten' and 'Priority')
  8. * @param string $priority Priority to use (requires 'Exten' and 'Context')
  9. * @param string $application Application to use
  10. * @param string $data Data to use (requires 'Application')
  11. * @param integer $timeout How long to wait for call to be answered (in ms)
  12. * @param string $callerid Caller ID to be set on the outgoing channel
  13. * @param string $variable Channel variable to set (VAR1=value1|VAR2=value2)
  14. * @param string $account Account code
  15. * @param boolean $async true fast origination
  16. * @param string $actionid message matching variable
  17. */
  18. function Originate($channel,
  19. $exten=NULL, $context=NULL, $priority=NULL,
  20. $application=NULL, $data=NULL,
  21. $timeout=NULL, $callerid=NULL, $variable=NULL, $account=NULL, $async=NULL, $actionid=NULL)
  22. {
  23. $parameters = array('Channel'=>$channel);
  24. if($exten) $parameters['Exten'] = $exten;
  25. if($context) $parameters['Context'] = $context;
  26. if($priority) $parameters['Priority'] = $priority;
  27. if($application) $parameters['Application'] = $application;
  28. if($data) $parameters['Data'] = $data;
  29. if($timeout) $parameters['Timeout'] = $timeout;
  30. if($callerid) $parameters['CallerID'] = $callerid;
  31. if($variable) $parameters['Variable'] = $variable;
  32. if($account) $parameters['Account'] = $account;
  33. if(!is_null($async)) $parameters['Async'] = ($async) ? 'true' : 'false';
  34. if($actionid) $parameters['ActionID'] = $actionid;
  35. return $this->send_request('Originate', $parameters);
  36. }

下面是服務端的簡單代碼(很簡單我只是實現執行該文件自動撥號,對方接聽後會聽到 hello world 的語音)

ami.php

  1. #!/usr/bin/php -q
  2. <?php
  3. include "phpagi-asmanager.php";
  4. $ams = new AGI_AsteriskManager();
  5. $ams->AGI_AsteriskManager("ami.conf");
  6. $result = $ams->connect();
  7. $res = $ams->Originate('sip/8001','8001','from-internal','1','Playback','hello-world',30000,'192.168.1.112');
  8. var_dump($res); //這個是查看輸出信息的調試代碼
  9. ?>

ami.conf文件是訪問API的驗證文件:內容如下:

  1. [asmanager]
  2. server=127.0.0.1 ; server to connect to
  3. port=5038 ; default manager port
  4. username=admin ; username for login
  5. secret=123456 ; password for login

該文件的內容要和asterisk內的/etc/asterisk/manager.conf 文件中的用戶密碼相同 ,以上兩個文件我是放在 /var/spool/asterisk/outgoing/ 目錄中的

(要給執行的權限)

以上編寫完成之後在服務器上直接運行就可以呼叫撥號了:

執行./ami.php

各位看官可以自己擴展一下,比如通過訪問網址來傳遞呼叫參數!或者通過某些程序自動執行該程序!我只想把這個用到服務器監控上面,不用再使用舊的短信提示,如果宕機就直接撥負責人的電話,接通之後自動撥放錄音,還要循環播放,讓他不知道都難!

本文出自 “我菜故我在” 博客,請務必保留此出處http://strugglelinux.blog.51cto.com/1009905/609161

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章