def __init__(self, username, password, file, dialog_name, url=None, dialog_id_file=DIALOG_ID_FILE): self._check_file(file) self.dialog_id_file = dialog_id_file self.dialog_name = dialog_name if url is None: url = Dialog.default_url self.dialog = Dialog(url, username=username, password=password) self.file = file self.conversation_id = None self._profile = None self.last_response = None self.last_response_raw = None self.dialog_id = None
def auto_call(): # Get Emergency Service for location # Dial number (Would be emergency service) response = VoiceResponse() dial = Dial() dial.number('470-705-7212') response.append(dial) print(response) # Get pickup status response = VoiceResponse() response.dial('415-123-4567', action='/handleDialCallStatus', method='GET') response.say('I am unreachable') # Twilio will submit a request to the action URL with the parameter 'DialCallStatus'. # If nobody picks up, 'DialCallStatus' will be 'no-answer'. # If the line is busy, 'DialCallStatus' will be 'busy'. # If the called party picks up, 'DialCallStatus' will be 'completed'. # If a conference is successfully dialed, 'DialCallStatus' will be 'answered'. # If an invalid phone number was provided, 'DialCallStatus' will be 'failed'. print(response) # Automated Voice Intro = "(person's name)" # Get information from Smart911Connect Account # http://www.smart911connect.com/get-access/ # Development Block :( # no free APIs that send medical info to Emergency Services, however they all return JSON # Use Mongoose, JSON -> DB -> JSON # Get information as JSON - Python NamedTuple -> String # Feed into IBM Watson import json from watson_developer_cloud import DialogV1 as Dialog dialog = Dialog(username='******', password='******') dialog_id = 'YOUR_DIALOGID' conversation = dialog.conversation(dialog_id) client_id = conversation['client_id'] print dialog.update_profile(dialog_id=dialog_id, client_id=client_id, name_values={"name_values": [{"name": "Product_Name", "value": "Sunglasses"}]}) print dialog.update_profile(dialog_id=dialog_id, client_id=client_id, name_values=[{"name": "Product_Name", "value": "Sunglasses"}]) # Operator Interface with Twilio return
def __init__(self, directory): # Open STT Service with open(join(directory, "sttConfig.json")) as confile: stt_config = json.load(confile)['credentials'] self.speechToText = SpeechToTextV1(username=stt_config['username'], password=stt_config['password']) # Open TTS Service with open(join(directory, "ttsConfig.json")) as confile: tts_config = json.load(confile)['credentials'] self.textToSpeech = TextToSpeechV1(username=tts_config['username'], password=tts_config['password']) # Open the Dialog Service with open(join(directory, "dialogConfig.json")) as confile: dialog_config = json.load(confile)['credentials'] self.dialogID = dialog_config['id'] self.dialog = DialogV1(username=dialog_config['username'], password=dialog_config['password']) self.response = self.dialog.conversation(self.dialogID) self.workingDirectory = directory
# coding=utf-8 import json from os.path import join, dirname from watson_developer_cloud import DialogV1 as Dialog dialog = Dialog(username='******', password='******') print(json.dumps(dialog.get_dialogs(), indent=2)) # CREATE A DIALOG # with open(join(dirname(__file__), '../resources/dialog.xml') as dialog_file: # print(json.dumps(dialog.create_dialog( # dialog_file=dialog_file, name='pizza_test_9'), indent=2)) # dialog_id = '98734721-8952-4a1c-bb72-ef9957d4be93' # with open(join(dirname(__file__), '../resources/dialog.xml') as dialog_file: # print(json.dumps(dialog.update_dialog(dialog_file=dialog_file, dialog_id=dialog_id), indent=2)) # print(json.dumps(dialog.get_content(dialog_id), indent=2)) # # initial_response = dialog.conversation(dialog_id) # # print(json.dumps(initial_response, indent=2)) # # print(json.dumps(dialog.conversation(dialog_id=dialog_id, # dialog_input='What type of toppings do you have?', # conversation_id=initial_response['conversation_id'], # client_id=initial_response['client_id']), indent=2))
# coding=utf-8 import json from os.path import join, dirname from watson_developer_cloud import DialogV1 dialog = DialogV1(username='******', password='******') print(json.dumps(dialog.get_dialogs(), indent=2)) # print(json.dumps(dialog.get_dialog('6250d170-41d6-468a-a697-5675578c8012'), indent=2)) # CREATE A DIALOG # with open(join(dirname(__file__), '../resources/dialog.xml') as dialog_file: # print(json.dumps(dialog.create_dialog( # dialog_file=dialog_file, name='pizza_test_9'), indent=2)) # dialog_id = '98734721-8952-4a1c-bb72-ef9957d4be93' # with open(join(dirname(__file__), '../resources/dialog.xml') as dialog_file: # print(json.dumps(dialog.update_dialog(dialog_file=dialog_file, dialog_id=dialog_id), indent=2)) # print(json.dumps(dialog.get_content(dialog_id), indent=2)) # # initial_response = dialog.conversation(dialog_id) # # print(json.dumps(initial_response, indent=2)) # # print(json.dumps(dialog.conversation(dialog_id=dialog_id, # dialog_input='What type of toppings do you have?', # conversation_id=initial_response['conversation_id'],
for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt in ("-f", "---dialogfile"): dialog_filepath = arg elif opt in ("-a", "---dialog_id"): dialog_id = arg elif opt == '-d': DEBUG = True if not dialog_filepath or not dialog_id: print('Required argument missing.') usage() sys.exit(2) try: # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # update the dialog sys.stdout.write('Updating dialog %s\n' % dialog_id) with open(dialog_filepath, 'rb') as dialog_file: res = dialog.update_dialog(dialog_id, dialog_file) sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) except Exception as e: sys.stdout.write(str(e)) exit(1)
from os.path import join, dirname from time import sleep from dialog.settings import WATSON_USERNAME, WATSON_PASSWORD, WATSON_DIALOG_ID from watson_developer_cloud import DialogV1 as Dialog from dialog_build_file import build build() dialog_service = Dialog(username=WATSON_USERNAME, password=WATSON_PASSWORD) dialog_id = WATSON_DIALOG_ID sleep(4) with open(join(dirname(__file__), 'dialog_files/jemboo-dialog-file.xml'), mode="r") as dialog_file: dialog_service.update_dialog(dialog_file=dialog_file, dialog_id=dialog_id) print("done")
class Client: DIALOG_ID_FILE = './dialog_id_file.txt' def __init__(self, username, password, file, dialog_name, url=None, dialog_id_file=DIALOG_ID_FILE): self._check_file(file) self.dialog_id_file = dialog_id_file self.dialog_name = dialog_name if url is None: url = Dialog.default_url self.dialog = Dialog(url, username=username, password=password) self.file = file self.conversation_id = None self._profile = None self.last_response = None self.last_response_raw = None self.dialog_id = None def clean_dialogs(self): dialogs = self.get_dialogs() for dialog in dialogs['dialogs']: self.dialog.delete_dialog(dialog['dialog_id']) def get_dialogs(self): return self.dialog.get_dialogs() def _check_file(self, file): if not os.path.exists(file): raise ExceptionDialogFile("File '" + file + "' doesn't exist") def _validate_xml(self, file): if not etree: return data = pkgutil.get_data('dialog_watson_client', 'WatsonDialogDocument_1.0.xsd') dataIo = StringIO.StringIO(data) xsd_doc = etree.parse(dataIo) xsd = etree.XMLSchema(xsd_doc) xml = etree.parse(file) xsd.assertValid(xml) def _is_dialog_id_created(self): if not os.path.exists(self.dialog_id_file): return False if not os.path.getmtime(self.file) == os.path.getmtime(self.dialog_id_file): return False return True def get_dialog_id(self): if not os.path.exists(self.dialog_id_file): return None if self.dialog_id is not None: return self.dialog_id dialogIdFile = open(self.dialog_id_file, 'r') dialogId = dialogIdFile.readline() dialogIdFile.close() self.dialog_id = dialogId return dialogId def delete_dialog(self): dialogId = self.get_dialog_id() resp = None if dialogId is not None and not dialogId: resp = self.dialog.delete_dialog(dialogId) os.remove(self.dialog_id_file) return resp def update_dialog(self): dialogId = self.get_dialog_id() resp = None if dialogId is not None and not dialogId: with open(self.file) as dialogFile: resp = self.dialog.update_dialog(dialogId, dialogFile) return resp def _touch(self, fname): if os.path.exists(fname): os.utime(fname, None) else: open(fname, 'a').close() def create_or_update_dialog(self): if self._is_dialog_id_created(): return dialogId = self.get_dialog_id() filename, ext = os.path.splitext(self.file) if ext == 'xml': self._validate_xml(self.file) if dialogId is not None and self.conversation_id is None: self.update_dialog() else: with open(self.file) as dialogFile: resp = self.dialog.create_dialog(dialogFile, self.dialog_name) f = open(self.dialog_id_file, 'w') f.write(resp['dialog_id']) f.close() self._touch(self.file) def get_last_response(self): return self.last_response def get_last_response_json(self): return self.last_response_raw def _update_response(self, jsonResp): self.last_response_raw = jsonResp self.last_response = DialogResponse(jsonResp['response']) if 'confidence' in jsonResp: self.last_response.confidence = jsonResp['confidence'] if 'input' in jsonResp: self.last_response.input = jsonResp['input'] def start_dialog(self): self.create_or_update_dialog() dialogId = self.get_dialog_id() initial_response = self.dialog.conversation(dialogId) self.last_response_raw = initial_response self.conversation_id = initial_response['conversation_id'] self._profile = Profile(initial_response['client_id']) self._update_response(initial_response) return self.last_response def converse(self, user_input): dialogId = self.get_dialog_id() if self._profile is None: self.start_dialog() resp = self.dialog.conversation(dialogId, dialog_input=user_input, client_id=self._profile.client_id, conversation_id=self.conversation_id) self._update_response(resp) return self.last_response def get_profile(self): dialogId = self.get_dialog_id() if self._profile is None: self.start_dialog() resp = self.dialog.get_profile(dialogId, self._profile.client_id) self._profile.load_data(resp["name_values"]) return self._profile def get_conversation(self, date_from, date_to): dialogId = self.get_dialog_id() return self.dialog.get_conversation(dialogId, date_from, date_to) def update_profile(self): dialogId = self.get_dialog_id() profile = self.get_profile() data = profile.get_data() name_values = []; for (name, value) in data.items(): name_values.append({ 'name': name, 'value': value }) return self.dialog.update_profile(dialogId, profile.client_id, name_values)
print(str(err)) print(usage()) sys.exit(2) for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt in ("-a", "---dialog_id"): dialog_id = arg elif opt == '-d': DEBUG = True if not dialog_id: print('dialog_id is missing.') usage() sys.exit(2) try: # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # delete the specified dialog sys.stdout.write('Deleting the dialog account %s ...\n' % dialog_id) res = dialog.delete_dialog(dialog_id) sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) except Exception as e: sys.stdout.write(str(e)) exit(1)
client_id = arg elif opt in ("-o", "--conversatin_id"): conversation_id = arg elif opt == '-d': DEBUG = True if not dialog_id: print('dialog_id is missing.') usage() sys.exit(2) try: sys.stdout.write('Watson Dialog conversation app. (Ctrl-z for terminating.)\n') # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # check dialog_id and get from constant if dialog_id =='': dialog_id = dialogConstants.getDialogId() # start conversation to get client_id and conversation_id res = dialog.conversation(dialog_id, '', client_id, conversation_id) if DEBUG: sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) client_id = res['client_id'] sys.stdout.write('client_id = %s\n' % (client_id)) conversation_id = res['conversation_id'] sys.stdout.write('conversation_id = %s\n' % (conversation_id))
for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt in ("-f", "---dialogfile"): dialog_filepath = arg elif opt in ("-n", "---name"): name = arg elif opt == '-d': DEBUG = True if not dialog_filepath or not name: print('Required argument missing.') usage() sys.exit(2) try: # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # create a dialog sys.stdout.write('Creating dialog %s\n' % name) with open(dialog_filepath, 'rb') as dialog_file: res = dialog.create_dialog(dialog_file, name) sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) except Exception as e: sys.stdout.write(str(e)) exit(1)
from os.path import join, dirname from time import sleep from dialog.settings import WATSON_USERNAME, WATSON_PASSWORD, WATSON_DIALOG_ID from watson_developer_cloud import DialogV1 as Dialog from dialog_build_file import build build() dialog_service = Dialog( username=WATSON_USERNAME, password=WATSON_PASSWORD ) dialog_id = WATSON_DIALOG_ID sleep(4) with open(join(dirname(__file__), 'dialog_files/jemboo-dialog-file.xml'), mode="r") as dialog_file: dialog_service.update_dialog(dialog_file=dialog_file, dialog_id=dialog_id) print("done")
elif opt in ("-o", "--conversatin_id"): conversation_id = arg elif opt == '-d': DEBUG = True if not dialog_id: print('dialog_id is missing.') usage() sys.exit(2) try: sys.stdout.write( 'Watson Dialog conversation app. (Ctrl-z for terminating.)\n') # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # check dialog_id and get from constant if dialog_id == '': dialog_id = dialogConstants.getDialogId() # start conversation to get client_id and conversation_id res = dialog.conversation(dialog_id, '', client_id, conversation_id) if DEBUG: sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) client_id = res['client_id'] sys.stdout.write('client_id = %s\n' % (client_id)) conversation_id = res['conversation_id'] sys.stdout.write('conversation_id = %s\n' % (conversation_id))
opts, args = getopt.getopt(sys.argv[1:], "hd", []) except getopt.GetoptError as err: print(str(err)) print(usage()) sys.exit(2) for opt, arg in opts: if opt == '-h': usage() sys.exit() elif opt == '-d': DEBUG = True try: # get dialog object dialog = Dialog(username=dialogConstants.getUsername(), password=dialogConstants.getPassword()) # list dialogs to get client_id and conversation_id res = dialog.get_dialogs() if DEBUG: sys.stdout.write('Response: \n%s\n' % json.dumps(res, indent=2)) sys.stdout.write('Dialog accounts:\n') sys.stdout.write('\tdialog_id,\tname\n') dialogs = res['dialogs'] for dialog in dialogs: sys.stdout.write('\t%s : %s\n' % (dialog['dialog_id'], dialog['name'])) sys.stdout.write('\n') sys.stdout.write('Language packs accounts:\n') sys.stdout.write('\tdialog_id,\tname\n')