def __init__(self, hostname=TEST_CLOUD_HOSTNAME, port=TEST_CLOUD_PORT, ssl_ca_cert=CERT_DIR + '/rootCA.pem', cert_file=CERT_DIR + '/admin_operator.pem', key_file=CERT_DIR + '/admin_operator.key.pem'): self._hostname = hostname self._host = 'https://{hostname}:{port}/magma'.format( hostname=hostname, port=port) self._managed_networks = [] # type: List[str] self._ssl_ca = ssl_ca_cert self._ssl_cert = cert_file self._ssl_key = key_file # Set SSL config. NOTE: must be done before creating ApiClient configuration = swagger_client.Configuration() configuration.ssl_ca_cert = ssl_ca_cert configuration.cert_file = cert_file configuration.key_file = key_file configuration.verify_ssl = True configuration.assert_hostname = True api_client = swagger_client.ApiClient(host=self._host) self.subscribers_api = swagger_client.SubscribersApi( api_client=api_client, ) self.networks_api = swagger_client.NetworksApi(api_client=api_client) self.gateways_api = swagger_client.GatewaysApi(api_client=api_client) self.tiers_api = swagger_client.TiersApi(api_client=api_client) self.channels_api = swagger_client.ChannelsApi(api_client=api_client)
def create_channels_with_id(_phone, _channel_id): api_instance = swagger_client.ChannelsApi( swagger_client.ApiClient(configuration)) endpoint = 'PJSIP/' + str(_phone) channel_id = _channel_id # { "endpoint": "SIP/Alice", "variables": { "CALLERID(name)": "Alice" } } (optional) body = swagger_client.Containers() # extension = 'extension_example' # context = 'context_example' # priority = 789 label = 'label_example' # app = 'channel-playback-monkeys' # app = 'channel-tones' # app = 'hello' app = 'channel-playback' app_args = 'externalCall,summer' app_args = 'externalCall,urliz' app_args = 'externalCall,fizlizcourt' app_args = 'externalCall,fizliz' caller_id = _phone timeout = 60 # //other_channel_id = 'other_channel_id_example' # //originator = 'originator_example' # formats = 'formats_example' api_response = None try: # Create a new channel (originate with id). # please pass async_req=True # thread = api.originate_with_id(endpoint, channel_id, async_req=True) # result = thread.get() api_response = api_instance.originate_with_id( endpoint, channel_id, body=body, _return_http_data_only=True, # extension=extension, context=context, priority=priority, label=label, app=app, app_args=app_args, caller_id=caller_id, timeout=timeout, async_req=True) result = api_response.get() # , other_channel_id=other_channel_id, originator=originator) # ,formats=formats) # pprint(api_response) #print(result) #pprint(result) except ApiException as e: pprint("Exception when calling ChannelsApi->originate_with_id: %s\n" % e) if result: data = api_instance.getchannel(_channel_id, async_req=False) print(f'\napi_instance.getchannel(_channel_id):' f'{data.state}\n') # data = data.replace(',\n', ',') ''' ''' return result
async def say_asterisk_id(self, asterisk_id, channel): while True: logger.info("Going to say something on channel %s:%s" % (asterisk_id, channel.id)) sub_id = asterisk_id.split(":")[-1] text = "Your are connected to Asterisk number %s" % sub_id endpoint = "http://%s:%d" % (self.context.host, self.context.port) uri = "sound:%s/say?text=%s.wav" % (endpoint, urllib.parse.quote(text)) try: channels_api = swagger_client.ChannelsApi(self.api_client) await channels_api.channels_channel_id_play_post( channel.id, [uri], x_asterisk_id=asterisk_id) logger.info("Said something on channel %s:%s" % (asterisk_id, channel.id)) except ApiException as e: logger.error("Error while saiying something %s : %s" % (channel.id, e)) return await asyncio.sleep(5)
async def _dial(self, context, extension, adddress, port): endpoint = "SIP/%s:%s/%s" % (adddress, port, extension) logger.info("Dialing endpoint %s" % endpoint) try: channels_api = swagger_client.ChannelsApi(self.api_client) return await channels_api.channels_post( endpoint, app=self.id, x_asterisk_id=context.asterisk_id) except Exception as e: logger.error("Error while dialing endpoint %s : %s" % (endpoint, e))
async def answer(self, asterisk_id, channel): logger.info("Answering call on channel : %s" % channel.id) try: channels_api = swagger_client.ChannelsApi(self.api_client) await channels_api.channels_channel_id_answer_post( channel.id, x_asterisk_id=asterisk_id) logger.info("Answered channel %s successful" % channel.id) except Exception as e: logger.error("Error while answering channel %s : %s" % (channel.id, e))
async def play_media(self, context, uri): if context not in self.contextes: return try: channels_api = swagger_client.ChannelsApi(self.api_client) await channels_api.channels_channel_id_play_post( context.channel.id, [uri], x_asterisk_id=context.asterisk_id) logger.info("Play something on channel %s" % context) except ApiException as e: logger.error("Error while playing something %s : %s" % (context, e))
async def create_channels_with_id(_phone, _channel_id): """ :param _phone: :param _channel_id: :return: """ # TODO endpoint - сделать правильно выбор в зависимости от типа звонков(местные, городские, сотовые) global endpoint api_instance = swagger_client.ChannelsApi(swagger_client.ApiClient(configuration)) if len(str(_phone)) <= 4: # print(f"create_channels_with_id::len(phone): {_phone} = {len(str(_phone))}") logger.info(f"|Длинна номера: {_phone}| = {len(str(_phone))}") endpoint = 'PJSIP/' elif len(str(_phone)) == 10: logger.info(f"|Длинна номера: {_phone}| = {len(str(_phone))}") endpoint = 'IAX2/Aster4_Aster1/' elif len(str(_phone)) == 11: logger.info(f"|Длинна номера: {_phone}| = {len(str(_phone))}") endpoint = 'IAX2/Aster4_Aster1/' endpoint += str(_phone) channel_id = _channel_id body = swagger_client.Containers() label = 'label_example' app = 'channel-playback' app_args = 'externalCall,' + typeautodial caller_id = _phone timeout = 60 api_response = None try: api_response = api_instance.originate_with_id(endpoint, channel_id, body=body, # extension=extension, context=context, priority=priority, label=label, app=app, app_args=app_args, caller_id=caller_id, timeout=timeout) except ApiException as e: # print("Exception when calling ChannelsApi->originate_with_id: %s\n" % e) logger.exception('exception') return api_response