Esempio n. 1
0
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
Esempio n. 2
0
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('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))

    messages = res['response']
    for message in messages:
        sys.stdout.write('Dialog> %s\n' % (message))

    # set input encoding - change the encoding name according your platform
    sys.stdin  = codecs.getreader('shift_jis')(sys.stdin)
    
Esempio n. 4
0
    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))

    messages = res['response']
    for message in messages:
        sys.stdout.write('Dialog> %s\n' % (message))

    # set input encoding - change the encoding name according your platform
    sys.stdin = codecs.getreader('shift_jis')(sys.stdin)