|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * PAGI basic use example. Please see run.sh in this same directory for an |
| 4 | + * example of how to actually run this from your dialplan. |
| 5 | + * |
| 6 | + * Note: The client accepts an array with options. The available options are |
| 7 | + * |
| 8 | + * log4php.properties => Optional. If set, should contain the absolute |
| 9 | + * path to the log4php.properties file. |
| 10 | + * |
| 11 | + * stdin => Optional. If set, should contain an already open stream from |
| 12 | + * where the client will read data (useful to make it interact with fastagi |
| 13 | + * servers or even text files to mock stuff when testing). If not set, stdin |
| 14 | + * will be used by the client. |
| 15 | + * |
| 16 | + * stdout => Optional. Same as stdin but for the output of the client. |
| 17 | + * |
| 18 | + * |
| 19 | + * PHP Version 5 |
| 20 | + * |
| 21 | + * @category Pagi |
| 22 | + * @package examples |
| 23 | + * @subpackage quickstart |
| 24 | + * @author Marcelo Gornstein <[email protected]> |
| 25 | + * @license http://www.noneyet.ar/ Apache License 2.0 |
| 26 | + * @version SVN: $Id$ |
| 27 | + * @link http://www.noneyet.ar/ |
| 28 | + * |
| 29 | + * Copyright 2011 Marcelo Gornstein <[email protected]> |
| 30 | + * |
| 31 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 32 | + * you may not use this file except in compliance with the License. |
| 33 | + * You may obtain a copy of the License at |
| 34 | + * |
| 35 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 36 | + * |
| 37 | + * Unless required by applicable law or agreed to in writing, software |
| 38 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 39 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 40 | + * See the License for the specific language governing permissions and |
| 41 | + * limitations under the License. |
| 42 | + * |
| 43 | + */ |
| 44 | +use PAGI\Client\ChannelStatus; |
| 45 | +use PAMI\AsyncAgi\Application\PAGIAsyncApplication; |
| 46 | + |
| 47 | +declare(ticks = 1); |
| 48 | + |
| 49 | +/** |
| 50 | + * PAGI basic use example. Please see run.sh in this same directory for an |
| 51 | + * example of how to actually run this from your dialplan. |
| 52 | + * |
| 53 | + * Note: The client accepts an array with options. The available options are |
| 54 | + * |
| 55 | + * log4php.properties => Optional. If set, should contain the absolute |
| 56 | + * path to the log4php.properties file. |
| 57 | + * |
| 58 | + * stdin => Optional. If set, should contain an already open stream from |
| 59 | + * where the client will read data (useful to make it interact with fastagi |
| 60 | + * servers or even text files to mock stuff when testing). If not set, stdin |
| 61 | + * will be used by the client. |
| 62 | + * |
| 63 | + * stdout => Optional. Same as stdin but for the output of the client. |
| 64 | + * |
| 65 | + * PHP Version 5 |
| 66 | + * |
| 67 | + * @category Pagi |
| 68 | + * @package examples |
| 69 | + * @subpackage quickstart |
| 70 | + * @author Marcelo Gornstein <[email protected]> |
| 71 | + * @license http://www.noneyet.ar/ Apache License 2.0 |
| 72 | + * @link http://www.noneyet.ar/ |
| 73 | + */ |
| 74 | +class MyPAGIApplication extends PAGIAsyncApplication |
| 75 | +{ |
| 76 | + /** |
| 77 | + * (non-PHPdoc) |
| 78 | + * @see \PAGI\Application\PAGIApplication::init() |
| 79 | + */ |
| 80 | + public function init() |
| 81 | + { |
| 82 | + $this->log('Init'); |
| 83 | + $client = $this->getAgi(); |
| 84 | + $client->answer(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Logs to asterisk console. |
| 89 | + * |
| 90 | + * @param string $msg Message to log. |
| 91 | + * |
| 92 | + * @return void |
| 93 | + */ |
| 94 | + public function log($msg) |
| 95 | + { |
| 96 | + $agi = $this->getAgi(); |
| 97 | + $this->logger->debug($msg); |
| 98 | + $agi->consoleLog($msg); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * (non-PHPdoc) |
| 103 | + * @see \PAGI\Application\PAGIApplication::shutdown() |
| 104 | + */ |
| 105 | + public function shutdown() |
| 106 | + { |
| 107 | + try { |
| 108 | + $this->log('Shutdown'); |
| 109 | + $client = $this->getAgi(); |
| 110 | + $client->hangup(); |
| 111 | + } catch (\Exception $e) { |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * (non-PHPdoc) |
| 118 | + * @see \PAGI\Application\PAGIApplication::run() |
| 119 | + */ |
| 120 | + public function run() |
| 121 | + { |
| 122 | + $this->log('Run'); |
| 123 | + $client = $this->getAgi(); |
| 124 | + $loggerFacade = $client->getAsteriskLogger(); |
| 125 | + $loggerFacade->notice('hello notice'); |
| 126 | + $loggerFacade->warn('hello warn'); |
| 127 | + $loggerFacade->debug('hello debug'); |
| 128 | + $loggerFacade->error('hello error'); |
| 129 | + $loggerFacade->dtmf('hello dtmf'); |
| 130 | + //$this->log($client->faxReceive('/tmp/a.tiff')->__toString()); |
| 131 | + //$this->log($client->faxSend('/tmp/a.tiff')->__toString()); |
| 132 | + $variables = $client->getChannelVariables(); |
| 133 | + $this->log('Config dir: ' . $variables->getDirectoryConfig()); |
| 134 | + $this->log('Config file: ' . $variables->getConfigFile()); |
| 135 | + $this->log('Module dir: ' . $variables->getDirectoryModules()); |
| 136 | + $this->log('Spool dir: ' . $variables->getDirectorySpool()); |
| 137 | + $this->log('Monitor dir: ' . $variables->getDirectoryMonitor()); |
| 138 | + $this->log('Var dir: ' . $variables->getDirectoryVar()); |
| 139 | + $this->log('Data dir: ' . $variables->getDirectoryData()); |
| 140 | + $this->log('Log dir: ' . $variables->getDirectoryLog()); |
| 141 | + $this->log('Agi dir: ' . $variables->getDirectoryAgi()); |
| 142 | + $this->log('Key dir: ' . $variables->getDirectoryKey()); |
| 143 | + $this->log('Run dir: ' . $variables->getDirectoryRun()); |
| 144 | + $this->log('Request: ' . $variables->getRequest()); |
| 145 | + $this->log('Channel: ' . $variables->getChannel()); |
| 146 | + $this->log('Language: ' . $variables->getLanguage()); |
| 147 | + $this->log('Type: ' . $variables->getType()); |
| 148 | + $this->log('UniqueId: ' . $variables->getUniqueId()); |
| 149 | + $this->log('Version: ' . $variables->getVersion()); |
| 150 | + $this->log('CallerId: ' . $variables->getCallerId()); |
| 151 | + $this->log('CallerId name: ' . $variables->getCallerIdName()); |
| 152 | + $this->log('CallerId pres: ' . $variables->getCallingPres()); |
| 153 | + $this->log('CallingAni2: ' . $variables->getCallingAni2()); |
| 154 | + $this->log('CallingTon: ' . $variables->getCallingTon()); |
| 155 | + $this->log('CallingTNS: ' . $variables->getCallingTns()); |
| 156 | + $this->log('DNID: ' . $variables->getDNID()); |
| 157 | + $this->log('RDNIS: ' . $variables->getRDNIS()); |
| 158 | + $this->log('Context: ' . $variables->getContext()); |
| 159 | + $this->log('Extension: ' . $variables->getDNIS()); |
| 160 | + $this->log('Priority: ' . $variables->getPriority()); |
| 161 | + $this->log('Enhanced: ' . $variables->getEnhanced()); |
| 162 | + $this->log('AccountCode: ' . $variables->getAccountCode()); |
| 163 | + $this->log('ThreadId: ' . $variables->getThreadId()); |
| 164 | + $this->log('Arguments: ' . intval($variables->getTotalArguments())); |
| 165 | + for ($i = 0; $i < $variables->getTotalArguments(); $i++) { |
| 166 | + $this->log(' -- Argument ' . intval($i) . ': ' . $variables->getArgument($i)); |
| 167 | + } |
| 168 | + |
| 169 | + $result = $client->sayDigits('12345', '12#'); |
| 170 | + if (!$result->isTimeout()) { |
| 171 | + $this->log('Read: ' . $result->getDigits()); |
| 172 | + } else { |
| 173 | + $this->log('Timeouted for say digits.'); |
| 174 | + } |
| 175 | + |
| 176 | + $result = $client->sayNumber('12345', '12#'); |
| 177 | + if (!$result->isTimeout()) { |
| 178 | + $this->log('Read: ' . $result->getDigits()); |
| 179 | + } else { |
| 180 | + $this->log('Timeouted for say number.'); |
| 181 | + } |
| 182 | + |
| 183 | + $result = $client->getData('/var/lib/asterisk/sounds/welcome', 10000, 4); |
| 184 | + if (!$result->isTimeout()) { |
| 185 | + $this->log('Read: ' . $result->getDigits()); |
| 186 | + } else { |
| 187 | + $this->log('Timeouted for get data with: ' . $result->getDigits()); |
| 188 | + } |
| 189 | + |
| 190 | + $result = $client->getOption('/var/lib/asterisk/sounds/welcome', '0123456789*#', 10000); |
| 191 | + if (!$result->isTimeout()) { |
| 192 | + $this->log('Read: ' . $result->getDigits()); |
| 193 | + } else { |
| 194 | + $this->log('Timeouted for get option.'); |
| 195 | + } |
| 196 | + |
| 197 | + $result = $client->streamFile('/var/lib/asterisk/sounds/welcome', '#'); |
| 198 | + if (!$result->isTimeout()) { |
| 199 | + $this->log('Read: ' . $result->getDigits()); |
| 200 | + } else { |
| 201 | + $this->log('Timeouted for stream file.'); |
| 202 | + } |
| 203 | + |
| 204 | + $this->log('Channel status: ' . ChannelStatus::toString($client->channelStatus())); |
| 205 | + $this->log('Channel status: ' . ChannelStatus::toString($client->channelStatus($variables->getChannel()))); |
| 206 | + $this->log('Variable: ' . $client->getVariable('EXTEN')); |
| 207 | + $this->log('FullVariable: ' . $client->getFullVariable('EXTEN')); |
| 208 | + $cdr = $client->getCDR(); |
| 209 | + $this->log('CDRVariable: ' . $cdr->getSource()); |
| 210 | + $cdr->setAccountCode('foo'); |
| 211 | + $this->log('CDRVariable: ' . $cdr->getAccountCode()); |
| 212 | + |
| 213 | + $callerId = $client->getCallerId(); |
| 214 | + $this->log('CallerID: ' . $callerId); |
| 215 | + $callerId->setName('pepe'); |
| 216 | + $this->log('CallerID: ' . $callerId); |
| 217 | + $client->setCallerId('foo', '123123'); |
| 218 | + $this->log('CallerID: ' . $callerId); |
| 219 | + |
| 220 | + //$this->log($client->exec('Dial', array('SIP/sip', 30, 'r'))); |
| 221 | + $this->log($client->dial('SIP/01', array(30, 'r'))); |
| 222 | + |
| 223 | + $result = $client->sayPhonetic('marcelog', '123#'); |
| 224 | + if (!$result->isTimeout()) { |
| 225 | + $this->log('Read: ' . $result->getDigits()); |
| 226 | + } else { |
| 227 | + $this->log('Timeouted for say phonetic.'); |
| 228 | + } |
| 229 | + |
| 230 | + $result = $client->sayAlpha('marcelog', '123#'); |
| 231 | + if (!$result->isTimeout()) { |
| 232 | + $this->log('Read: ' . $result->getDigits()); |
| 233 | + } else { |
| 234 | + $this->log('Timeouted for say alpha.'); |
| 235 | + } |
| 236 | + |
| 237 | + $result = $client->sayTime(time(), '123#'); |
| 238 | + if (!$result->isTimeout()) { |
| 239 | + $this->log('Read: ' . $result->getDigits()); |
| 240 | + } else { |
| 241 | + $this->log('Timeouted for say time.'); |
| 242 | + } |
| 243 | + |
| 244 | + $result = $client->sayDateTime(time(), 'mdYHMS', '123#'); |
| 245 | + if (!$result->isTimeout()) { |
| 246 | + $this->log('Read: ' . $result->getDigits()); |
| 247 | + } else { |
| 248 | + $this->log('Timeouted for say datetime.'); |
| 249 | + } |
| 250 | + |
| 251 | + $result = $client->sayDate(time(), '123#'); |
| 252 | + if (!$result->isTimeout()) { |
| 253 | + $this->log('Read: ' . $result->getDigits()); |
| 254 | + } else { |
| 255 | + $this->log('Timeouted for say date.'); |
| 256 | + } |
| 257 | + |
| 258 | + $client->setPriority(1000); |
| 259 | + $client->setExtension(1000); |
| 260 | + $client->setContext('foo'); |
| 261 | + $client->setMusic(true); |
| 262 | + sleep(10); |
| 263 | + $client->setMusic(false); |
| 264 | + |
| 265 | + $result = $client->waitDigit(10000); |
| 266 | + if (!$result->isTimeout()) { |
| 267 | + $this->log('Read: ' . $result->getDigits()); |
| 268 | + } else { |
| 269 | + $this->log('Timeouted for waitdigit.'); |
| 270 | + } |
| 271 | + $result = $client->record('/tmp/asd', 'wav', '#'); |
| 272 | + if ($result->isInterrupted()) { |
| 273 | + if ($result->isHangup()) { |
| 274 | + $this->log('hangup when recording.'); |
| 275 | + } else { |
| 276 | + $this->log('interrupted with: ' . $result->getDigits()); |
| 277 | + } |
| 278 | + $this->log('Recorded: ' . $result->getEndPos()); |
| 279 | + } |
| 280 | + //$this->log($client->databaseGet('SIP', 'Registry')); |
| 281 | + //$client->setAutoHangup(10); |
| 282 | + //sleep(20); |
| 283 | + } |
| 284 | + |
| 285 | + /** |
| 286 | + * (non-PHPdoc) |
| 287 | + * @see \PAGI\Application\PAGIApplication::errorHandler() |
| 288 | + */ |
| 289 | + public function errorHandler($type, $message, $file, $line) |
| 290 | + { |
| 291 | + $this->log( |
| 292 | + 'ErrorHandler: ' |
| 293 | + . implode(' ', array($type, $message, $file, $line)) |
| 294 | + ); |
| 295 | + } |
| 296 | + |
| 297 | + /** |
| 298 | + * (non-PHPdoc) |
| 299 | + * @see \PAGI\Application\PAGIApplication::signalHandler() |
| 300 | + */ |
| 301 | + public function signalHandler($signal) |
| 302 | + { |
| 303 | + $this->log('SignalHandler got signal: ' . $signal); |
| 304 | + $this->shutdown(); |
| 305 | + exit(0); |
| 306 | + } |
| 307 | +} |
0 commit comments