I am sitting at work in the morning. A colleague is late, there is nothing special to do. Boring Finally, the latecomer comes and complains that he did not wake up on his alarm clock. Either he didn’t start, or he didn’t hear, I don’t remember that. 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 are we going to implement?
Dialplan decided not to touch, because he is no longer 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.
Writing to whom and when to call the script itself seemed to me inconvenient, so a text file called alarmnumbers.txt 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 an asterisk can eat. The file name is written without permission, aster chooses what he needs. You can sound the file and do not specify, in this case, when you pick up the phone call will be immediately reset. Well, XXXX instead of time indicates not to wake up. For example, at the weekend.
In the file there can be as many lines as you like, repetition of numbers is also allowed. The solution, of course, is not the most optimal, but since the use of an alarm clock is assumed only within the IT department, it is quite appropriate. I posted it on the server's win ball, hooking it to FreeBSD using mount_smbfs.
The script itself runs on the crown once a day. Those. Update to whom and where to call occurs every day at 00.02.
Let's look at what the script does:
Specify where that is and how to call the call-files for asterisk (by the way, the file name is not important to it, you can specify any)
filename - our list of who and when
CallFileName - the name of the call-file, only for our convenience
NewCallFilesPath - the directory for the created call-files. It’s impossible to create them where the aster takes them, he reads them quickly and often, can read incomplete ones. This directory should be on the same section with the directory where the aster is looking for call files to be executed.
AsteriskCallFilePath is the directory from which the aster reads call files. Since I have FreeBSD, they may be located elsewhere.
currentdate=$(date +%Y%m%d) weekday=$(date +%u) var0=0
We 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 parameters 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, choosing the right options from each line.
DialTime is the call time, the
wd parameter is used here, which we set earlier.
number - the number to which the call will be made
dialwavfile - 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 here is this:
Channel: SIP/providername/89993332211 CallerID: CompanyNumber MaxRetries: 2 RetryTime: 450 WaitTime: 20 Application: Playback Data: /recs/macroform-robot_dity Archive: yes
Channel - What to call, indicates any channel. I have a peer for external calls. If you are going to call and to internal numbers, then it is necessary to add a condition on which various channels will be substituted.
CallerID - I think it is clear. It is not relevant if it is a call to an outside line and the provider does not allow changing CallerID
MaxRetries - Did not
pick up the phone? No problem! Call again, suddenly did not wake up? The parameter tells how many times to try to call the subscriber.
RetryTime -
Call back in so many seconds
WaitTime - Call up to the subscriber as many seconds.
Application - This is the asterisk application that will be used if the subscriber picked 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 issue the rights to read, modify and delete the user, from which the asterisk is running.
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 rule the time of the change or delete the call-file in the event that XXXX is indicated instead of time.
One of the important points in working with call-files. Asterisk will only read files with the date and time of change less than or equal to the current one. Thus, if we specify a modification time in the future, the aster will wait until the right time comes.
mv $NewCallFilesPath$CallFileName$varr$var0.call $AsteriskCallFilePath
Move our created file to aster to be eaten.
done echo "HAPPY END!!!" exit 0
Finish the script. With him everything.
It remains only to cram his execution in CZK. Do not forget to give him the right to perform.
2 0 * * * /usr/bin/my/alarm.sh
Well, in general, the alarm clock is ready. Fill
alarmnumbers.txt and wait for the call. We have been working regularly for 3 weeks. Glitches not yet detected.
What is the result?
We got an alarm clock with a schedule. It is possible to exclude any days of the week from the schedule. The system will create call-files, and the asterisk will execute when the appropriate time comes. Nothing complicated.
What is wrong:
- Run once a day. If you want to wake up today in 4 hours, the aster will not do anything, the call will be only in a week.
- The content of alarmnumbers.txt is critical. If there is a jamb in careless filling, there will be no call. The aster himself will, of course, be nothing, but still unpleasant.
- It is required to convert a sound file in advance into the required format. Autoconvert screw lazy.
- The colleague still does not respond to this alarm clock.
Criticism, comments, suggestions for improvement are welcome.