def import_mod(self, filenames, language): """ Importe l'/les intent(s) sur DialogFlow :param filenames: Nom du/des fichier(s) JSON contenant la configuration du/des module(s) :param language: Langage source de l'intent """ base_file = "Config/Intents/import_base.json" concat_files_json = '' with open(base_file) as f: base_json = f.read() base_json = re.sub('\$LANGUAGE', '"' + language + '"', base_json) # TODO: Mettre la boucle for et le regex dans une fonction for i, filename in enumerate(filenames, start=1): with open(filename, "r") as f: file_json = f.read() concat_files_json += file_json if i is not len(filenames): concat_files_json += ',' base_json = re.sub('\$INTENTS', concat_files_json, base_json) base_json = self._get_id(base_json) response = post_query("agent/intents:batchUpdate", base_json) status_details(response)
def export_mod(self, modnames): """ Exporte l'/les intent(s) de DialogFlow :param modnames: Nom de l'/des intent(s) """ name_id_dict = self._get_name_id_dict() for modname in modnames: if modname not in name_id_dict: raise ValueError("Il n'y a pas d'intentions qui s'appelle " + modname + "!") response = get_query("agent/intents/" + name_id_dict[modname]) status_details(response) name_intent = json.loads(response.text)["displayName"] # TODO: Fonction pour créer un nouveau dossier et write dans un fichier path = "/".join( (config.data_export_folder, globals.project_id, self.folder)) filename = '/'.join((path, name_intent + ".json")) pathlib.Path(path).mkdir(parents=True, exist_ok=True) with open(filename, 'w') as f: f.write(response.text)
def get_query(url): """ Envoi une requête GET à l'API de Dialogflow :param url: Fin de l'url de "https://dialogflow.googleapis.com/v2/projects/$PROJECT_ID/" :return: Réponse de l'API de Dialogflow """ response = globals.authr.request( 'GET', "https://dialogflow.googleapis.com/v2/projects/" + globals.project_id + "/" + url) status_details(response) return response
def post_query(url, data): """ :param url: Fin de l'url de "https://dialogflow.googleapis.com/v2/projects/$PROJECT_ID/" :param data: Contenu du fichier :return: Réponse de l'API de Dialogflow """ response = globals.authr.request( 'POST', "https://dialogflow.googleapis.com/v2/projects/" + globals.project_id + "/" + url, data=data) status_details(response) return response
def remove_mod(self, modnames): """ Supprime l'/les intent(s) de DialogFlow modnames : Nom de l'/ des intent(s) """ name_idurl_dict = self._get_name_idurl_dict() intent_list = [] for modname in modnames: if modname not in name_idurl_dict: print("Attention : Il n'y a pas d'intention qui s'appelle " + modname + "!") continue intent_list.append({"name": name_idurl_dict[modname]}) dict_json = {"intents": intent_list} response = post_query("agent/intents:batchDelete", data=json.dumps(dict_json)) status_details(response)
def remove_mod(self, modnames): """ Supprime l'entity de DialogFlow :param modnames: Nom des entités """ name_idurl_dict = self._get_name_idurl_dict() entity_list = [] for modname in modnames: if modname not in name_idurl_dict: print("Attention : Il n'y a pas d'entité qui s'appelle " + modname + "!") continue entity_list.append(name_idurl_dict[modname]) dict_json = {"entityTypeNames": entity_list} response = post_query("agent/entityTypes/:batchDelete", data=json.dumps(dict_json)) status_details(response)