Implementing balance deduction

I have a MySQL table that has a limit column where total talk time in seconds in given. e.g 1200s
If the calls are being made how do I to it such that it automatically update this column to deduct the balance from the number of seconds used.

I don’t know the function to do this in my extensions.conf but I think there is ${CHANNEL(Duration)} available. After the statement Dial() then how do I update the limit column in realtime as the call progresses.

There’s no function in Asterisk that will do this automatically for you. You could come close to this by querying the table from the dialplan, setting a timeout on the call based on that value using the TIMEOUT function, and then updating the table with the balance when a call ends using a hangup handler or the h extension.

Your other alternative would be to write a system that monitors Asterisk via AMI, deducts in pseudo-realtime from the balance in your table, and terminates calls when their limit is up.

I have a Python script that connect to the AMI. Through this I can see active calls, hangup calls. Do you think I can implement this in this script.