Monday, April 5, 2021

Check GMAIL with a bashscript

The code below will check a GMAIL account and if it receives a new email, it will trigger the Issabel PBX to make a phone call. This might be useful to some people. This code will check the GMAIL inbox for email.

I setup a GMAIL account, but I had to turn down the security

in the Gmail account to let a shell script access it. This script doesn't have much intelligence, so it can't really tell the difference between an alarm email or a regular email, so use a dedicated GMAIL account.

gmailsecurity

Now create a shell script called " check_email.sh " on the PBX


#!/bin/bash username="Gmail-Name" password="Gmail-Password"

curl -u $username:$password --silent "https://mail.google.com/mail/feed/atom" > emailresult

cat emailresult | grep -oPm1 "(?<=<title>)[^<]+" | sed '1d' > title
cat emailresult | grep -oPm1 "(?<=<modified>)[^<]+" | sed '1d' >  date
sed -e 's/^/|/' -i date
paste title date > merge

diff merge lastmerge
if [ $? -ne 0 ]; then
    echo "Channel: IAX2/T28_Y02_Y07/5551234" > /etc/asterisk/alarm.call
    echo "MaxRetries: 2" >> /etc/asterisk/alarm.call
    echo "RetryTime: 60" >> /etc/asterisk/alarm.call
    echo "WaitTime: 30" >> /etc/asterisk/alarm.call
    echo "application: Playback" >> /etc/asterisk/alarm.call
    echo "data: /path/to/sound/file.wav" >> /etc/asterisk/alarm.call
    chmod 777 /etc/asterisk/alarm.call
    chown asterisk:asterisk /etc/asterisk/alarm.call
    mv /etc/asterisk/alarm.call /var/spool/asterisk/outgoing/

else
    echo "No New Email"

fi

mv merge -f lastmerge

change "IAX2/T28_Y02_Y07/5551234" to be the outbound trunk and the phone number you want the system to call.

data: /path/to/sound/file.wav - this is the recording you want the system to play when the person answers. "THIS IS AN ALARM, PLEASE CHECK SYSTEM"

Make the file executable

chmod u+x check_email.sh

Now run the file

 ./check_email.sh

The system

checks GMAIL It looks at the list of emails in the

INBOX and compares it to the list that was there previous If the list is the same, it doesn't do anything. If its different, it will generate a call file and ring the number in the script.

Now create a CRON job and run this over a period of time. I wouldn't do it more than eleven every 5 mins, I'm not sure if GMAIL will throttle you or not

The script finds the emails and the dates of each email, merges them together. Perhaps someone more proficient at parsing HTML code can make it do more. I might work on it more myself later. But something to try.

The "GREP" statement I got from https://linuxconfig.org/check-your-gmail-inbox-for-new-emails-with-bash-script, that site might give you more information how to make things work.

2 comments:

  1. hola lo etoy prbando pero tengo un error repica y cuelga la llamada.que etoy haciendo mal

    ReplyDelete
  2. Funcionando corrigiendo unos detalles GRACIAS

    ReplyDelete

Feel free to leave a comment! If you have any information that you think should be included, please do so here and I'll get it added in.