Agi for periodical announcement and log in background

Hi,
please guide related to periodical announcement and log in background(for every caller).
My requirement is all caller with receive a small announcement and add entry to log file in every 20 minutes till they are in system. I am not agi person but tried with different settings. With this following code system stuck with agi.


Announcement.php
#!/usr/bin/php
<?php
// require('phpagi/phpagi.php');
error_reporting (E_ALL ^ E_NOTICE);
set_time_limit(30);
include_once("/var/lib/asterisk/phpagi-2.20/phpagi.php");
$agi = new AGI();
$callerId = $agi->request['agi_callerid']; // Get the caller ID
$logDirectory = '/var/log/asterisk/test-logs/'; // Replace with your log directory
$logFileName = $logDirectory . date('Y-m-d_H-i-s') . '_' . $callerId . '.txt';
$agi->answer(); // Answer the incoming call
$pid = pcntl_fork(); // Fork a child process
if ($pid == -1) {
    die("Could not fork.\n");
} elseif ($pid) {
    // Parent process
    exit(0);
} else {
while (true) {
    $timestamp = date('Y-m-d H:i:s');
//    $agi->stream_file('silence/1'); // Insert silence in logs
    $agi->stream_file('beep'); // Insert a beep in logs
    $logEntry = "$timestamp - Call from $callerId\n";
    file_put_contents($logFileName, $logEntry, FILE_APPEND);

    sleep(1200); // Sleep for 10 minutes (600 seconds)
}}
?>

Thanks @david551 for making it appropriate.
I hope you understand my requirement.

What happens when you run this?

A console log with ‘agi set debug on’ and wrapped in preformatted text tags may yield clues.

This will not do what the comment says.

It just stuck when I run

