How to set an alarm using Asterisk, FreeBSD and some free time

I sit at work in the morning. A colleague is late, there is nothing special to do. Boring. Finally, a latecomer arrives and complains that he has not woken up according to his alarm clock. Either I didn’t start, or I didn’t hear, I don’t remember that anymore. Why not give him a call from work early in the morning?

No sooner said than done. I decided to write an alarm clock.

How will we implement it?


Dialplan decided not to touch, because he is already not the smallest, and I do not want to complicate it. Therefore, it was decided to write a shell script that runs on the crown and creates call-files with the necessary data.

It seemed to me inconvenient to write down to whom and when to call the script itself, so a text file alarmnumbers.txt of the form was created:

89993332211 1000 1100 1200 1300 1400 1500 1600 /recs/macroform-robot_dity
89993332211 1005 1105 1205 1305 1405 XXXX XXXX /recs/macroform-robot_dity


Where the first is the phone number to which the call will be made, then there are 7 blocks, separated by spaces, indicating the time of the call: the first block for Monday, the second for Tuesday, and so on. At the very end, the path to the file that will be played if you pick up the phone. This should be any sound file that the asterisk can eat. The file name is written without permission, the aster will choose what it needs. You can omit the sound file at all, in this case, when you pick up the handset, the call will immediately be dropped. Well, XXXX instead of time indicates not to wake up. For example, on the weekend.

The file can have as many lines as you like, repeating numbers is also allowed. The solution, of course, is not the most optimal, but since the use of the alarm clock is supposed only within the IT department, it is quite suitable. I placed it on the win server ball, hooking it to FreeBSD using mount_smbfs.

The script itself runs on the crown once a day. Those. Data update to whom and where to call occurs every day at 00.02.

Let's see what the script does:


#!/bin/sh
echo 'START!'
filename=/recs/alarmnumbers.txt
CallFileName=AlarmFile
NewCallFilesPath=/var/spool/asterisk/outgoing_new/
AsteriskCallFilePath=/var/spool/asterisk/outgoing/


We indicate where what lies and how to name the call-files for asterisk (by the way, the file name is not important for him, you can specify any)
filename - our file with a list to whom and when
CallFileName - the name of the call-file, only for our convenience
NewCallFilesPath - directory for generated call files. It is impossible to create them where the aster takes them from - he reads them quickly and often, he can read incomplete. This directory should be on the same section with the directory where the aster looks for call-files to execute.
AsteriskCallFilePath - the directory from which the aster reads call files. Because I have FreeBSD, you can have them in another place.


currentdate=$(date +%Y%m%d)
weekday=$(date +%u)
var0=0


Remember the current time and day of the week.


case "$weekday" in
        1)wd="13-16";;
        2)wd="18-21";;
        3)wd="23-26";;
        4)wd="28-31";;
        5)wd="33-36";;
        6)wd="38-41";;
        7)wd="43-46";;
esac


Depending on the day of the week, we will select the appropriate block in alarmnumbers.txt . These are the options for cut .


cat $filename | while read line
do
 DialTime=$(echo $line | cut -c$wd)
 var0=`expr $var0 + 1`
 number=$(echo $line | cut -c1-11)
 dialwavfile=$(echo $line | cut -c48-200)


Read alarmnumbers.txt . line by line, selecting the desired parameters from each row.
DialTime - call time, here we use the wd parameter , which we set earlier.
number - the number to which
dialwavfile will be dialed - sound file


 echo "Channel: SIP/providername/$number" > "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "CallerID: CompanyNumber" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "MaxRetries: 2" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "RetryTime: 450" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "WaitTime: 20" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "Application: Playback" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "Data: $dialwavfile" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"
 echo "Archive: yes" >> "$NewCallFilesPath$CallFileName$DialTime$var0.call"


Create a call file. The structure he will get is this:


Channel: SIP/providername/89993332211
CallerID: CompanyNumber
MaxRetries: 2
RetryTime: 450
WaitTime: 20
Application: Playback
Data: /recs/macroform-robot_dity
Archive: yes


Channel - Through what to call, any channel is indicated. I have a feast for external calls. If you are going to make calls to internal numbers, you will have to add the condition by which various channels will be substituted.
CallerID - I think it’s clear. It is not relevant if it is a call to an external line and the provider does not allow changing CallerID
MaxRetries - Didn’t pick up the phone? No problem! Let's call again, didn’t suddenly wake up? The parameter tells how many times to try to call the subscriber.
RetryTime - After so many calls back, in seconds
WaitTime - Calls the subscriber for so many seconds.
Application - This is the asterisk application that will be used if the subscriber picks up the phone.
Data- Data for the application from the previous line
Archive - Save executed call-files, for analysis, for example


 chmod 755 $NewCallFilesPath$CallFileName$DialTime$var0.call
 chown asterisk $NewCallFilesPath$CallFileName$DialTime$var0.call
 chgrp asterisk $NewCallFilesPath$CallFileName$DialTime$var0.call


We give read, modify and delete permissions to the user from whom the asterisk is launched.


 time=$(echo $line | cut -c$wd)
 case $time in
  XXXX) rm $NewCallFilesPath$CallFileName$DialTime$var0.call ;;
  "") rm $NewCallFilesPath$CallFileName$DialTime$var0.call ;;
  *) touch -t "$currentdate$time" "$NewCallFilesPath$CallFileName$DialTime$var0.call" ;;
 esac


Here we correct the time of the change or delete the call-file if XXXX is indicated instead of the time.
One of the important points in working with call-files. The asterisk will only read files with a date and time of change less than or equal to the current one. Thus, if we indicate the modification time in the future, the aster will wait until the right time arrives.


 mv $NewCallFilesPath$CallFileName$varr$var0.call $AsteriskCallFilePath


We move our created file to the aster to eat.

done
echo "HAPPY END!!!"
exit 0


We complete the script. Everything is with him.

It remains only to cram its implementation in crowns. Do not forget to give him the right to performance.


2       0          *       *       *       /usr/bin/my/alarm.sh


Well, basically, the alarm clock is ready. Fill in alarmnumbers.txt and wait for the call. We have been working properly for 3 weeks. No glitches have been found.

What is the result?


We got an alarm with a schedule. It is possible to exclude any days of the week from the schedule. The system will create call-files, and perform the asterisk when the right time comes. Nothing complicated.

What is wrong:


  • Run once a day. If you want to wake up today after 4 hours, the aster will do nothing, the call will be only in a week.
  • The content of alarmnumbers.txt is critical. If there is a cant on sloppy filling, there will be no call. Aster himself, of course, will not be anything, but still unpleasant.
  • You need to convert the sound file to the desired format in advance. Autoconversion screw lazy.
  • A colleague still does not respond to this alarm clock.


Criticism, comments, suggestions for improvement are welcome.

Also popular now: