Thursday, April 15, 2021

PowerShell Scripts

 Get a list of running applications on windows 10

From Powershell

Get-Process | Where-Object { $_.MainWindowTitle } | Format-Table Name,Mainwindowtitle -AutoSize

or from windows CMD prompt

powershell "Get-Process | Where-Object { $_.MainWindowTitle } | Format-Table Name,Mainwindowtitle -AutoSize"

Tuesday, April 6, 2021

Asterisk Post Call Recording Script configuration

This code shows how to alter a file with the Post Call Recording script.  But in general shows a bit of how the process of making this asterisk feature work

I did worked on trying to get that AGENT ID variable via dialplan, but I just couldn't figure it out in Issabel. The Callcenter system in that is very tied in and I just wasn't able to figure it out. If someone else can show me, that would be fantastic.

I know that is value is stored in the QUEUE. LOG file as each call is sent to an agent. So using the "Post Call Recording Script", at the end of each call, I use a shell script that is automatically activated. This script searches the queue.log file for the unqiue callID and the value "COMPLETEAGENT". This indicates the end of a queue call. The shell parses the entry and pulls out the agent id, Then it renames the sound recording by adding on the AGENTXXXX ID.

So a call that went to queue Agent ID "9989", the recording changes from this:

rg-600-1000-20210331-213521-1617237321.105.wav

and becomes this:

rg-600-1000-20210331-213521-1617237321.105-Agent9989.wav

Here's how i did it:

You don't need any modifications to the Issabel diaplan. The only thing you have to do in Issabel is activate "Post Call Recording Script"

PBX -> "Advanced Settings"

HiddenMenu

You will need to activate "Display Readonly Settings"

and "Override Readonly Settings" Set them both to "TRUE" then click on the Green checkbox on the right side.

You wlll get prompted that you will need to "REFRESH THE PAGE" which you will have to.
Once both boxes are set to "TRUE" and you have pressed the checkbox, click on "ADVANCED SETTINGS" again to refresh.

Now scroll down and you will see

"Post Call Recording Script"

In that box put in this. The quotes around the path and script are required. Asterisk $variables are substituted with a "hat" symbol

HiddenMenu

"/etc/asterisk/updateagent.sh" ^{UNIQUEID} ^{MIXMONITOR_FILENAME}

This is the code that will run after the caller hangsup and the call recording is completed processing.
After each call, the "updateagent.sh" shell script is activated and it passes the UniqueID of the call as well as the current file location

Select the green check mark to the right of this box. Then press APPLY SETTINGS

Now we need to create a shell script.

In /etc/asterisk create a file called "updateagent.sh" (you can of course put this file in your preferential own directory, just make sure the path in the config in Issabel reflects it. You can also change the name.

In side this file you will want to copy in the following into that file and then save it.

#!/bin/bash

#This line finds the agent number in the queue_log file.  It then parses out any extra data to get the number value
#It will find thise 1617240353|1617234166.70|NONE|Agent/9989|AGENTLOGOFF|SIP/1000-00000028|6182 and finish with
#this "9989"
agentnumber=$(grep $1.*COMPLETEAGENT /var/log/asterisk/queue_log | awk -F "|" '{ print $4 }' | awk -F "/" '{ print $2 }')

#this line finds the file type you are using.  They are usually .WAV, but in case its something else we want to account for that
filetype=$(echo $2 | awk -F "." '{ print $3 }')

#This creates a new file name based on the AGENTID and the FileType you are using
newfilename=$(echo $2 | awk -F "." '{ print $1"."$2}')-Agent$agentnumber.$filetype

mv $2 $newfilename
#This rg-600-1000-20210331-213521-1617237321.105.wav
#becomes
#rg-600-1000-20210331-213521-1617237321.105-Agent-9989.wav

Now you will need to make the file executable

chmod u+x updateagent.sh

And now you need to change ownership to allow asterisk PBX to run it

chown asterisk:asterisk updateagent.sh

And that should be it. Make a test call and see if it works. If you have problems, just take out the code in "Post Call Recording Script" and will let you troubleshoot.

MODIFICATION TO PASS OTHER VARIABLES

You can pass additional variables into the script with two modifications

In the Call Record Script you can add more entries by adding them after the MixMonitor entry. Every variable that would be in asterisk needs to have the $ changed to a ? (hat) symbol. So if you wanted to pass $'CONTEXT' and $'EPOCH' data, you can add it. Very important to have "quotes" around the shellscript path. Many examples on internet forget to include this requirement

"/etc/asterisk/updateagent.sh" ^{UNIQUEID} ^{MIXMONITOR_FILENAME} ^{CONTEXT} ^{EPOCH}

THEN in the updateagent.sh script, you just need to alter the "newfilename" entry by adding a $3 and then a $4 like the example below. These would represent the 2 additional values.

newfilename=$(echo $2 | awk -F "." '{ print $1"."$2}')-Agent$agentnumber-$3-$4.$filetype

Save and you should be able to run the file. If the variables are passed correctly, you'll see them appear in the filename, something like this: (DialPlanVar1 and Var2)

rg-600-1000-20210331-230350-1617242630.111-Agent9989-DialPlanVar1-DialPlanVar2.wav

If the data is NOT available, they will just look like this: (you'll see the - that are delimiters)

rg-600-1000-20210331-230350-1617242630.111-Agent--.wav

    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.