def test_update_entity(): endpoint = '/v1/workspaces/{0}/entities/{1}'.format('boguswid', 'pizza_toppings') url = '{0}{1}'.format(base_url, endpoint) response = { "entity": "pizza_toppings", "description": "Tasty pizza toppings", "created": "2015-12-06T04:32:20.000Z", "updated": "2015-12-07T18:53:59.153Z", "metadata": { "property": "value" } } responses.add( responses.POST, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-04-21') entity = service.update_entity( workspace_id='boguswid', entity='pizza_toppings', new_entity='pizza_toppings') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert entity == response
def test_list_entities(): endpoint = '/v1/workspaces/{0}/entities'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) response = { "entities": [{ "entity": "pizza_toppings", "description": "Tasty pizza toppings", "created": "2015-12-06T04:32:20.000Z", "updated": "2015-12-07T18:53:59.153Z", "metadata": { "property": "value" } }], "pagination": { "refresh_url": "/v1/workspaces/pizza_app-e0f3/entities?version=2017-12-18&filter=name:pizza&include_count=true&page_limit=1", "next_url": "/v1/workspaces/pizza_app-e0f3/entities?cursor=base64=&version=2017-12-18&filter=name:pizza&page_limit=1", "total": 1, "matched": 1 } } responses.add(responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1(username='******', password='******', version='2017-04-21') entities = service.list_entities(workspace_id='boguswid', export=True) assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert entities == response
def test_update_synonym(): endpoint = '/v1/workspaces/{0}/entities/{1}/values/{2}/synonyms/{3}'.format( 'boguswid', 'grilling', 'bbq', 'barbecue') url = '{0}{1}'.format(base_url, endpoint) response = { "synonym": "barbecue", "created": "2015-12-06T23:53:59.153Z", "updated": "2015-12-07T18:53:59.153Z" } responses.add(responses.POST, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1(username='******', password='******', version='2017-04-21') synonym = service.update_synonym(workspace_id='boguswid', entity='grilling', value='bbq', synonym='barbecue', new_synonym='barbecue') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert synonym == response
def test_list_examples(): endpoint = '/v1/workspaces/{0}/intents/{1}/examples'.format( 'boguswid', 'pizza_order') url = '{0}{1}'.format(base_url, endpoint) response = { "examples": [{ "text": "Can I order a pizza?", "created": "2016-07-11T16:39:01.774Z", "updated": "2015-12-07T18:53:59.153Z" }, { "text": "Gimme a pizza with pepperoni", "created": "2016-07-11T16:39:01.774Z", "updated": "2015-12-07T18:53:59.153Z" }], "pagination": { "refresh_url": "/v1/workspaces/pizza_app-e0f3/intents/order/examples?version=2017-12-18&page_limit=2", "next_url": "/v1/workspaces/pizza_app-e0f3/intents/order/examples?cursor=base64=&version=2017-12-18&page_limit=2" } } responses.add( responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') examples = service.list_examples( workspace_id='boguswid', intent='pizza_order') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert examples == response
def test_list_counterexamples(): endpoint = '/v1/workspaces/{0}/counterexamples'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) response = { "counterexamples": [{ "text": "I want financial advice today.", "created": "2016-07-11T16:39:01.774Z", "updated": "2015-12-07T18:53:59.153Z" }, { "text": "What are you wearing today", "created": "2016-07-11T16:39:01.774Z", "updated": "2015-12-07T18:53:59.153Z" }], "pagination": { "refresh_url": "/v1/workspaces/pizza_app-e0f3/counterexamples?version=2017-12-18&page_limit=2", "next_url": "/v1/workspaces/pizza_app-e0f3/counterexamples?cursor=base64=&version=2017-12-18&page_limit=2" } } responses.add( responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') counterexamples = service.list_counterexamples(workspace_id='boguswid') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert counterexamples == response
def test_list_intents(): endpoint = '/v1/workspaces/{0}/intents'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) response = { "intents": [{ "intent": "pizza_order", "created": "2015-12-06T23:53:59.153Z", "updated": "2015-12-07T18:53:59.153Z", "description": "User wants to start a new pizza order" }], "pagination": { "refresh_url": "/v1/workspaces/pizza_app-e0f3/intents?version=2017-12-18&page_limit=1", "next_url": "/v1/workspaces/pizza_app-e0f3/intents?cursor=base64=&version=2017-12-18&page_limit=1" } } responses.add( responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') intents = service.list_intents(workspace_id='boguswid', export=False) assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert intents == response
def test_update_example(): endpoint = '/v1/workspaces/{0}/intents/{1}/examples/{2}'.format( 'boguswid', 'pizza_order', 'Gimme%20a%20pizza%20with%20pepperoni') url = '{0}{1}'.format(base_url, endpoint) response = { "text": "Gimme a pizza with pepperoni", "created": "2016-07-11T23:53:59.153Z", "updated": "2015-12-07T18:53:59.153Z" } responses.add( responses.POST, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') example = service.update_example( workspace_id='boguswid', intent='pizza_order', text='Gimme a pizza with pepperoni', new_text='Gimme a pizza with pepperoni') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert example == response
def test_update_intent(): endpoint = '/v1/workspaces/{0}/intents/{1}'.format('boguswid', 'pizza_order') url = '{0}{1}'.format(base_url, endpoint) response = { "intent": "pizza_order", "created": "2015-12-06T23:53:59.153Z", "updated": "2015-12-07T18:53:59.153Z", "description": "User wants to start a new pizza order" } responses.add( responses.POST, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') intent = service.update_intent( workspace_id='boguswid', intent='pizza_order', new_intent='pizza_order', new_description='User wants to start a new pizza order') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert intent == response
def test_get_workspace(): workspace_url = "{0}{1}/boguswid".format(base_url, "/workspaces") message_response = { "name": "development", "created": "2017-02-09T18:41:19.890Z", "updated": "2017-02-09T18:41:19.890Z", "language": "en", "metadata": None, "description": "this is a workspace", "workspace_id": "boguswid", "status": 'Training', } responses.add(responses.GET, workspace_url, body=json.dumps(message_response), status=200, content_type='application/json') conversation = watson_developer_cloud.ConversationV1(username="******", password="******", version='2016-09-20') workspace = conversation.get_workspace(workspace_id='boguswid') assert workspace['name'] == 'development' assert workspace['workspace_id'] == 'boguswid' assert workspace['status'] == 'Training' workspace = conversation.get_workspace(workspace_id='boguswid', export=True) assert workspace['status'] == 'Training'
def new_conversation(): app.logger.info('setting up watson cloud API') return watson_developer_cloud.ConversationV1( username='******', password='******', version='2018-03-12' )
def test_get_value(): endpoint = '/v1/workspaces/{0}/entities/{1}/values/{2}'.format( 'boguswid', 'grilling', 'bbq') url = '{0}{1}'.format(base_url, endpoint) response = { "value": "BBQ sauce", "metadata": { "code": 1422 }, "created": "2015-12-06T23:53:59.153Z", "updated": "2015-12-07T18:53:59.153Z" } responses.add( responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-04-21') value = service.get_value( workspace_id='boguswid', entity='grilling', value='bbq', export=True) assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert value == response
def test_message(): # Ranker endpoints conversation = watson_developer_cloud.ConversationV1(username="******", password="******", version='2016-09-20') workspace_id = 'f8fdbc65-e0bd-4e43-b9f8-2975a366d4ec' message_url = 'https://gateway.watsonplatform.net/conversation/api/v1/workspaces/%s/message' % workspace_id message_url1 = 'https://gateway.watsonplatform.net/conversation/api/v1/workspaces/%s/message?version=2016-09-20' % workspace_id message_response = '{"context":{"conversation_id":"1b7b67c0-90ed-45dc-8508-9488bc483d5b","system":{"dialog_stack":["root"],"dialog_turn_counter":1,"dialog_request_counter":1}},"intents":[],"entities":[],"input":{}}' responses.add(responses.POST, message_url, body=message_response, status=200, content_type='application/json') message = conversation.message(workspace_id=workspace_id, message_input={'text': 'Turn on the lights'}, context=None) assert message is not None assert responses.calls[0].request.url == message_url1 assert responses.calls[0].response.text == message_response # test context responses.add(responses.POST, message_url, body=message_response, status=200, content_type='application/json') message = conversation.message(workspace_id=workspace_id, message_input={'text': 'Turn on the lights'}, context={'context': {'conversation_id':'1b7b67c0-90ed-45dc-8508-9488bc483d5b', 'system': {'dialog_stack':['root'], 'dialog_turn_counter':2, 'dialog_request_counter':1}}}) assert message is not None assert responses.calls[1].request.url == message_url1 assert responses.calls[1].response.text == message_response assert len(responses.calls) == 2
def test_list_workspaces(): endpoint = '/v1/workspaces' url = '{0}{1}'.format(base_url, endpoint) response = { "workspaces": [{ "name": "Pizza app", "created": "2015-12-06T23:53:59.153Z", "language": "en", "metadata": {}, "updated": "2015-12-06T23:53:59.153Z", "description": "Pizza app", "workspace_id": "pizza_app-e0f3" }], "pagination": { "refresh_url": "/v1/workspaces?version=2016-01-24&page_limit=1", "next_url": "/v1/workspaces?cursor=base64=&version=2016-01-24&page_limit=1" } } responses.add( responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') workspaces = service.list_workspaces() assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert workspaces == response
def test_update_workspace(): endpoint = '/v1/workspaces/{0}'.format('pizza_app-e0f3') url = '{0}{1}'.format(base_url, endpoint) response = { "name": "Pizza app", "created": "2015-12-06T23:53:59.153Z", "language": "en", "metadata": {}, "updated": "2015-12-06T23:53:59.153Z", "description": "Pizza app", "workspace_id": "pizza_app-e0f3" } responses.add( responses.POST, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') workspace = service.update_workspace( workspace_id='pizza_app-e0f3', name='Pizza app', description='Pizza app', language='en', metadata={}) assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert workspace == response
def test_list_logs(): endpoint = '/v1/workspaces/{0}/logs'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) response = { "logs": [{ "request": { "input": { "text": "Can you turn off the AC" }, "context": {} }, "response": { "input": { "text": "Can you turn off the AC" }, "context": { "conversation_id": "f2c7e362-4cc8-4761-8b0f-9ccd70c63bca", "system": { "dialog_stack": ["root"], "dialog_turn_counter": 1, "dialog_request_counter": 1 }, "defaultCounter": 0 }, "entities": [], "intents": [{ "intent": "turn_off", "confidence": 0.9332477126694649 }], "output": { "log_messages": [], "text": [ "Hi. It looks like a nice drive today. What would you like me to do?" ], "nodes_visited": ["node_1_1467221909631"] } }, "request_timestamp": "2016-07-16T09:22:38.960Z", "response_timestamp": "2016-07-16T09:22:39.011Z", "log_id": "e70d6c12-582d-47a8-a6a2-845120a1f232" }], "pagination": { "next_url": "/v1/workspaces/15fb0e8a-463d-4fec-86aa-a737d9c38a32/logs?cursor=dOfVSuh6fBpDuOxEL9m1S7JKDV7KLuBmRR+lQG1s1i/rVnBZ0ZBVCuy53ruHgPImC31gQv5prUsJ77e0Mj+6sGu/yfusHYF5&version=2016-07-11&filter=response.top_intent:turn_off&page_limit=1", "matched": 215 } } responses.add(responses.GET, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1(username='******', password='******', version='2017-04-21') logs = service.list_logs(workspace_id='boguswid') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert logs == response
def callWatsonConversation(input): conversation = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-05-26') response = conversation.message( workspace_id='1c9b588c-8b0e-4ae3-a7e1-83588a30ae81', message_input={'text': input}) return str((response["output"]["text"][0]))
def getConversationSevice(): if 'VCAP_SERVICES' in os.environ: vcap = json.loads(os.getenv('VCAP_SERVICES')) if 'conversation' in vcap: creds = vcap['conversation'][0]['credentials'] user = creds['username'] password = creds['password'] conversation = watson.ConversationV1(username=user, password=password, version='2017-04-21') return conversation elif os.path.isfile('credentials.json'): with open('credentials.json') as f: vcap = json.load(f) creds = vcap['services']['conversation'][0]['credentials'] user = creds['username'] password = creds['password'] conversation = watson.ConversationV1(username=user, password=password, version='2017-04-21') return conversation
def connectToTwitch(): global count_ds try: client = socket.socket() dat = ("irc.chat.twitch.tv",6667) client.connect( ("irc.chat.twitch.tv",6667)) message_user = '******' message = "PASS " + config.OAUTH_TOKEN +"\r\n" client.sendto(message.encode('utf-8'),dat) client.sendto(message_user.encode('utf-8'),dat) client.sendto('JOIN #sail338 \r\n'.encode(),dat) conversation = watson_developer_cloud.ConversationV1( username = config.USERNAME, password = config.PASSWORD, version = '2017-05-26' ) workspace_id = config.WORKSPACE_ID #start polling printit() pygame.mixer.music.load("Creepy-Music.mp3") pygame.mixer.music.play() while True: #poll for 5 seconds did_pool = False data = client.recv(1024) print(data) if data != "b''": #Parse data data = str(data) parsed = data.split(":") if len(parsed) >2: check = parsed[2] check = check.strip("\\r\\n") check = check.replace("\\r\\n'","") response = conversation.message( workspace_id = workspace_id, input = { 'text': check } ) intent = response['output']['text'] print(intent) if(len(intent)>0): check = intent[0] if check in intents: print("Found something to increment") count_ds[check] +=1 except Exception as e: print(str(e))
def __init__(self): #Constructor to initialize conversation tokens self.last_intent = '' self.conversation = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-12-05') self.workspace_id = '296bb520-681b-4bcc-86f8-55c001aad75f' self.num_queries = 0 self.context = {}
def initialize_watson(): conversation = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-05-26') response = conversation.message( workspace_id='7e437f5e-946b-43de-986e-4fc6968f9130') conversation_id = response['context']['conversation_id'] outputs = response['output']['text'] this_context = response['context'] return (conversation, conversation_id, outputs, this_context)
def test_update_workspace(): workspace_data = { "name": "development", "intents": [{ "intent": "orderpizza", "examples": [{ "text": "can I order a pizza?" }, { "text": "want order a pizza" }, { "text": "pizza order" }, { "text": "pizza to go" }], "description": None }], "entities": [], "language": "en", "metadata": {}, "description": "this is a development workspace", "dialog_nodes": [], "counterexamples": [], "workspace_id": 'boguswid' } workspace_url = "{0}{1}".format(base_url, "/workspaces/boguswid") message_response = workspace_data responses.add(responses.POST, workspace_url, body=json.dumps(message_response), status=200, content_type='application/json') conversation = watson_developer_cloud.ConversationV1(username="******", password="******", version='2016-09-20') workspace = conversation.update_workspace( 'boguswid', name=workspace_data['name'], description=workspace_data['description'], language=workspace_data['language'], intents=workspace_data['intents'], metadata=workspace_data['metadata'], counterexamples=workspace_data['counterexamples'], dialog_nodes=workspace_data['dialog_nodes'], entities=workspace_data['entities']) assert len(responses.calls) == 1 assert workspace == message_response
def bot_talk(): message = request.values.get('Body', None) number = request.values.get('From', None) twilioNumber = request.values.get('To') context = None index = 0 contextIndex = 0 for cont in contexts: print(cont.get("From", None)) if cont.get("From", None) == number: context = cont.get("Body", None) contextIndex = index index = index + 1 print("Received Message from " + number + "saying " + message) conversation = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-05-26') response = conversation.message( workspace_id='1d7276eb-2bff-4e67-b8fc-8261df022546', message_input={'text': message}) account_sid = "ACc5173ddda5dea6f7c9b4398c0f80d545" auth_token = "87e59826d7f49301f6bebedf32d21d51" client = Client(account_sid, auth_token) try: def new(response): #resp= if context == None: contexts.append({'from': number, 'context': response.context}) else: contexts[contextIndex].context = response.context if response["intents"][0]['intent'] == "Redzone": apple = str(redzone()) else: apple = response["output"]["text"][0] message1 = client.messages.create(to=number, from_=twilioNumber, body=apple) except: message1 = client.messages.create(to=number, from_=twilioNumber, body="I dont understand")
def test_delete_workspace(): workspace_url = "{0}{1}/boguswid".format(base_url, "/workspaces") message_response = {} responses.add(responses.DELETE, workspace_url, body=json.dumps(message_response), status=200, content_type='application/json') conversation = watson_developer_cloud.ConversationV1(username="******", password="******", version='2016-09-20') workspace = conversation.delete_workspace(workspace_id='boguswid') assert len(responses.calls) == 1 assert workspace == {}
def __init__(self, username, password, workspace_name): # Get Watson Workspace self.conversation = watson_developer_cloud.ConversationV1( username=username, password=password, version='2017-05-26' ) watson_workspaces = self.conversation.list_workspaces() self.workspace_id = None for workspace in watson_workspaces["workspaces"]: if workspace["name"] == workspace_name: self.workspace_id = workspace["workspace_id"] # Logging self.pp = pprint.PrettyPrinter(indent=4) self.logger = logging.getLogger('watson_sync({})'.format(workspace_name))
def test_delete_workspace(): endpoint = '/v1/workspaces/{0}'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) response = {} responses.add( responses.DELETE, url, body=json.dumps(response), status=200, content_type='') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') workspace = service.delete_workspace(workspace_id='boguswid') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert workspace == response
def test_delete_entity(): endpoint = '/v1/workspaces/{0}/entities/{1}'.format('boguswid', 'pizza_toppings') url = '{0}{1}'.format(base_url, endpoint) response = "" responses.add( responses.DELETE, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-04-21') entity = service.delete_entity(workspace_id='boguswid', entity='pizza_toppings') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert entity == response
def test_unknown_error(): endpoint = '/v1/workspaces/{0}/counterexamples'.format('boguswid') url = '{0}{1}'.format(base_url, endpoint) error_msg = 'Unknown error' responses.add( responses.POST, url, status=407, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') try: service.create_counterexample( workspace_id='boguswid', text='I want financial advice today.') except WatsonException as ex: assert len(responses.calls) == 1 assert error_msg in str(ex)
def test_delete_counterexample(): endpoint = '/v1/workspaces/{0}/counterexamples/{1}'.format( 'boguswid', 'I%20want%20financial%20advice%20today') url = '{0}{1}'.format(base_url, endpoint) response = {} responses.add( responses.DELETE, url, body=json.dumps(response), status=200, content_type='application/json') service = watson_developer_cloud.ConversationV1( username='******', password='******', version='2017-02-03') counterexample = service.delete_counterexample( workspace_id='boguswid', text='I want financial advice today') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert counterexample == response
def sent(msg): global conversation global conversation_id if conversation is None: conversation = watson_developer_cloud.ConversationV1( iam_api_key= "Wu5glUEuIEvEQnyDk_66YODTZDz9TDeGzm1C2rZng_5I", version='2018-07-10' ) conversation.set_url("https://gateway-syd.watsonplatform.net/assistant/api") response = conversation.message( workspace_id='09b80ae8-ae6e-422c-ada5-fdb44e300f44', input={ 'text': msg }, alternate_intents=True ) conversation_id = response['context']['conversation_id'] return response
def test_delete_example(): endpoint = '/v1/workspaces/{0}/intents/{1}/examples/{2}'.format( 'boguswid', 'pizza_order', 'Gimme%20a%20pizza%20with%20pepperoni') url = '{0}{1}'.format(base_url, endpoint) response = {} responses.add(responses.DELETE, url, body=json.dumps(response), status=200, content_type='') service = watson_developer_cloud.ConversationV1(username='******', password='******', version='2017-02-03') example = service.delete_example(workspace_id='boguswid', intent='pizza_order', text='Gimme a pizza with pepperoni') assert len(responses.calls) == 1 assert responses.calls[0].request.url.startswith(url) assert example == response