def create_application(self, name=None, voiceUrl=None, messagingUrl=None, platform=None, partition=None, request_format=None): """ Create a new Application @param name The name of the application @param voiceUrl The name of the application @param messagingUrl The name of the application @param platform The name of the application @param partition The name of the application @return: String application The ID of the new application created if request-type is JSON. Full response with application url otherwise. """ self.preconditionCheck() # if(name is None and voiceUrl is None and messagingUrl is None and platform is None and partition is None): # return "Create Application failed as none of the field is set" if(request_format is not None): self.requestType = request_format resource = self.url + "applications" if self.requestType == data_format.JSON: postBody = {"name": name, "voiceUrl": voiceUrl, "messagingUrl": messagingUrl, "platform": platform, "partition": partition} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<application><name>%s</name><voiceurl>%s</voiceurl><messagingurl>%s</messagingurl><platform>%s</platform><partition>%s</partition></application>" % (name, voiceUrl, messagingUrl, platform, partition) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "name=%s&voiceURL=%s&messagingURL=%s&platform=%s&partition=%s" % (name, voiceUrl, messagingUrl, platform, partition) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Create Application failed." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'][u'href'].rpartition('/')[2] else: result = response[u'responseValue'] return result else: return "Create Application failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])
def add_IM_account(self, applicationId, accountType, username, password, request_format=None): """ Add an IM account (Gmail / AIM / Yahoo etc) to the Application @param applicationId The ID of the application to which the IM account has to be added @param accountType The type of the IM account that needs to be added @param username The username of that IM account @param password The password to that IM account @return: response The URL of the IM account added """ self.preconditionCheck() if(request_format is not None): self.requestType = request_format resource = self.url + "applications/" + applicationId + "/addresses" if self.requestType == data_format.JSON: postBody = {"type":accountType, "username":username, "password":password} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<address><type>%s</type><username>%s</username><password>%s</password></address>" % (accountType, username, password) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "type=%s&username=%s&password=%s" % (accountType, username, password) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Bad request type." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'][u'href'] else: result = response[u'responseValue'] return result else: return "Add IM Account failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])
def add_voice_token(self, applicationId, field_type, field_channel, request_format=None): """ Add a voice token to the Application @param applicationId The ID of the application to which the voice token has to be added @param field_type The type @param field_channel The channel @return: response The token for that channel if headers is JSON. Full URL for that voice token otherwise. """ self.preconditionCheck() if(request_format is not None): self.requestType = request_format resource = self.url + "applications/" + applicationId + "/addresses" if self.requestType == data_format.JSON: postBody = {"type":field_type, "channel":field_channel} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<address><type>%s</type><channel>%s</channel></address>" % (field_type, field_channel) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "type=%s&channel=%s" % (field_type, field_channel) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Bad request type." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'][u'href'].rpartition('/')[2] else: result = response[u'responseValue'] return result else: return "Adding Voice Token failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])
def add_international_number_from_pool(self, applicationId, prefix, request_format=None): """ Add an international number to the Application @param applicationId The ID of the application to which the number has to be added @param prefix The country code prefix where the number will be added @return: response The international number added to the application if Headers is JSON. Full response body otherwise. """ self.preconditionCheck() if(request_format is not None): self.requestType = request_format resource = self.url + "applications/" + applicationId + "/addresses" if self.requestType == data_format.JSON: postBody = {"type":"number", "prefix":prefix} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<address><type>number</type><prefix>%s</prefix></address>" % (prefix) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "type=number&prefix=%s" % (prefix) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Bad request type." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'][u'href'].rpartition('/')[2] else: result = response[u'responseValue'] return result else: return "Adding International Number failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])
def start_session_webApi(self, method, token=None, request_format=None): """ Create a new WebApi Session @param token The body of the request @return: Dict response The response from the REST API call as a dict containing token """ self.preconditionCheck() if(token is None): return "Token is not set." if(request_format is not None): self.requestType = request_format resource = "https://api.tropo.com/1.0/sessions" # resource = "https://api.tropo.com/1.0/sessions?action=create&token=TOKEN&numberToDial=4075551212&customerName=John+Dyer&msg=the+sky+is+falling" if (method == "POST"): if self.requestType == data_format.JSON: postBody = {"token": token} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<session><token>%s</token></session>" % (token) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "token=%s" % (token) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Bad request type." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'] return result # returns a list else: result = response[u'responseValue'] return result else: return "Starting Session-WebApi failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue']) if (method == "GET"): resource = resource + "?action=create&token=" + token response = TropoConnect.doGET(self.userName, self.password, resource, self.requestType) self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'] return result # returns a list else: result = response[u'responseValue'] return result else: return "Starting Session-WebApi failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])
def start_session(self, method, token=None, customerName=None, numberToDial=None, msg=None, request_format=None): """ Create a new Application @param token The token for the session @param customerName The name of the customer @param numberToDial The number to dial @param msg The message body @return: Dict response The response from the REST API call as a dict containing token """ self.preconditionCheck() if(token is None): return "Token is not set." if(request_format is not None): self.requestType = request_format postBody = "" resource = "https://api.tropo.com/1.0/sessions" if (method == "POST"): if self.requestType == data_format.JSON: postBody = {"token": token, "customerName": customerName, "numberToDial": numberToDial, "msg": msg} response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.XML: postBody = "<session><token>%s</token><var name=\"customerName\" value=\"%s\"></var><var name=\"numberToDial\" value=\"%s\"></var><var name=\"msg\" value=\"%s\"></var></session>" % (token, customerName, numberToDial, msg) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) elif self.requestType == data_format.FORM_ENCODED: postBody = "token=%s&customerName=%s&numberToDial=%s&msg=%s" % (token, customerName, numberToDial, msg) response = TropoConnect.doPOST(self.userName, self.password, resource, postBody, self.requestType) else: return "Bad request type." self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'] return result # returns a list else: result = response[u'responseValue'] return result else: return "Starting Session failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue']) if (method == "GET"): params = urllib.urlencode({"token": token, "customerName": customerName, "numberToDial": numberToDial, "msg": msg}) resource = resource + "?action=create&%s" % (params) response = TropoConnect.doGET(self.userName, self.password, resource, self.requestType) self.tropo_logger_inst.logger(DEBUG, "ResponseCode = %s" % response[u'responseCode']) self.tropo_logger_inst.logger(DEBUG, "ResponseValue = %s" % response[u'responseValue']) if (response[u'responseCode'] == 200): if(self.requestType == data_format.JSON): result = response[u'responseValue'] return result # returns a list else: result = response[u'responseValue'] return result else: return "Starting Session failed with the following reason: (Error Code = %s) (Error msg = %s)" % (response[u'responseCode'], response[u'responseValue'])