Several years ago, I wrote a python script to notify me when my asterisk server got an incoming call. I programmed this out of pure laziness: I wanted to know who was calling me without having to look at my phone.
This script would show a pop-up on my laptop whenever I got a call and the notification would show who was calling and for whom. This was very useful since I was traveling a lot during that time, so I would know if somebody was trying to reach me at home.
I’m still using this script, but I’m finding myself using my cellphone more often than my laptop when I’m out of the house. So, I modified my script to send a notification to my smart phone instead of my laptop.
Even though it would have been very good learning experience, I didn’t want to program a fully fledged app for this. First of all, I would have needed to setup a new development environment, learn a whole new API stack, deal with deployment, etc. Fortunately, I heard of Pushbullet. This app can receive notifications – and do much more – from an web API so it seemed like the perfect way to resolve my problem.
Simply, I only needed to send an web request using curl to the pushbullet API using the provided API key (found on Pushbullet’s setting page):
curl -u $API_KEY: https://api.pushbullet.com/v2/pushes -d type=note -d title="Alert" -d body="Notification Body"
This simple request would pop-up a notification on my smartphone – see the screenshot above. The only thing remaining to do was to cleanup my old script, remove everything related to the GUI and add the curl command. After testing the following script, I added it on my asterisk server and made sure it was launched on startup using upstart.
Here’s the script. You can use it and adapt it has you wish, but a short credit would be nice.
Created on 2015-05-18
from twisted.internet import reactor
from starpy.manager import AMIFactory
from twisted.internet import task
from starpy import manager
#asterisk AMI login
"SIP/6000":"Olivier – 2"}
API_KEY = "XXXXXXXXXXXXXXXXX"
log = logging.getLogger("pyCalledMe-PB")
AMIFactory.__init__(self, username, secret)
df = self.login(server, port)
log.info("connection lost – connecting again")
log.info("connection failed – connecting again")
if ‘destination’ in event:
for s in extensions.keys():
os.system("curl -silent -u "+API_KEY+": https://api.pushbullet.com/v2/pushes -d type=note -d title=\"Call for "+extname+"\" -d body=\""+cidname+" – "+cid+"\" > /dev/null")
dc = reactor.callLater(timeoutping,ontimeout)
df = protocol.ping()
df = protocol.registerEvent("Dial",onDial)
timeouttask = task.LoopingCall(checknetlink,protocol)
cm = callMeFactory()
if __name__ == ‘__main__’:
#manager.log.setLevel( logging.DEBUG )