Asterisk Python Starpy Click-to-Call

With the advent of VoIP and computer telephony integration, one feature that have popped up is Click2Call. Its a convenient way to initiate a call with people whom you want to talk to by just clicking on their number in Outlook or on some webpage etc etc. Vonage has this feature for their service.

Lets see how we can achieve this with Asterisk. Asterisk provides Originate AMI action for this. Just call the Originate action with the numbers you want to connect. Here is the twisted code on how you can do it ( You need starpy to do it). The following example integrates http with AMI where by hitting the url some thing like http://localhost:8000?number=12322342345&ext=234 will do the magic needed. The same way you integrate this with any other protocol you think that it useful to you. After all twisted supports tons of those.


#!/usr/bin/python


from starpy import manager
from starpy.manager import AMIFactory
from twisted.internet import reactor
from twisted.web import server,resource

import logging

logging.basicConfig()
log = logging.getLogger("Click2Call")
log.setLevel(logging.DEBUG)
manager.log.setLevel(logging.DEBUG)

class Click2CallProtocol(object):
 def __init__(self):
     pass
 def onConnect(self,ami):
     log.info("Logged in successfully")
     self.ami = ami
 
 def dial(self,number,ext):
     try:
         self.ami.originate(channel='Local/'+number+"/n",context='mycontext',priority='1',exten=ext,async=True)
         return True
     except:
         return False
 

     
class Click2CallFactory(AMIFactory):

 def __init__(self):
     AMIFactory.__init__(self,"mark","mysecret")
 
 def connect(self):
     df = self.login("127.0.0.1")
     df.addCallback(c2cp.onConnect)
 
 def clientConnectionLost(self,connector,reason):
     log.info("We lost connection trying reconnect")
     reactor.callLater(1,self.connect)
 
 def clientConnectionFailed(self,connector,reason):
     log.info(reason)
     reactor.callLater(1,self.connect)

class Click2CallResource(resource.Resource):

 def render(self,request):
     try:
         number = request.args['number'][0]
         agent = request.args['ext'][0]
     except:
         return "Required arguments not found"
     if c2cp.dial(number,agent):
         return "OK"
     else:
         return "NOTOK"

c2cp = Click2CallProtocol()
c2cf = Click2CallFactory()
c2cf.connect()

root = Click2CallResource()
root.putChild("",root)
site = server.Site(root)

reactor.listenTCP(8000,site,)
reactor.run()


For installing Asterisk on ubuntu visit http://godson.in/how-to-install-asterisk-on-ubuntu
You can install Twisted on ubuntu by the following command
apt-get install python-twisted
Get starpy from https://sourceforge.net/project/showfiles.php?group_id=164040

Asterisk VoIP consultant Hyderabadhttp://godson.in/voip-asterisk-freeswitch-python-consulting
With the advent of VoIP and computer telephony integration, one feature that have popped up is Click2Call. Its a convenient way to initiate a call with people whom you want to talk to by just clicking on their number in Outlook or on some webpage etc etc. Vonage has this feature for their service.

Lets see how we can achieve this with Asterisk. Asterisk provides Originate AMI action for this. Just call the Originate action with the numbers you want to connect. Here is the twisted code on how you can do it ( You need starpy to do it). The following example integrates http with AMI where by hitting the url some thing like http://localhost:8000?number=12322342345&ext=234 will do the magic needed. The same way you integrate this with any other protocol you think that it useful to you. After all twisted supports tons of those.


#!/usr/bin/python


from starpy import manager
from starpy.manager import AMIFactory
from twisted.internet import reactor
from twisted.web import server,resource

import logging

logging.basicConfig()
log = logging.getLogger("Click2Call")
log.setLevel(logging.DEBUG)
manager.log.setLevel(logging.DEBUG)

class Click2CallProtocol(object):
 def __init__(self):
     pass
 def onConnect(self,ami):
     log.info("Logged in successfully")
     self.ami = ami
 
 def dial(self,number,ext):
     try:
         self.ami.originate(channel='Local/'+number+"/n",context='mycontext',priority='1',exten=ext,async=True)
         return True
     except:
         return False
 

     
class Click2CallFactory(AMIFactory):

 def __init__(self):
     AMIFactory.__init__(self,"mark","mysecret")
 
 def connect(self):
     df = self.login("127.0.0.1")
     df.addCallback(c2cp.onConnect)
 
 def clientConnectionLost(self,connector,reason):
     log.info("We lost connection trying reconnect")
     reactor.callLater(1,self.connect)
 
 def clientConnectionFailed(self,connector,reason):
     log.info(reason)
     reactor.callLater(1,self.connect)

class Click2CallResource(resource.Resource):

 def render(self,request):
     try:
         number = request.args['number'][0]
         agent = request.args['ext'][0]
     except:
         return "Required arguments not found"
     if c2cp.dial(number,agent):
         return "OK"
     else:
         return "NOTOK"

c2cp = Click2CallProtocol()
c2cf = Click2CallFactory()
c2cf.connect()

root = Click2CallResource()
root.putChild("",root)
site = server.Site(root)

reactor.listenTCP(8000,site,)
reactor.run()


For installing Asterisk on ubuntu visit http://godson.in/how-to-install-asterisk-on-ubuntu
You can install Twisted on ubuntu by the following command
apt-get install python-twisted
Get starpy from https://sourceforge.net/project/showfiles.php?group_id=164040

Asterisk VoIP consultant Hyderabadhttp://godson.in/voip-asterisk-freeswitch-python-consulting
Created by: godson, Last modification: Fri 02 of Jul, 2010 (00:26 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+