Created
February 1, 2018 22:02
-
-
Save mil06/c48df3955cb574f8043d4baa6c84ae92 to your computer and use it in GitHub Desktop.
Posting new entities to dialog flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # from __future__ import print_function | |
| # import httplib2 | |
| # import os | |
| # from apiclient import discovery | |
| # from oauth2client import client | |
| # from oauth2client import tools | |
| # from oauth2client.file import Storage | |
| import urllib.request as urllibReq | |
| import urllib.parse as urllibParse | |
| import json | |
| import csv | |
| filename = 'account_mapping.csv' | |
| entity_to_update = 'Account' | |
| dialogflow_url_entities = 'https://api.dialogflow.com/v1/entities' | |
| headers = {'Authorization': 'Bearer 894f03e1b38246fe9db4305e8a1d5579', 'Content-Type': 'application/json'} | |
| def readFile(): | |
| with open(filename) as csvfile: | |
| readCSV = csv.reader(csvfile, delimiter=',') | |
| entities_from_file = set() | |
| counter = 0; | |
| for row in readCSV: | |
| if(row[4] is not None and counter<10): | |
| entities_from_file.add(row[4].lower()) | |
| counter = counter + 1 | |
| return entities_from_file | |
| def getEntityID(): | |
| entity_id = '' | |
| url = dialogflow_url_entities+'?v=20150910' | |
| req = urllibReq.Request(url, headers=headers) | |
| response = urllibReq.urlopen(req) | |
| entities_DF = json.loads(response.read()) | |
| response.close() | |
| for entity in entities_DF: | |
| if entity['name']==entity_to_update: | |
| entity_id = entity['id'] | |
| break | |
| return entity_id | |
| def compareEntitiesToUpdate(entity_id, entities_from_file): | |
| url = dialogflow_url_entities + '/' + entity_id + '?v=20150910' | |
| req = urllibReq.Request(url, headers=headers) | |
| response = urllibReq.urlopen(req) | |
| entity_values = json.loads(response.read()) | |
| response.close() | |
| entries = entity_values['entries'] | |
| print('LENGTH OF ENTITIES BEFORE COMPARE: ' + str(len(entities_from_file))) | |
| for entity in entries: | |
| if entity['value'].lower() in entities_from_file: | |
| entities_from_file.remove(entity['value'].lower()) | |
| print('Match') | |
| print('LENGTH OF ENTITIES AFTER COMPARE: ' + str(len(entities_from_file))) | |
| return formatEntities(entities_from_file) | |
| def updateEntitiesToDialogFlow(entity_id, formated_entities): | |
| urlPost = dialogflow_url_entities + '/' + entity_id + '/entries?v=20150910' | |
| data = urllibParse.quote(str(formated_entities)) | |
| data = data.encode('utf-8') | |
| reqPost = urllibReq.Request(urlPost, data, headers) | |
| responsePost = urllibReq.urlopen(reqPost) | |
| responseText = responsePost.read() | |
| print('return from post call: ' + str(responseText)) | |
| print('info return: ' + str(responsePost.info())) | |
| def formatEntities(entities_from_file): | |
| formated_accounts = [] | |
| for entity in entities_from_file: | |
| formated_entity = { "synonyms": [entity], "value": entity} | |
| formated_accounts.append(formated_entity) | |
| return formated_accounts | |
| entities = readFile() | |
| print('DONE READING FILE') | |
| entityID = getEntityID() | |
| print('ENTITY ID: ' + str(entityID)) | |
| formatedEntities = compareEntitiesToUpdate(entityID, entities) | |
| print(str(formatedEntities)) | |
| updateEntitiesToDialogFlow(entityID, formatedEntities) | |
| # if __name__ == '__main__': | |
| # main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment