Random Announcement Script

So I’ve been looking for a way to randomly play different repeater announcements at random times. And I have finally figured it out.

I first set the audio files in the format that asterisk wants and placed in a directory called “audio-files”.

I then set two cron jobs to call the script every hour between designated hours that I want the announcements to play:

0 6-22 * * 1-5 cd /etc/asterisk/local; ./announcements.sh
0 8-22 * * 0,6 cd /etc/asterisk/local; ./announcements.sh

I then setup the script and made it executable:

#!/bin/bash
#Created by K6IAN 12/21/2020
#Set (path) variable to audio announcements directory
path=“/etc/asterisk/audio-files”

#Cron job set at hourly interval, sleep for a random amount of time
sleep $(( RANDOM % 1800 ))

if [ -e “$path” ]
then
id=find "$path" -type f|shuf -n1

#Set to global or local play; define node number to play on; trim off file extension
/usr/sbin/asterisk -rx “rpt localplay 1200 echo ${id%.*}
else
echo “$path not found”
fi

The result is that the cron job is called every hour, but sleeps for a random amount of time up to 30 minutes (this can be changed, and is in 60 second increments) between the specified hours in the original cron job. It then picks an audio file at random in the directory, takes off the .wav file extension (as Asterisk doesn’t like those I found out…) then plays it either globally or locally depending on how it’s set.

Just thought I’d share… :slight_smile:

-Ian K6IAN

1 Like

Very nice! Tailmessage is similar but only if there is node activity and it’s not random.