Esempio n. 1
0
    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.')
Esempio n. 4
0
    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()
Esempio n. 5
0
    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))
Esempio n. 6
0
    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()
Esempio n. 7
0
 def __init__(self):
     client = dialogflow.AgentsClient()
     parent = client.project_path('animal-ai')
     response = client.export_agent(parent)
Esempio n. 8
0
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)))
Esempio n. 9
0
 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
Esempio n. 10
0
 def isValidAgentId(self, agent_id):
     client = dialogflow.AgentsClient()
     parent = client.project_path(agent_id)
     response = client.get_agent(parent)