script notify and email if bgp status change

Hi everyone,

Im newbie in script and have one unresolved script in my network
I`d like to create a script which would change some mangle, distance route and email me if BGP session is established or NOT based on ping link gateway
I have tried writing script from few source on this forum but it seem not works correctly, the problem is that on every script run it shows repeated status on the logs and e-mail notification
I would like to be notified only once (log & email) if the current status become different from the last status.
Im sory for my bad english, Thank you in advance

CTTH : viewtopic.php?t=40507
/viewtopic.php?t=189128

:global pinghost "10.10.211.2"
:global oldbgpstate
:if (:len $oldbgpstate = 0 ) do={ 
   :set $oldbgpstate {"_dummy"=33} }
:global bgpstates ({disabled=0},{idle=1},{connect=2},{active=3},{opensent=4},{openconfirm=5},{established=6})
    :foreach peer in=$bgpstate do={
    :global peerstate [:toarray [/routing bgp peer get $bgpstate state]]
	:global pstate [get $peer state]
        :if ( [get $peer disabled] ) do={ :set $pstate value="disabled" }
            :global peername [get $peer name]
            :global severity "info"
	    :global result [/ping 10.10.211.2 count=10]
    	
	    :if ($result=0) do={
            :log error "Ping host: ALL FAILED (0/10)"
            /ip firewall mangle set [find comment="mangle" disabled=yes] disabled=no
	    /tool e-mail send to=$emailTo subject="[$DeviceName] $emailSubject $commentFilter" body="$emailBody" 
	} else={
        :if ($result!=10) do={
            :log warning "Ping : ONLY SOME ($result/10)"
        } else={
            :log info "Ping host: ALL OK (10/10)"
            /ip firewall mangle set [find comment="mangle" disabled=no] disabled=yes
            /tool e-mail send to=$emailTo subject="[$DeviceName] $emailSubject $commentFilter" body="$emailBody"
        }
   }
}

anyone, maybe have another idea

Of course: As he sees that script, he changes the topic.

So, what should i do to solve it