def _upload_agent(self, agent_name, agent_id): client = dialogflow.AgentsClient() parent = client.project_path(agent_id) in_file = open(os.path.join("./dialogflow", agent_name + ".zip"), "rb") data = in_file.read() response = client.restore_agent(parent, agent_content=data)
def restoreAgent(self, file): client = dialogflow.AgentsClient() f = open(file, 'rb') data = f.read() f.close() response = client.restore_agent(self.project, agent_content=data) def callback(operation_future): result = operation_future.result() response.add_done_callback(callback) print('Agent restored with the file', file)
def exportAgent(self): client = dialogflow.AgentsClient() response = client.export_agent(self.project) def callback(operation_future): result = operation_future.result() response.add_done_callback(callback) bytes = response.result().agent_content filename = '{}_{}.zip'.format(self.project_id, now.strftime('%Y-%m-%d')) f = open(filename, 'wb') f.write(bytes) f.close() print(filename, 'exported.')
def pull_agent(self, agent_id=None): try: if not agent_id: agent_id = self.get_credential('dialogflow')['agent_id'] client = dialogflow.AgentsClient() parent = client.project_path(agent_id) response = client.get_agent(parent) agent_name = response.display_name if not os.path.exists("./dialogflow"): os.mkdir("./dialogflow") self._download_agent(agent_name, agent_id) self._json2yml(agent_name, agent_id) except google.api_core.exceptions.PermissionDenied: raise exc.InvalidAgentId(agent_id) except google.auth.exceptions.DefaultCredentialsError: raise exc.InvalidCredentialPath()
def _download_agent(self, agent_name, agent_id): client = dialogflow.AgentsClient() parent = client.project_path(agent_id) agent_folder = os.path.join("./dialogflow", agent_name) if os.path.exists(agent_folder): shutil.rmtree(agent_folder) os.mkdir(agent_folder) response = client.export_agent(parent).operation.response.value zip_file = zipfile.ZipFile(io.BytesIO(response), "r") for filename in zip_file.namelist(): split_name = filename.split('/') for name in split_name[:-1]: if not os.path.exists(os.path.join(agent_folder, name)): os.mkdir(os.path.join(agent_folder, name)) with open(os.path.join(agent_folder, filename), "wb") as f: f.write(zip_file.read(filename))
def push_agent(self): agent_id = self.get_credential('dialogflow')['agent_id'] client = dialogflow.AgentsClient() parent = client.project_path(agent_id) response = client.get_agent(parent) agent_name = response.display_name agent_folder = os.path.join("./dialogflow", agent_name) try: self._yml2json(agent_name, agent_id) with zipfile.ZipFile(agent_folder + '.zip', 'w') as myzip: for folder, subfolders, files in os.walk(agent_folder): for f in subfolders + files: if not f.endswith(".yml"): absname = os.path.join(folder, f) arcname = absname.replace("/dialogflow", "") myzip.write(absname, arcname) self._upload_agent(agent_name, agent_id) except google.api_core.exceptions.InvalidArgument: raise exc.InvalidYamlFormat() except TypeError: raise exc.InvalidYamlFormat()
def __init__(self): client = dialogflow.AgentsClient() parent = client.project_path('animal-ai') response = client.export_agent(parent)
def get_dialogflow_account_details(): client = dialogflow.AgentsClient() parent = client.project_path(DIALOGFLOW_PROJECT_ID) details = client.get_agent(parent) return make_response(jsonify(MessageToDict(details)))
def _get_dialogflow_lang(self, agent_id): client = dialogflow.AgentsClient() parent = client.project_path(agent_id) response = client.get_agent(parent) lang = response.default_language_code return lang
def isValidAgentId(self, agent_id): client = dialogflow.AgentsClient() parent = client.project_path(agent_id) response = client.get_agent(parent)