Executing [s@QuizIVRHHIN:11] AGI(“SIP/8081-00000006”, “Announcement2.php”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/Announcement2.php
– <SIP/8081-00000006> Playing ‘beep.ulaw’ (escape_digits=) (sample_offset 0) (language ‘en’)
[Nov 13 21:54:43] WARNING[19737]: chan_sip.c:4422 __sip_autodestruct: Autodestruct on dialog ‘ea2efb36de032252OTA3YmI1MWY3MDU3OTdkYTBhNmVhODc4NTUxYTY0MWM.’ with owner SIP/8081-00000002 in place (Method: BYE). Rescheduling destruction for 10000 ms
[Nov 13 21:54:50] WARNING[19737]: chan_sip.c:4422 __sip_autodestruct: Autodestruct on dialog ‘ea2efb36de032252OTA3YmI1MWY3MDU3OTdkYTBhNmVhODc4NTUxYTY0MWM.’ with owner SIP/8081-00000002 in place (Method: BYE). Rescheduling destruction for 10000 ms

Executing [s@QuizIVRHHIN:11] AGI(“SIP/8081-00000007”, “Announcement2.php”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/Announcement2.php
<SIP/8081-00000007>AGI Tx >> agi_request: Announcement2.php
<SIP/8081-00000007>AGI Tx >> agi_channel: SIP/8081-00000007
<SIP/8081-00000007>AGI Tx >> agi_language: en
<SIP/8081-00000007>AGI Tx >> agi_type: SIP
<SIP/8081-00000007>AGI Tx >> agi_uniqueid: 1699892785.12
<SIP/8081-00000007>AGI Tx >> agi_version: 20.2.0
<SIP/8081-00000007>AGI Tx >> agi_callerid: 9990494388
<SIP/8081-00000007>AGI Tx >> agi_calleridname: 8081
<SIP/8081-00000007>AGI Tx >> agi_callingpres: 0
<SIP/8081-00000007>AGI Tx >> agi_callingani2: 0
<SIP/8081-00000007>AGI Tx >> agi_callington: 0
<SIP/8081-00000007>AGI Tx >> agi_callingtns: 0
<SIP/8081-00000007>AGI Tx >> agi_dnid: 195
<SIP/8081-00000007>AGI Tx >> agi_rdnis: unknown
<SIP/8081-00000007>AGI Tx >> agi_context: QuizIVRHHIN
<SIP/8081-00000007>AGI Tx >> agi_extension: s
<SIP/8081-00000007>AGI Tx >> agi_priority: 11
<SIP/8081-00000007>AGI Tx >> agi_enhanced: 0.0
<SIP/8081-00000007>AGI Tx >> agi_accountcode: 8081
<SIP/8081-00000007>AGI Tx >> agi_threadid: 140108052268800
<SIP/8081-00000007>AGI Tx >>
<SIP/8081-00000007>AGI Rx << ANSWER
<SIP/8081-00000007>AGI Tx >> 200 result=0
<SIP/8081-00000007>AGI Rx << STREAM FILE beep “” 0
– <SIP/8081-00000007> Playing ‘beep.ulaw’ (escape_digits=) (sample_offset 0) (language ‘en’)
<SIP/8081-00000007>AGI Tx >> 200 result=0 endpos=3404
[Nov 13 21:56:30] WARNING[19737]: chan_sip.c:4422 __sip_autodestruct: Autodestruct on dialog ‘ea2efb36de032252OTA3Ym I1MWY3MDU3OTdkYTBhNmVhODc4NTUxYTY0MWM.’ with owner SIP/8081-00000002 in place (Method: BYE). Rescheduling destructio n for 10000 ms
[Nov 13 21:56:31] WARNING[19737]: chan_sip.c:4422 __sip_autodestruct: Autodestruct on dialog ‘4975181b772e4a23OTA3Ym I1MWY3MDU3OTdkYTBhNmVhODc4NTUxYTY0MWM.’ with owner SIP/8081-00000006 in place (Method: BYE). Rescheduling destructio n for 10000 ms
<SIP/8081-00000002>AGI Rx << STREAM FILE beep “” 0
<SIP/8081-00000002>AGI Tx >> 511 Command Not Permitted on a dead channel or intercept routine
<SIP/8081-00000006>AGI Rx << STREAM FILE beep “” 0
<SIP/8081-00000006>AGI Tx >> 511 Command Not Permitted on a dead channel or intercept routine
localhost*CLI>

If by ‘stuck’ you mean that your dialplan does not continue execution, it’s because your parent AGI still exists. It probably shows up as <defunct> in ps.

What does this mean?

What will cause the child to exit?

What is the purpose of the log file? Do you just want to know when a caller started execution of the AGI and approximately when the caller received their last ‘beep?’

I suspect other methods of playing a ‘periodic announcement’ may be more appropriate and less problematic.

Thanks for reply.
Yes stuck means dialplan does not continue execution,
My need is like just to maintain a log/attendance report of each user with start/hangup, user may be in any context section, musiconhold section or confbridge section. Sound is not necessary but activity log detail is required. Whether we can use AGi or dialplan/etc. Hope you understand.
At present we doing 15 minutes log with core show channels concise and individual log with system command but looking some good individual log report for every user.

My first Asterisk gig was for an adult chat service. Each context represented a different product with different billing rates. This necessitated writing a row to the database every time a caller changed context (product), including when a caller hung up (or was kicked off), capturing the time spent in the previous context.

Maybe something similar would work for you?

Here’s an odd thought that was not available then…

What if you set a channel variable (‘CURRENT-CONTEXT’) as you entered each context and then set up a PERIODIC_HOOK() to execute every x seconds that wrote the current context to the database (maybe updating a ‘duration’ column if the caller already has a current context row)?

I would not use the phpagi class for this task. Instead, I suggest the traditional and very old method, which involves using chanspy and background with local channels, and utilizing cron (*/20 * * * * ).

I just tried PERIODIC_HOOK

exten => s,n,Set(BEEPID1=${PERIODIC_HOOK(Thooks1,25,10)})
[Thooks1]
exten => 25,1,NoOp(hello)
exten => 25,n,Playback(beep)
exten => 25,n,NoOp(Done)

My required is for periodic log of every channel, which is still pending.
[Thooks1]
exten => 25,1,NoOp(hello)
exten => 25,n,Playback(beep)
exten => 25,n,System(echo -e “,Call-Details = ${CALLER_ID}, ${CHANNEL}, Call Duration =${CDR(duration)}, ContextMenu =${CONTEXT}, Name =${SCORE_PERCENT1} -DateTime-$(date +‘%d-%m-%Y %H:%M:%S’)” >> /root/Log/Log/csvlog1/${CALLER_ID}.csv)
exten => 25,n,NoOp(Done)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.