Package: aioagi
AioagiAsync agi client/server framework.
The project based on “aiohttp” framework.Key FeaturesSupports both client and server side of AGI protocol.AGI-server has middlewares and pluggable routing.Getting startedServerSimple AGI server:importasynciofromaiohttp.webimportApplication,AppRunner,TCPSite,Responsefromaioagiimportrunnerfromaioagi.appimportAGIApplicationfromaioagi.logimportagi_server_loggerfromaioagi.urldispathcerimportAGIViewfromaiohttp.web_runnerimportGracefulExitasyncdefhello(request):message=awaitrequest.agi.stream_file('hello-world')awaitrequest.agi.verbose('Hello handler:{}.'.format(request.rel_url.query))agi_server_logger.debug(message)asyncdefhttp_hello(request):returnResponse(text="Hello, world")classHelloView(AGIView):asyncdefsip(self):message=awaitself.request.agi.stream_file('hello-world')awaitself.request.agi.verbose('HelloView handler:{}.'.format(self.request.rel_url.query))agi_server_logger.debug(message)if__name__=='__main__':app=AGIApplication()app.router.add_route('SIP','/',hello)runner.run_app(app)# ORif__name__=='__main__':apps=[]app=AGIApplication()app.router.add_route('SIP','/',hello)http_app=Application()http_app.router.add_route('GET','/',http_hello)loop=asyncio.get_event_loop()runners=[]sites=[]for_appin[app,http_app]:app_runner=AppRunner(_app)loop.run_until_complete(app_runner.setup())ifisinstance(_app,AGIApplication):sites.append(runner.AGISite(app_runner,port=8081))else:sites.append(TCPSite(app_runner,port=8080))runners.append(app_runner)forsiteinsites:loop.run_until_complete(site.start())uris=sorted(str(s.name)forrunnerinrunnersforsinrunner.sites)print("======== Running on{}========\n""(Press CTRL+C to quit)".format(', '.join(uris)))try:loop.run_forever()except(GracefulExit,KeyboardInterrupt):# pragma: no coverpassfinally:forrunnerinreversed(runners):loop.run_until_complete(runner.cleanup())ifhasattr(loop,'shutdown_asyncgens'):loop.run_until_complete(loop.shutdown_asyncgens())loop.close()ClientTo set AGI connection as Asterisk:importasyncioimportlogging.configfromaioagi.logimportagi_client_loggerfromaioagi.clientimportAGIClientSessionfromaioagi.parserimportAGIMessage,AGICodeasyncdeftest_request(loop):headers={'agi_channel':'SIP/100-00000001','agi_language':'ru','agi_uniqueid':'1532375920.8','agi_version':'14.0.1','agi_callerid':'100','agi_calleridname':'test','agi_callingpres':'0','agi_callingani2':'0','agi_callington':'0','agi_callingtns':'0','agi_dnid':'101','agi_rdnis':'unknown','agi_context':'from-internal','agi_extension':'101','agi_priority':'1','agi_enhanced':'0.0','agi_accountcode':'','agi_threadid':'139689736754944',}asyncwithAGIClientSession(headers=headers,loop=loop)assession:asyncwithsession.sip('agi://localhost:8080/hello/?a=test1&b=var1')asresponse:asyncformessageinresponse:client_logger.debug(message)awaitresponse.send(AGIMessage(AGICode.OK,'0',{}))asyncwithsession.sip('agi://localhost:8080/hello-view/?a=test2&b=var2')asresponse:asyncformessageinresponse:client_logger.debug(message)awaitresponse.send(AGIMessage(AGICode.OK,'0',{}))NoteSession request headers are set automatically forsession.sip('agi://localhost:8080/hello/?a=test1&b=var1')request:agi_type:SIPagi_network:yesagi_network_script:hello/agi_request:agi://localhost:8080/hello/AMIimportasynciofromaioagi.ami.actionimportAMIActionfromaioagi.ami.managerimportAMIManagerasyncdefcallback(manager,message):print(message)asyncdefmain(app):manager=AMIManager(app=app,title='myasterisk',host='127.0.0.1',port=5038,username='username',secret='secret',)manager.register_event('*',callback)app['manager']=managerawaitmanager.connect()awaitasyncio.sleep(2)message=awaitmanager.send_action(AMIAction({'Action':'Command','Command':'database show',}))print(message)print(message.body)asyncdefcleanup(app):app['manager'].close()if__name__=='__main__':app={}_loop=asyncio.get_event_loop()try:_loop.run_until_complete(main(app))exceptKeyboardInterrupt:_loop.run_until_complete(cleanup(app))_loop.close()Installpip install aioagiThanksGael Pasgrimaud -panoramisk