class Bot(object): def __init__(self): self.nlg = NLG(user_name=my_name) self.speech = Speech(launch_phrase=launch_phrase, debugger_enabled=debugger_enabled) self.knowledge = Knowledge(weather_api_token) self.vision = Vision(camera=camera) def start(self): """ Main loop. Waits for the launch phrase, then decides an action. :return: """ while True: requests.get("http://localhost:8080/clear") if self.vision.recognize_face(): print "Found face" if use_launch_phrase: recognizer, audio = self.speech.listen_for_audio() if self.speech.is_call_to_action(recognizer, audio): self.__acknowledge_action() self.decide_action() else: self.decide_action() def decide_action(self): """ Recursively decides an action based on the intent. :return: """ recognizer, audio = self.speech.listen_for_audio() # received audio data, now we'll recognize it using Google Speech Recognition speech = self.speech.google_speech_recognition(recognizer, audio) #speech = "can you show me a map of Pune" #print speech if speech is not None: try: r = requests.get('https://api.wit.ai/message?v=20160918&q=%s' % speech, headers={"Authorization": wit_ai_token}) print r.text json_resp = json.loads(r.text) entities = None intent = None Status_Type = None if 'entities' in json_resp and 'Intent' in json_resp['entities']: entities = json_resp['entities'] intent = json_resp['entities']['Intent'][0]["value"] print intent if 'Status_Type' in json_resp['entities'] : entities = json_resp['entities'] Status_Type = json_resp['entities']['Status_Type'][0]["value"] #print Status_Type if Status_Type is not None: self.__module_action(entities) return if intent == 'greeting': self.__text_action(self.nlg.greet()) elif intent == 'snow white': self.__text_action(self.nlg.snow_white()) elif intent == 'weather': self.__weather_action(entities) elif intent == 'news': self.__news_action() elif intent == 'maps': self.__maps_action(entities) elif intent == 'youtube': self.__youtube_action(entities) #if self.vision.recognize_face(): # print "Found face" elif intent == 'zomato': self.__zomato_action(entities) elif intent == 'uber': self.__uber_action(entities) elif intent == 'holidays': self.__holidays_action() elif intent == 'appearance': self.__appearance_action() elif intent == 'user status': self.__user_status_action(entities) elif intent == 'user name': self.__user_name_action() elif intent == 'personal status': self.__personal_status_action() elif intent == 'joke': self.__joke_action() elif intent == 'insult': self.__insult_action() return elif intent == 'appreciation': self.__appreciation_action() return elif intent is None: self.__hound_action(speech) return else: self.__text_action("I'm sorry, I don't know about that yet.") return #time.sleep(20) except Exception as e: print "Failed wit!" print(e) traceback.print_exc() self.__text_action("I'm sorry, I couldn't understand what you meant by that") return self.decide_action() def __joke_action(self): joke = self.nlg.joke() if joke is not None: self.__text_action(joke) else: self.__text_action("I couldn't find any jokes") def __user_status_action(self, nlu_entities=None): attribute = None if (nlu_entities is not None) and ("Status_Type" in nlu_entities): attribute = nlu_entities['Status_Type'][0]['value'] self.__text_action(self.nlg.user_status(attribute=attribute)) def __user_name_action(self): if self.nlg.user_name is None: self.__text_action("I don't know your name. You can configure it in bot.py") self.__text_action(self.nlg.user_name) def __appearance_action(self): requests.get("http://localhost:8080/face") def __appreciation_action(self): self.__text_action(self.nlg.appreciation()) def __acknowledge_action(self): self.__text_action(self.nlg.acknowledge()) def __insult_action(self): self.__text_action(self.nlg.insult()) def __personal_status_action(self): self.__text_action(self.nlg.personal_status()) def __text_action(self, text=None): if text is not None: requests.get("http://localhost:8080/statement?text=%s" % text) self.speech.synthesize_text(text) def __news_action(self): headlines = self.knowledge.get_news() if headlines: requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines})) self.speech.synthesize_text(self.nlg.news("past")) interest = self.nlg.article_interest(headlines) if interest is not None: self.speech.synthesize_text(interest) else: self.__text_action("I had some trouble finding news for you") def __weather_action(self, nlu_entities=None): current_dtime = datetime.datetime.now() skip_weather = False # used if we decide that current weather is not important weather_obj = self.knowledge.find_weather() temperature = weather_obj['temperature'] icon = weather_obj['icon'] wind_speed = weather_obj['windSpeed'] weather_speech = self.nlg.weather(temperature, current_dtime, "present") forecast_speech = None if nlu_entities is not None: if 'datetime' in nlu_entities: if 'grain' in nlu_entities['datetime'][0] and nlu_entities['datetime'][0]['grain'] == 'day': dtime_str = nlu_entities['datetime'][0]['value'] # 2016-09-26T00:00:00.000-07:00 dtime = dateutil.parser.parse(dtime_str) if current_dtime.date() == dtime.date(): # hourly weather forecast_obj = {'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast']} forecast_speech = self.nlg.forecast(forecast_obj) elif current_dtime.date() < dtime.date(): # sometime in the future ... get the weekly forecast/ handle specific days forecast_obj = {'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast']} forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True if 'Weather_Type' in nlu_entities: weather_type = nlu_entities['Weather_Type'][0]['value'] print weather_type if weather_type == "current": forecast_obj = {'forecast_type': 'current', 'forecast': weather_obj['current_forecast']} forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'today': forecast_obj = {'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast']} forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'tomorrow' or weather_type == '3 day' or weather_type == '7 day': forecast_obj = {'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast']} forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True weather_data = {"temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour} requests.post("http://localhost:8080/weather", data=json.dumps(weather_data)) if not skip_weather: self.speech.synthesize_text(weather_speech) if forecast_speech is not None: self.speech.synthesize_text(forecast_speech) def __maps_action(self, nlu_entities=None): location = None map_type = None if nlu_entities is not None: if 'location' in nlu_entities: location = nlu_entities['location'][0]["value"] #location = nlu_entities['search_query'][0]["value"] else: location = nlu_entities['search_query'][0]["value"] if "Map_Type" in nlu_entities: map_type = nlu_entities['Map_Type'][0]["value"] if location is not None: maps_url = self.knowledge.get_map_url(location, map_type) maps_action = "Sure. Here's a map of %s." % location body = {'url': maps_url} requests.post("http://localhost:8080/image", data=json.dumps(body)) self.speech.synthesize_text(maps_action) else: self.__text_action("I'm sorry, I couldn't understand what location you wanted.") def __youtube_action(self, nlu_entities=None): video = None DEVELOPER_KEY = "" #add developer key YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" if nlu_entities is not None: if 'search_query' in nlu_entities: video = nlu_entities['search_query'][0]["value"] if video is not None: youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q=video, part="id,snippet", ).execute() for search_result in search_response.get("items"): if search_result["id"]["kind"] == "youtube#video": id= search_result["id"]["videoId"] ch_title = search_result["snippet"]["channelTitle"] title = search_result["snippet"]["title"] break; thumb = search_result["snippet"]["thumbnails"]["medium"] for search_result in search_response.get("items"): thumb = search_result["snippet"]["thumbnails"]["medium"] break; print id #print thumb videou = "https://www.youtube.com/embed/" + id + "?autoplay=1&controls=1" #print videou youtube_action = "Sure. Here's a video of %s." % video body = {'url': videou, 'channel': ch_title, 'thumb':thumb, 'titles':title} requests.post("http://localhost:8080/video", data=json.dumps(body)) self.speech.synthesize_text(youtube_action) time.sleep(8) else: self.__text_action("I'm sorry, I couldn't understand what video you wanted.") def __zomato_action(self, nlu_entities=None): if nlu_entities is not None: if 'location' in nlu_entities: entities=nlu_entities['location'][0]["value"] print entities response= requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=' %entities) #inset google maps key here resp_json_payload = response.json() lat=(resp_json_payload['results'][0]['geometry']['location']['lat']) lng=(resp_json_payload['results'][0]['geometry']['location']['lng']) print lat print lng locationUrlFromLatLong = "https://developers.zomato.com/api/v2.1/geocode?lat=%s&lon=%s" %(lat,lng) header = {"Accept": "application/json", "user_key": ""} #add user key response = requests.get(locationUrlFromLatLong, headers=header) restaurants=(response.json().get("nearby_restaurants")) name = [] rating = [] avgcost = [] for restaurant in restaurants: name.append(restaurant['restaurant']['name']) avgcost.append(restaurant['restaurant']['average_cost_for_two']) rating.append(restaurant['restaurant']['user_rating']['aggregate_rating']) #print name zomato_data = {"cabtype": name, 'maxfare': avgcost, "minfare": rating} uber_action = "Sure. Here are some results" requests.post("http://localhost:8080/zomato", data=json.dumps(zomato_data)) #self.speech.synthesize_text(uber_action) self.speech.synthesize_text(uber_action) else: self.__text_action("I'm sorry, I couldn't understand what restaurant you wanted.") def __uber_action(self, nlu_entities=None): uber = None if nlu_entities is not None: if 'location' in nlu_entities: entities3=nlu_entities['search_query'][0]["value"] entities1=nlu_entities['location'][0]["value"] entities2=nlu_entities['location'][1]["value"] print entities3 print entities1 print entities2 if entities1 and entities2 is not None: response= requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=' %entities1) #add key resp_json_payload = response.json() lat1=(resp_json_payload['results'][0]['geometry']['location']['lat']) lng1=(resp_json_payload['results'][0]['geometry']['location']['lng']) response = requests.get('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=' %entities2) #add key resp_json_payload = response.json() lat2=(resp_json_payload['results'][0]['geometry']['location']['lat']) lng2=(resp_json_payload['results'][0]['geometry']['location']['lng']) oauth2credential = OAuth2Credential( client_id='', #add client id access_token='', #get access token expires_in_seconds= '2592000', scopes='all_trips delivery history history_lite places profile request request_receipt ride_widgets', grant_type='authorization_code', redirect_url='', #add redirect_url client_secret='', #add client secret refresh_token='', # add refresh token ) session = Session(oauth2credential=oauth2credential) client = UberRidesClient(session, sandbox_mode=True) print (client) response = client.get_products(lat1, lng1) credentials = session.oauth2credential #print (response) response = client.get_user_profile() profile = response.json # response_uber = client.get_price_estimates( # start_latitude=lat1, # start_longitude=lng1, # end_latitude=lat2, # end_longitude=lng2, # seat_count=1 # ) # estimate = response_uber.json.get('prices') # print estimate[0]['high_estimate'] # print estimate[0]['low_estimate'] # print estimate[0]['localized_display_name'] # print estimate[0]['currency_code'] # currency = estimate[0]['currency_code'] # hi_est = str(estimate[0]['high_estimate']) + str(estimate[0]['currency_code']) # low_est = str(estimate[0]['low_estimate']) + str(estimate[0]['currency_code']) # type_cab = estimate[0]['localized_display_name'] # print estimate[0] response = client.get_products(lat1, lng1) products = response.json.get('products') #print(products) if entities3 == 'Uber go': for i in range(1,5): if products[i]['display_name']=='uberGO': product_id = products[i].get('product_id') type_cab = products[i].get('display_name') elif entities3 == 'Uber pool': for i in range(1,5): if products[i]['display_name']=='POOL': product_id = products[i].get('product_id') type_cab = products[i].get('display_name') else: product_id = products[0].get('product_id') type_cab = products[0].get('display_name') estimate = client.estimate_ride( product_id=product_id, start_latitude=lat1, start_longitude=lng1, end_latitude=lat2, end_longitude=lng2, seat_count=1 ) fare = estimate.json.get('fare') bas = fare['display'] client.cancel_current_ride() response = client.request_ride( product_id=product_id, start_latitude=lat1, start_longitude=lng1, end_latitude=lat2, end_longitude=lng2, seat_count=1, fare_id=fare['fare_id'] ) request = response.json print request request_id = request.get('request_id') url = 'https://sandbox-api.uber.com/v1.2/sandbox/requests/' + request_id ur = 'https://sandbox-api.uber.com/v1.2/requests/' + request_id + '/map' token = "" #insert token data = { "status": "accepted" } headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json"} #Call REST API respons = requests.put(url, data=json.dumps(data), headers=headers) respon = requests.get(ur, headers=headers) response = client.get_ride_details(request_id) ride = response.json print ride status = ride.get('status') dri_name = ride.get('driver').get('name') dri_pic = ride.get('driver').get('picture_url') eta = ride.get('destination').get('eta') car_pix = ride.get('vehicle').get('picture_url') # product_name1 = products[3]['display_name'] #GO # product_nam2 = products[2]['display_name'] #POOL uber_action = "Sure. Booking your uber from %s to %s. Your cab type is %s and estimated time of arrival is %s and fare will be approx %s" % (entities1, entities2, type_cab, eta, bas) cab_data = {"cabtype": type_cab, 'maxfare': bas, "minfare": eta, 'to': entities2, 'from': entities1, 'status':status, 'driver': dri_name, 'pic': dri_pic, 'car': car_pix, 'map':ur} #print cab_data requests.post("http://localhost:8080/cab", data=json.dumps(cab_data)) self.speech.synthesize_text(uber_action) else: self.__text_action("I'm sorry, I don't think that their is any cab available between these two locations.") def __holidays_action(self): holidays = self.knowledge.get_holidays() next_holiday = self.__find_next_holiday(holidays) requests.post("http://localhost:8080/holidays", json.dumps({"holiday": next_holiday})) self.speech.synthesize_text(self.nlg.holiday(next_holiday['localName'])) def __find_next_holiday(self, holidays): today = datetime.datetime.now() for holiday in holidays: date = holiday['date'] if (date['day'] > today.day) and (date['month'] > today.month): return holiday # next year return holidays[0] def __module_action(self, nlu_entities=None): if nlu_entities is not None: Status_Type = nlu_entities['Status_Type'][0]["value"] if 'search_query' in nlu_entities: video = nlu_entities['search_query'][0]["value"] print video body = {'url': video, 'sta': Status_Type} print body requests.post("http://localhost:8080/module", data=json.dumps(body)) print 'Raghav' self.speech.synthesize_text("sure") def __hound_action(self, speech=None): if speech is not None: clientId = "" #get client id from houndify clientKey = "" #insert client key userId = "test_user" requestInfo = { "Latitude": 18.5679, "Longitude": 73.9143 } client = houndify.TextHoundClient(clientId, clientKey, userId, requestInfo) response = client.query(speech) #conversationState = response["AllResults"][0]["ConversationState"] #client.setConversationState(conversationState) command = response["AllResults"][0]["WrittenResponseLong"] spoken = response["AllResults"][0]["SpokenResponseLong"] name = [] rating = [] avgcost = [] url = [] print if "Template" in response["AllResults"][0]["ViewType"]: if len(response["AllResults"][0]["TemplateData"]["Items"]) > 8: for item in range(0,10): if 'Title' in response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]: name.append(response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]["Title"]) if 'Subtitle' in response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]: rating.append(response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]["Subtitle"]) if 'BodyText' in response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]: avgcost.append(response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]["BodyText"]) if 'URL' in response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]["Image"]: url.append(response["AllResults"][0]["TemplateData"]["Items"][item]["TemplateData"]["Image"][ "URL"]) hound_data = {"title": name, 'subtitle': rating, "details": avgcost, "url": url, "command": command} requests.post("http://localhost:8080/other", data=json.dumps(hound_data)) else: requests.post("http://localhost:8080/other", data=json.dumps(command)) #requests.get("http://localhost:8080/statement?text=%s" % text) #self.speech.synthesize_text(text) else: requests.post("http://localhost:8080/other", data=json.dumps(command)) self.speech.synthesize_text(spoken) self.decide_action()
class Bot(object): def __init__(self): self.nlg = NLG(user_name=my_name) self.speech = Speech(launch_phrase=launch_phrase, debugger_enabled=debugger_enabled) self.knowledge = Knowledge(weather_api_token) self.vision = Vision(camera=camera) def start(self): """ Main loop. Waits for the launch phrase, then decides an action. :return: """ while True: requests.get("http://localhost:8080/clear") if self.vision.recognize_face(): print "Found face" if use_launch_phrase: recognizer, audio = self.speech.listen_for_audio() if self.speech.is_call_to_action(recognizer, audio): self.__acknowledge_action() self.decide_action() else: self.decide_action() def decide_action(self): """ Recursively decides an action based on the intent. :return: """ recognizer, audio = self.speech.listen_for_audio() # received audio data, now we'll recognize it using Google Speech Recognition speech = self.speech.google_speech_recognition(recognizer, audio) if speech is not None: try: r = requests.get('https://api.wit.ai/message?v=20160918&q=%s' % speech, headers={"Authorization": wit_ai_token}) print r.text json_resp = json.loads(r.text) entities = None intent = None if 'entities' in json_resp and 'Intent' in json_resp[ 'entities']: entities = json_resp['entities'] intent = json_resp['entities']['Intent'][0]["value"] print intent if intent == 'greeting': self.__text_action(self.nlg.greet()) elif intent == 'snow white': self.__text_action(self.nlg.snow_white()) elif intent == 'weather': self.__weather_action(entities) elif intent == 'news': self.__news_action() elif intent == 'maps': self.__maps_action(entities) elif intent == 'holidays': self.__holidays_action() elif intent == 'appearance': self.__appearance_action() elif intent == 'user status': self.__user_status_action(entities) elif intent == 'user name': self.__user_name_action() elif intent == 'personal status': self.__personal_status_action() elif intent == 'joke': self.__joke_action() elif intent == 'insult': self.__insult_action() return elif intent == 'appreciation': self.__appreciation_action() return else: # No recognized intent self.__text_action( "I'm sorry, I don't know about that yet.") return except Exception as e: print "Failed wit!" print(e) traceback.print_exc() self.__text_action( "I'm sorry, I couldn't understand what you meant by that") return self.decide_action() def __joke_action(self): joke = self.nlg.joke() if joke is not None: self.__text_action(joke) else: self.__text_action("I couldn't find any jokes") def __user_status_action(self, nlu_entities=None): attribute = None if (nlu_entities is not None) and ("Status_Type" in nlu_entities): attribute = nlu_entities['Status_Type'][0]['value'] self.__text_action(self.nlg.user_status(attribute=attribute)) def __user_name_action(self): if self.nlg.user_name is None: self.__text_action( "I don't know your name. You can configure it in bot.py") self.__text_action(self.nlg.user_name) def __appearance_action(self): requests.get("http://localhost:8080/face") def __appreciation_action(self): self.__text_action(self.nlg.appreciation()) def __acknowledge_action(self): self.__text_action(self.nlg.acknowledge()) def __insult_action(self): self.__text_action(self.nlg.insult()) def __personal_status_action(self): self.__text_action(self.nlg.personal_status()) def __text_action(self, text=None): if text is not None: requests.get("http://localhost:8080/statement?text=%s" % text) self.speech.synthesize_text(text) def __news_action(self): headlines = self.knowledge.get_news() if headlines: requests.post("http://localhost:8080/news", data=json.dumps({"articles": headlines})) self.speech.synthesize_text(self.nlg.news("past")) interest = self.nlg.article_interest(headlines) if interest is not None: self.speech.synthesize_text(interest) else: self.__text_action("I had some trouble finding news for you") def __weather_action(self, nlu_entities=None): current_dtime = datetime.datetime.now() skip_weather = False # used if we decide that current weather is not important weather_obj = self.knowledge.find_weather() temperature = weather_obj['temperature'] icon = weather_obj['icon'] wind_speed = weather_obj['windSpeed'] weather_speech = self.nlg.weather(temperature, current_dtime, "present") forecast_speech = None if nlu_entities is not None: if 'datetime' in nlu_entities: if 'grain' in nlu_entities['datetime'][0] and nlu_entities[ 'datetime'][0]['grain'] == 'day': dtime_str = nlu_entities['datetime'][0][ 'value'] # 2016-09-26T00:00:00.000-07:00 dtime = dateutil.parser.parse(dtime_str) if current_dtime.date() == dtime.date(): # hourly weather forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif current_dtime.date() < dtime.date( ): # sometime in the future ... get the weekly forecast/ handle specific days forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True if 'Weather_Type' in nlu_entities: weather_type = nlu_entities['Weather_Type'][0]['value'] print weather_type if weather_type == "current": forecast_obj = { 'forecast_type': 'current', 'forecast': weather_obj['current_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'today': forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'tomorrow' or weather_type == '3 day' or weather_type == '7 day': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True weather_data = { "temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour } requests.post("http://localhost:8080/weather", data=json.dumps(weather_data)) if not skip_weather: self.speech.synthesize_text(weather_speech) if forecast_speech is not None: self.speech.synthesize_text(forecast_speech) def __maps_action(self, nlu_entities=None): location = None map_type = None if nlu_entities is not None: if 'location' in nlu_entities: location = nlu_entities['location'][0]["value"] if "Map_Type" in nlu_entities: map_type = nlu_entities['Map_Type'][0]["value"] if location is not None: maps_url = self.knowledge.get_map_url(location, map_type) maps_action = "Sure. Here's a map of %s." % location body = {'url': maps_url} requests.post("http://localhost:8080/image", data=json.dumps(body)) self.speech.synthesize_text(maps_action) else: self.__text_action( "I'm sorry, I couldn't understand what location you wanted.") def __holidays_action(self): holidays = self.knowledge.get_holidays() next_holiday = self.__find_next_holiday(holidays) requests.post("http://localhost:8080/holidays", json.dumps({"holiday": next_holiday})) self.speech.synthesize_text(self.nlg.holiday( next_holiday['localName'])) def __find_next_holiday(self, holidays): today = datetime.datetime.now() for holiday in holidays: date = holiday['date'] if (date['day'] > today.day) and (date['month'] > today.month): return holiday # next year return holidays[0]
class Alfred(): def __init__(self): self.nlg = NLG(user_name=NAME) self.remote_data = RemoteData(weather_api_token=DARKSKY_TOKEN) self.audio_handler = AudioHandler(debug=True) # self.calendar = Calendar() self.calendar = None self.session_id = uuid.uuid1() self.context = {} self.prev_ai_message = "" self.ai_message = " - " self._active_alarms = [] self.active = False # When the user has activated Alfred def close(self): self.nlg.close() sys.exit(0) def get_ai_message(self): message = self.ai_message return message def get_alarms(self): alarms = self._active_alarms return alarms def _active_timeout(self, signum, frame): print "Timeout..." del self.context['active'] return self.context def _first_entity_value(self, entities, entity): if entity not in entities: return None if 'value' in entities[entity][0]: val = entities[entity][0]['value'] return val['value'] if isinstance(val, dict) else val elif 'values' in entities[entity][0]: return { "from": entities[entity][0]["from"]["value"], "to": entities[entity][0]["to"]["value"] } else: return None def _confident(self, entities): if not entities: return False print entities if not "Intent" in entities: # Trust that entities confidence is high return True intent = entities['Intent'][0]['value'] confidence = float(entities['Intent'][0]['confidence']) print "Confidence (%s): %s" % (intent, confidence) return (confidence > 0.8) def _if_wake_alfred(self, message): if "Alfred" in message: self.active = True def _converse(self, message): # print "" # print "Message:", message # print "Context:", self.context # print "Session-id:", self.session_id # print "" new_context = self.client.run_actions(self.session_id, message, self.context) self.context = new_context print('The session state is now: ' + str(self.context)) # -------- # ACTIONS # -------- def _send(self, request, response): message = response['text'] if message != self.prev_ai_message: self.ai_message = message print "Alfred:", message self.prev_ai_message = message def _get_forecast(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context weather_request = "currently" loc = self._first_entity_value(entities, 'location') time = self._first_entity_value(entities, 'datetime') if not time: time = datetime.datetime.now() else: w_type = entities[u'datetime'][0][u'grain'] if w_type == "hour": weather_request = "hourly" elif w_type == "second": weather_request = "minutely" elif w_type == "day": weather_request = "daily" time_query = str(time).split('.')[0].replace(' ', 'T') # Remove timezone encoded_date_obj = datetime.datetime.strptime( time_query.split('.')[0], '%Y-%m-%dT%H:%M:%S') # search_phrase = self.nlg.searching() # self.ai_message = search_phrase weather_obj = self.remote_data.find_weather(time_query, loc, weather_request) context = {} context['forecast'] = self.nlg.weather(weather_obj, encoded_date_obj) return context def _get_score(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context team = self._first_entity_value(entities, 'team') date = self._first_entity_value(entities, 'datetime') if date != None: if isinstance(date, dict): date = date["from"] date = str(date).split('.')[0].replace(' ', 'T') date = datetime.datetime.strptime( date.split('.')[0], '%Y-%m-%dT%H:%M:%S') # search_phrase = self.nlg.searching() # self.ai_message = search_phrase score_obj = self.remote_data.get_score(date, team) context = {} if not score_obj: self.ai_message = "Sorry. I could not find any scores matching your request." else: context['score'] = self.nlg.score(score_obj) return context def _get_news(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context # team = self._first_entity_value(entities, 'team') # time_query = str(time).split('.')[0].replace(' ', 'T') # Remove timezone # encoded_date_obj = datetime.datetime.strptime(time_query.split('.')[0], '%Y-%m-%dT%H:%M:%S') news_obj = self.remote_data.get_news() context = {} self.nlg.news("past") interest = self.nlg.article_interest(news_obj) if interest is not None: context['news'] = interest return context def _greeting(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context by_name = False ai = self._first_entity_value(entities, 'ai_entity') if ai: by_name = True context = {} context['greeting'] = self.nlg.greet(by_name) return context def _joke(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context context = {} # context['joke'] = self.nlg.joke() context['joke'] = self.remote_data.get_random_joke() return context def _status(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context context = {} context['status'] = self.nlg.personal_status() return context def _appreciation_response(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context context = {} context['appreciation_response'] = self.nlg.appreciation() return context def _set_alarm_clock(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context datetime = self._first_entity_value(entities, 'datetime') context = {} if not datetime: context['alarm_time_missing'] = self.nlg.alarm_info(None, None) print "Alarm: %s" % (context) return context date = datetime.split("T")[0] time = ":".join(datetime.split("T")[1].split(".")[0].split(":")[:-1]) self._active_alarms.append({"date": date, "time": time}) print "date: %s, time: %s" % (date, time) context['alarm_confirmation'] = self.nlg.alarm_info(date, time) print "Alarm: %s" % (context) return context def _manage_lights(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context off_on = self._first_entity_value(entities, 'off_on') room = self._first_entity_value(entities, 'location') lamp = self._first_entity_value(entities, 'lamp') context = {} if room: if room in devices_in_room: context['lights_confirmation'] = self.nlg.lights_confirmation( "room", room, off_on) for key in devices_in_room[room]: if off_on == "on": devices[key].turn_on() else: devices[key].turn_off() else: context['lights_confirmation'] = self.nlg.lights_confirmation( "room", None, None) elif lamp: if lamp in device_translation: key = device_translation[lamp] context['lights_confirmation'] = self.nlg.lights_confirmation( "lamp", lamp, off_on) if off_on == "on": devices[key].turn_on() else: devices[key].turn_off() else: context['lights_confirmation'] = self.nlg.lights_confirmation( "lamp", None, None) else: if not off_on: off_on = "on" context['lights_confirmation'] = self.nlg.lights_confirmation( "all", None, off_on) for key in devices.keys(): if off_on == "on": devices[key].turn_on() else: devices[key].turn_off() return context def _acknowledgement(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context context = {} context['acknowledge_response'] = self.nlg.acknowledge() return context def _get_next_event(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context event = self.calendar.get_next_event() context = {} context['next_event'] = self.nlg.next_event(event) return context def _get_events(self, request): entities = request['entities'] context = {} if not self._confident(entities): self.ai_message = self.nlg.clueless() return context datetime = self._first_entity_value(entities, 'datetime') if not datetime: context['events_missing'] = "What date do you want me to check?" return context date = datetime.split("T")[0] events = self.calendar.get_events(date) context['events'] = self.nlg.events(events) return context def _identification(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context context = {} context['identification'] = self.nlg.identification() return context def _sleep(self, request): entities = request['entities'] context = request['context'] if not self._confident(entities): self.ai_message = self.nlg.clueless() return context self.active = False context = {} context["bye"] = self.nlg.goodbye() return context
class Bot(object): def __init__(self): self.nlg = NLG(user_name=my_name) self.speech = Speech(launch_phrase=launch_phrase, debugger_enabled=debugger_enabled) self.knowledge = Knowledge(weather_api_token) self.vision = Vision(camera=camera) def start(self): """ Main loop. Waits for the launch phrase, then decides an action. :return: """ # subprocess.Popen(["aplay", "/home/pi/AI-Smart-Mirror/sample-audio-files/Startup.wav"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # time.sleep(1) self.__setup() while True: requests.get("http://localhost:8080/clear") if self.vision.recognize_face(): # if True: print('Found face') if use_launch_phrase: recognizer, audio = self.speech.listen_for_audio() if self.speech.is_call_to_action(recognizer, audio): GPIO.output(LEDPIN, GPIO.HIGH) # set volumn_down 10 if self.__ismpvplaying(): #check mpv have play or not if os.path.isfile("/home/pi/.mediavolume.json"): mpvsetvol = os.system("echo '" + json.dumps({ "command": ["set_property", "volume", "10"] }) + "' | socat - /tmp/mpvsocket") else: mpvgetvol = subprocess.Popen( [("echo '" + json.dumps({ "command": ["get_property", "volume"] }) + "' | socat - /tmp/mpvsocket")], shell=True, stdout=subprocess.PIPE) output = mpvgetvol.communicate()[0] for currntvol in re.findall( r"[-+]?\d*\.\d+|\d+", str(output)): with open('/home/pi/.mediavolume.json', 'w') as vol: json.dump(currntvol, vol) mpvsetvol = os.system("echo '" + json.dumps({ "command": ["set_property", "volume", "10"] }) + "' | socat - /tmp/mpvsocket") self.__acknowledge_action() self.decide_action() GPIO.output(LEDPIN, GPIO.LOW) #set back volume if self.__ismpvplaying(): #check mpv have play or not if os.path.isfile("/home/pi/.mediavolume.json"): with open('/home/pi/.mediavolume.json', 'r') as vol: oldvollevel = json.load(vol) print(oldvollevel) mpvsetvol = os.system("echo '" + json.dumps({ "command": [ "set_property", "volume", str(oldvollevel) ] }) + "' | socat - /tmp/mpvsocket") else: self.decide_action() def __setup(self): GPIO.setwarnings(False) #set the gpio modes to BCM numbering GPIO.setmode(GPIO.BCM) #set LEDPIN's mode to output,and initial level to LOW(0V) GPIO.setup(LEDPIN, GPIO.OUT, initial=GPIO.LOW) def decide_action(self): """ Recursively decides an action based on the intent. :return: """ recognizer, audio = self.speech.listen_for_audio() # received audio data, now we'll recognize it using Google Speech Recognition speech = self.speech.google_speech_recognition(recognizer, audio) if speech is not None: try: r = requests.get('https://api.wit.ai/message?v=20180428&q=%s' % speech, headers={"Authorization": wit_ai_token}) print(r.text) json_resp = json.loads(r.text) entities = None intent = None if 'entities' in json_resp and 'Intent' in json_resp[ 'entities']: entities = json_resp['entities'] intent = json_resp['entities']['Intent'][0]["value"] if intent == 'greeting': self.__text_action(self.nlg.greet()) elif intent == 'snow white': self.__text_action(self.nlg.snow_white()) elif intent == 'weather': self.__weather_action(entities) elif intent == 'news': self.__news_action() elif intent == 'maps': self.__maps_action(entities) elif intent == 'holidays': self.__holidays_action() elif intent == 'appearance': self.__appearance_action() elif intent == 'user status': self.__user_status_action(entities) elif intent == 'user name': self.__user_name_action() elif intent == 'personal status': self.__personal_status_action() elif intent == 'joke': self.__joke_action() elif intent == 'insult': self.__insult_action() return elif intent == 'appreciation': self.__appreciation_action() return elif intent == 'music': self.__playmusic(entities) return elif intent == 'light': self.__light_action(entities) return else: # No recognized intent self.__text_action( "I'm sorry, I don't know about that yet.") return except Exception as e: print("Failed wit!") print(e) traceback.print_exc() self.__text_action( "I'm sorry, I couldn't understand what you meant by that") return self.decide_action() def __light_action(self, nlu_entities=None): action = None position = None light = Light(23) if nlu_entities is not None: if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'] if 'position' in nlu_entities: position = nlu_entities['position'][0]['value'] if action == 'on': light.turn_on() elif action == 'off': light.turn_off() self.__text_action("Ok") else: self.__text_action( "I'm sorry, I couldn't understand what you meant by that") def __joke_action(self): joke = self.nlg.joke() if joke is not None: self.__text_action(joke) else: self.__text_action("I couldn't find any jokes") def __user_status_action(self, nlu_entities=None): attribute = None if (nlu_entities is not None) and ("Status_Type" in nlu_entities): attribute = nlu_entities['Status_Type'][0]['value'] self.__text_action(self.nlg.user_status(attribute=attribute)) def __user_name_action(self): if self.nlg.user_name is None: self.__text_action( "I don't know your name. You can configure it in bot.py") self.__text_action(self.nlg.user_name) def __appearance_action(self): requests.get("http://localhost:8080/face") def __appreciation_action(self): self.__text_action(self.nlg.appreciation()) def __acknowledge_action(self): self.__text_action(self.nlg.acknowledge()) def __insult_action(self): self.__text_action(self.nlg.insult()) def __personal_status_action(self): self.__text_action(self.nlg.personal_status()) def __text_action(self, text=None): if text is not None: requests.get("http://localhost:8080/statement?text=%s" % text) # self.speech.synthesize_text(text) say(text, language) def __news_action(self): headlines = self.knowledge.get_news() if headlines: requests.post("http://localhost:8080/news", data=json.dumps({"articles": headlines})) self.speech.synthesize_text(self.nlg.news("past")) interest = self.nlg.article_interest(headlines) if interest is not None: self.speech.synthesize_text(interest) else: self.__text_action("I had some trouble finding news for you") def __weather_action(self, nlu_entities=None): current_dtime = datetime.datetime.now() skip_weather = False # used if we decide that current weather is not important weather_obj = self.knowledge.find_weather() temperature = weather_obj['temperature'] icon = weather_obj['icon'] wind_speed = weather_obj['windSpeed'] weather_speech = self.nlg.weather(temperature, current_dtime, "present") forecast_speech = None if nlu_entities is not None: if 'datetime' in nlu_entities: if 'grain' in nlu_entities['datetime'][0] and nlu_entities[ 'datetime'][0]['grain'] == 'day': dtime_str = nlu_entities['datetime'][0][ 'value'] # 2016-09-26T00:00:00.000-07:00 dtime = dateutil.parser.parse(dtime_str) if current_dtime.date() == dtime.date(): # hourly weather forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif current_dtime.date() < dtime.date( ): # sometime in the future ... get the weekly forecast/ handle specific days forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True if 'Weather_Type' in nlu_entities: weather_type = nlu_entities['Weather_Type'][0]['value'] print(weather_type) if weather_type == "current": forecast_obj = { 'forecast_type': 'current', 'forecast': weather_obj['current_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'today': forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['daily_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'tomorrow' or weather_type == '3 day' or weather_type == '7 day': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'] } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True weather_data = { "temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour } requests.post("http://localhost:8080/weather", data=json.dumps(weather_data)) if not skip_weather: # self.speech.synthesize_text(weather_speech) say(weather_speech) if forecast_speech is not None: # self.speech.synthesize_text(forecast_speech) say(forecast_speech) def __maps_action(self, nlu_entities=None): location = None map_type = None origin = None destination = None if nlu_entities is not None: if 'location' in nlu_entities: location = nlu_entities['location'][0]["value"] if "Map_Type" in nlu_entities: map_type = nlu_entities['Map_Type'][0]["value"] if 'origin' in nlu_entities: origin = nlu_entities['origin'][0]["value"] if 'destination' in nlu_entities: destination = nlu_entities['destination'][0]["value"] if origin is not None: if destination is not None: print(origin, destination) if location is not None: maps_url = self.knowledge.get_map_url(location, map_type) maps_action = "Sure. Here's a map of %s." % location body = {'url': maps_url} requests.post("http://localhost:8080/image", data=json.dumps(body)) self.speech.synthesize_text(maps_action) else: self.__text_action( "I'm sorry, I couldn't understand what location you wanted.") def __playmusic(self, nlu_entities=None): chanel = None action = None region_code = 'VN' location = None local_search_query = None if nlu_entities is not None: if 'chanel' in nlu_entities: chanel = nlu_entities['chanel'][0]['value'] if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'] if 'local_search_query' in nlu_entities: local_search_query = nlu_entities['local_search_query'][0][ 'value'] if 'location' in nlu_entities: location = nlu_entities['location'][0]['value'] region_code = self.knowledge.get_country_code(location) if chanel is not None: if 'youtube' == chanel: if 'play' == action: stop() if local_search_query is not None: YouTube_Autoplay(local_search_query, region_code) else: YouTube_Autoplay('Le quyen', region_code) elif 'stop' == action: stop() self.__text_action("OK, stoped it") else: self.__text_action("what are you want to do %s" % my_name) def __holidays_action(self): holidays = self.knowledge.get_holidays() next_holiday = self.__find_next_holiday(holidays) requests.post("http://localhost:8080/holidays", json.dumps({"holiday": next_holiday})) self.speech.synthesize_text(self.nlg.holiday( next_holiday['localName'])) def __find_next_holiday(self, holidays): today = datetime.datetime.now() for holiday in holidays: date = holiday['date'] if (date['day'] > today.day) and (date['month'] > today.month): return holiday # next year return holidays[0] #Function to check if mpv is playing def __ismpvplaying(self): for pid in psutil.pids(): p = psutil.Process(pid) if 'mpv' in p.name(): mpvactive = True break else: mpvactive = False return mpvactive
class Jarvis(object): my_name = "Scott" launch_phrase = "jarvis" use_launch_phrase = True debugger_enabled = True location = "hoboken" unknown_fieldnames = ['Command', 'Action'] awake = False def __init__(self): self.nlg = NLG(user_name=self.my_name) self.speech = Speech(launch_phrase=self.launch_phrase, debugger_enabled=self.debugger_enabled) self.actions = Actions(self.location) if os.path.isfile('unknown_commands.csv') == False: with open('unknown_commands.csv', 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=self.unknown_fieldnames) writer.writeheader() def start(self): """ Main loop. Waits for the launch phrase, then decides an action. """ while True: requests.get('http://localhost:8080/clear') if self.use_launch_phrase: recognizer, audio = self.speech.listen_for_audio(self.awake) text = self.speech.google_speech_recognition(recognizer, audio) if text is not None and self.launch_phrase in text.lower(): self.awake = True #self.__acknowledge_action() self.decide_action(text) else: if self.awake == True: self.decide_action(text) else: self.speech.debugger_microphone(enable=False) def decide_action(self, text): """ Reursively decides an action based on the intent. """ acknowledgement = False if text is not None and self.awake == True: text = text.lower() if "hide" in text: if "time" in text: self.__hide_action("time") if "weather" in text: self.__hide_action("weather") if "calendar" in text: self.__hide_action("calendar") if "commute" in text: self.__hide_action("commute") if "all" in text: self.__hide_action("all") return if "show" in text: if "time" in text: self.__show_action("time") if "weather" in text: self.__show_action("weather") if "calendar" in text: self.__show_action("calendar") if "commute" in text: self.__show_action("commute") if "all" in text: self.__show_action("all") return if "hello" in text or "good morning" in text or "good afternoon" in text or "good evening" in text or "hey" in text or "hi" in text: if "jarvis" in text: self.__acknowledge_action() acknowledgement = True else: self.__greet_action() return if "how are you" in text or "how's it going" in text or "what's going on" in text: self.__personal_status() return if "weather" in text: if "today" in text: self.__weather_action("today") elif "tomorrow" in text: self.__weather_action("tomorrow") else: self.__weather_action("today") return if "time" in text: if "date" in text: self.__time_action(withDate=True) return else: self.__time_action(withDate=False) return if "date" in text and "time" not in text: self.__date_action() return if "play" in text and "music" in text: if "some" in text: self.__play_spotify_action() return if "goodbye" in text or "thank you" in text or "that's all" in text or "that is all" in text or "that's it" in text or "thanks" in text: self.__done_action() return if text == "": return if acknowledgement == True: return self.__unknown_command_action(text) def __text_action(self, text=None): if text is not None: requests.get("http://localhost:8080/statement?text=%s" % text) self.speech.synthesize_text(text) def __acknowledge_action(self): self.__text_action(self.nlg.acknowledge()) def __greet_action(self): self.__text_action(self.nlg.greet()) def __personal_status(self): self.__text_action(self.nlg.personal_status()) def __weather_action(self, tod): if tod is not None: weathertext = self.actions.getWeather(tod) if weathertext is not None: city = weathertext['city'].encode('ascii', 'ignore') date = weathertext['date'] condition = weathertext['condition'].encode('ascii', 'ignore') low = weathertext['low'].encode('ascii', 'ignore') high = weathertext['high'].encode('ascii', 'ignore') response = "The weather in " + city + " " + date + " is going to be " + condition + " with temperatures ranging from " + str( low) + " to " + str(high) + " degrees" self.__text_action(response) else: response = "Sorry, I couldn't gather weather text for some reason" self.__text_action(response) def __time_action(self, withDate=False): if withDate: time = self.actions.getTime() date = self.actions.getDate() response = "It is " + str(time) + " on " + str(date) self.__text_action(response) else: time = self.actions.getTime() response = "It is " + str(time) self.__text_action(response) def __date_action(self): date = self.actions.getDate() response = "It is " + str(date) self.__text_action(response) def __done_action(self): self.speech.debugger_microphone(enable=False) self.__text_action("Have a great day") self.awake = False def __unknown_command_action(self, text): self.__text_action("Command unknown. Save command?") commandToSave = text recognizer, audio = self.speech.listen_for_audio(self.awake) text = self.speech.google_speech_recognition(recognizer, audio) if text is not None: if "yes" in text: self.__text_action("Ok, what is its action?") recognizer, audio = self.speech.listen_for_audio(self.awake) actionToSave = self.speech.google_speech_recognition( recognizer, audio) if actionToSave is not None: with open('unknown_commands.csv', 'a') as csvfile: writer = csv.DictWriter( csvfile, fieldnames=self.unknown_fieldnames) writer.writerow({ 'Command': commandToSave, 'Action': actionToSave }) self.__text_action("Command recorded.") def __hide_action(self, widget): if (widget == "all"): requests.get("http://localhost:8080/hide?widget=%s" % "time") requests.get("http://localhost:8080/hide?widget=%s" % "weather") requests.get("http://localhost:8080/hide?widget=%s" % "calendar") requests.get("http://localhost:8080/hide?widget=%s" % "commute") else: requests.get("http://localhost:8080/hide?widget=%s" % widget) self.__text_action(self.nlg.confirmation()) def __show_action(self, widget): if (widget == "all"): requests.get("http://localhost:8080/show?widget=%s" % "time") requests.get("http://localhost:8080/show?widget=%s" % "weather") requests.get("http://localhost:8080/show?widget=%s" % "calendar") requests.get("http://localhost:8080/show?widget=%s" % "commute") else: requests.get("http://localhost:8080/show?widget=%s" % widget) self.__text_action(self.nlg.confirmation()) def __play_spotify_action(self): self.__text_action("Here you go") self.actions.playRandomSpotify() self.__done_action()
class Bot(object): def __init__(self): self.nlg = NLG(user_name=my_name) self.nlg_vn = NLG(user_name=my_name_vn, language='vi') self.speech = Speech(launch_phrase=launch_phrase, debugger_enabled=debugger_enabled) self.wit_client_vn = Wit(wit_ai_token_vn) self.wit_client_en = Wit(wit_ai_token) self.knowledge = Knowledge(weather_api_token, google_cloud_api_key=google_cloud_api_key) #self.vision = Vision(camera=camera) self.bot_vn = 'ty' subprocess.Popen([ "aplay", "/home/pi/AI-Smart-Mirror/sample-audio-files/Startup.wav" ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(4) def __setup(self): GPIO.setwarnings(False) # set the gpio modes to BCM numbering GPIO.setmode(GPIO.BCM) # set LEDPIN's mode to output,and initial level to LOW(0V) GPIO.setup(LEDPIN, GPIO.OUT, initial=GPIO.LOW) def start(self): """ Main loop. Waits for the launch phrase, then decides an action. :return: """ self.__setup() while True: # requests.get("http://localhost:8080/clear") if True: # if True: print('Found face') if use_launch_phrase: recognizer, audio = self.speech.listen_for_audio( ) # save file audio vn_bot = self.speech.is_call_to_action(recognizer, audio, language='vi') self.bot_vn = vn_bot en_bot = None if not (vn_bot in ['ty', 'be', 'xuka']): # vn_bot = self.speech_vn.is_call_to_action(recognizer, audio, language='vi') en_bot = self.speech.is_call_to_action( recognizer, audio) # check vn or en bot if en_bot is not None or vn_bot is not None: GPIO.output(LEDPIN, GPIO.HIGH) # light on # set volumn_down 10 if self.__ismpvplaying(): # check mpv have play or not if os.path.isfile("/home/pi/.mediavolume.json"): mpvsetvol = os.system("echo '" + json.dumps({ "command": ["set_property", "volume", "10"] }) + "' | socat - /tmp/mpvsocket") else: mpvgetvol = subprocess.Popen( [("echo '" + json.dumps({ "command": ["get_property", "volume"] }) + "' | socat - /tmp/mpvsocket")], shell=True, stdout=subprocess.PIPE) output = mpvgetvol.communicate()[0] for currntvol in re.findall( r"[-+]?\d*\.\d+|\d+", str(output)): with open('/home/pi/.mediavolume.json', 'w') as vol: json.dump(currntvol, vol) mpvsetvol = os.system("echo '" + json.dumps({ "command": ["set_property", "volume", "10"] }) + "' | socat - /tmp/mpvsocket") if vn_bot in ['ty', 'be', 'xuka']: self.__acknowledge_action(langague='vi') # self.decide_action(langague='vi') else: self.__acknowledge_action() # self.decide_action() GPIO.output(LEDPIN, GPIO.LOW) # set back volume if self.__ismpvplaying(): # check mpv have play or not if os.path.isfile("/home/pi/.mediavolume.json"): with open('/home/pi/.mediavolume.json', 'r') as vol: oldvollevel = json.load(vol) print(oldvollevel) mpvsetvol = os.system("echo '" + json.dumps({ "command": [ "set_property", "volume", str(oldvollevel) ] }) + "' | socat - /tmp/mpvsocket") else: self.decide_action(langague='vi') # Wait for all threads to complete def decide_action(self, langague=None, oldintent=None, pre_message=None): """ Recursively decides an action based on the intent. :return: """ speech_en = None speech_vn = None # received audio data, now we'll recognize it using Google Speech Recognition if langague is not None: while speech_vn is None: recognizer, audio = self.speech.listen_for_audio() speech_vn = self.speech.google_speech_recognition( recognizer, audio, langague) if oldintent is not None: speech_vn = '%s %s' % (oldintent, speech_vn) else: while speech_en is None: recognizer, audio = self.speech.listen_for_audio() speech_en = self.speech.google_speech_recognition( recognizer, audio) # Run EN Bot if speech_en is not None: try: # r = requests.get('https://api.wit.ai/message?v=20180428&q=%s' % speech_en, # headers={"Authorization": wit_ai_token}) response = self.wit_client_en.message(speech_en) entities = None intent = None if response is not None: entities = response['entities'] intent = entities['intent'][0]["value"] if intent == 'greeting': self.__text_action(self.nlg.greet()) elif intent == 'snow white': self.__text_action(self.nlg.snow_white()) elif intent == 'weather': self.__weather_action(entities) elif intent == 'news': self.__text_action('News function is not yet implemted') elif intent == 'maps': self.__maps_action(entities) elif intent == 'holidays': self.__holidays_action() # elif intent == 'appearance': # self.__appearance_action() elif intent == 'user status': self.__user_status_action(entities) elif intent == 'user name': self.__user_name_action() elif intent == 'personal status': self.__personal_status_action() elif intent == 'joke': self.__joke_action() elif intent == 'appreciation': self.__appreciation_action() return elif intent == 'music': self.__playmusic(entities) return elif intent == 'light': self.__light_action(entities) return else: # No recognized intent self.__text_action( "I'm sorry, I don't know about that yet.") return except Exception as e: print("Failed wit!") print(e) traceback.print_exc() self.__text_action( "I'm sorry, I couldn't understand what you meant by that") return self.decide_action() else: # Run VN Bot if speech_vn is not None: try: # r = requests.get('https://api.wit.ai/message?v=20180428&q=%s' % speech_vn, # headers={"Authorization": wit_ai_token_vn}) # resp = json.loads(r.text) response = self.wit_client_vn.message(speech_vn) entities = None intent = None print(response) if response is not None: entities = response['entities'] if 'intent' in entities: intent = entities['intent'][0]["value"] print(intent) if intent == 'lời chào': self.__text_action(self.nlg_vn.greet()) elif intent == 'thời tiết': self.__weather_action_vn(entities) elif intent == 'tin tức': self.__news_action(entities, langague, oldintent) if pre_message is not None: if speech_vn is not None and 'dịch' in speech_vn: self.__text_action(pre_message) return if oldintent is not None: self.__text_action('Kết thúc đọc tin tức') return elif intent == 'bản đồ': self.__maps_action(entities) elif intent == 'ngày nghỉ': self.__holidays_action(entities) elif intent == 'điều khiển': self.__device_action(entities) # elif intent == 'self': # self.__appearance_action() elif intent == 'alarm': self.__alarm(entities) return elif intent == 'user status': self.__user_status_action(entities) elif intent == 'user name': self.__user_name_action() elif intent == 'personal status': self.__personal_status_action() elif intent == 'joke': self.__joke_action() elif intent == 'đánh giá': self.__appreciation_action() return elif intent == 'nhạc': self.__playmusic(entities) return elif intent == 'light': self.__light_action(entities) return elif intent == 'current time': ti = datetime.datetime.now().time() self.__text_action(ti) else: # No recognized intent self.__text_action(self.nlg_vn.unknown()) # return except Exception as e: print("Failed wit!") print(e) traceback.print_exc() self.__text_action( "Code thối lỗi banh xác rồi, fix đêêêêê") return self.decide_action(langague='vi') def __device_action(self, nlu_entities=None): action = None position = None device = None mode = None if nlu_entities is not None: if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'].lower().encode() if 'position' in nlu_entities: position = nlu_entities['position'][0]['value'].lower().encode( ) if 'device' in nlu_entities: device = nlu_entities['device'][0]['value'].lower().encode() if 'mode' in nlu_entities: mode = nlu_entities['mode'][0]['value'].lower().encode() if device is not None and action is not None: if device == 'loa'.encode(): if action == 'bật'.encode() or action == 'tắt'.encode(): os.system('irsend SEND_ONCE speaker KEY_POWER') elif action == 'tăng'.encode() or action == 'lớn'.encode(): os.system('irsend SEND_ONCE speaker KEY_VOLUMEUP') elif action == 'giảm'.encode() or action == 'nhỏ'.encode(): os.system('irsend SEND_ONCE speaker KEY_VOLUMEDOWN') elif action == 'ánh sáng'.encode(): os.system('irsend SEND_ONCE speaker KEY_LIGHTS_TOGGLE') elif device == 'điều hòa'.encode(): print('%s, %s, %s' % (device, mode, action)) if action == 'bật'.encode() or action == 'chuyển'.encode(): if mode is not None and mode == 'lạnh'.encode(): os.system('irsend SEND_ONCE air COLD') # lanh self.__text_action('đã bật điều hoà, chế độ lạnh') elif mode is not None and mode == 'nóng'.encode(): os.system('irsend SEND_ONCE air HOT') # nong self.__text_action('đã bật điều hoà, chế độ nóng') elif action == 'tắt'.encode(): os.system('irsend SEND_ONCE air OFF') # tắt self.__text_action('đã tắt điều hoà') elif action == 'giảm'.encode(): os.system('irsend SEND_ONCE air DOWN') # DOWN self.__text_action('đã giảm điều hoà') elif action == 'tăng'.encode(): os.system('irsend SEND_ONCE air UP') # UP self.__text_action('đã tăng điều hoà') elif device == 'quạt'.encode(): if action == 'bật'.encode() or action == 'chuyển'.encode(): if mode is not None and mode == 'lạnh'.encode(): os.system( 'irsend SEND_ONCE fan KEY_POWER KEY_BLUE KEY_BLUE' ) # lanh self.__text_action('đã bật quạt chế độ lạnh') elif mode is not None and mode == 'nóng': os.system( 'irsend SEND_ONCE fan KEY_POWER KEY_0 KEY_0' ) # nong self.__text_action('đã bật quạt chế độ nóng') else: os.system( 'irsend SEND_ONCE fan KEY_POWER KEY_BLUE KEY_BLUE' ) # lanh self.__text_action('đã bật quạt chế độ nóng') elif action == 'tắt'.encode(): os.system( 'irsend SEND_ONCE fan KEY_POWER KEY_POWER') # tắt self.__text_action('đã tắt quạt') elif action == 'giảm'.encode(): if mode is not None and mode == 'lạnh'.encode(): os.system('irsend SEND_ONCE fan KEY_DOWN KEY_DOWN' ) # giam self.__text_action('đã giảm quạt chế độ lạnh') elif mode is not None and mode == 'nóng'.encode(): os.system( 'irsend SEND_ONCE fan KEY_1 KEY_1') # giam self.__text_action('đã giảm quạt chế độ nóng') elif action == 'tăng'.encode(): if mode is not None and mode == 'lạnh'.encode(): os.system( 'irsend SEND_ONCE fan KEY_UP KEY_UP') # tăng self.__text_action('đã tăng quạt chế độ lạnh') elif mode is not None and mode == 'nóng'.encode(): os.system( 'irsend SEND_ONCE fan KEY_0 KEY_0') # giam self.__text_action('đã tăng quạt chế độ nóng') elif action == 'xoay'.encode(): os.system('irsend SEND_ONCE fan KEY_RO KEY_RO') # xoay self.__text_action('đã xoay quạt') def __light_action(self, nlu_entities=None): action = None position = None light = Light(23) if nlu_entities is not None: if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'] if 'position' in nlu_entities: position = nlu_entities['position'][0]['value'] if action == 'on': light.turn_on() elif action == 'off': light.turn_off() self.__text_action("Ok") else: self.__text_action( "I'm sorry, I couldn't understand what you meant by that") def __joke_action(self): joke = self.nlg.joke() if joke is not None: self.__text_action(joke) else: self.__text_action("I couldn't find any jokes") def __user_status_action(self, nlu_entities=None): attribute = None if (nlu_entities is not None) and ("Status_Type" in nlu_entities): attribute = nlu_entities['Status_Type'][0]['value'] self.__text_action(self.nlg.user_status(attribute=attribute)) def __user_name_action(self): if self.nlg.user_name is None: self.__text_action( "I don't know your name. You can configure it in bot.py") self.__text_action(self.nlg.user_name) # def __appearance_action(self): # requests.get("http://localhost:8080/face") def __appreciation_action(self): if self.bot_vn in ['ty', 'be', 'xuka']: self.__text_action(self.nlg_vn.appreciation()) else: self.__text_action(self.nlg.appreciation()) def __acknowledge_action(self, langague=None): if self.bot_vn in ['ty', 'be', 'xuka']: self.__text_action(self.nlg_vn.acknowledge()) else: self.__text_action(self.nlg.acknowledge()) def __insult_action(self): self.__text_action(self.nlg.insult()) def __personal_status_action(self): self.__text_action(self.nlg.personal_status()) def __text_action(self, text=None): if text is not None: # requests.get("http://localhost:8080/statement?text=%s" % text) # self.speech.synthesize_text(text) if self.bot_vn in ['ty', 'be', 'xuka']: say(text, 'vi', self.bot_vn) else: say(text) # Voi viet nam thoi def __news_action(self, nlu_entities=None, language='vi', oldintent=None): country = None intent = None action = None if nlu_entities is not None: # get entites from API if 'country' in nlu_entities: country = nlu_entities['country'][0]['value'] if 'intent' in nlu_entities: intent = nlu_entities['intent'][0]['value'] # doc tin tuc cua nc nao if country is not None and 'việt' in country.lower(): self.__text_action('tin việt nam ạ, chờ em tý!') # todo:get_vnex_news news_vn = self.knowledge.get_news('vi') if news_vn: # requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines})) interest = self.nlg.article_interest(news_vn) if interest is not None: self.__text_action(interest) elif country is not None and 'nhật bản' in country.lower(): self.__text_action( 'tin nhật bản ạ, chờ tý em bảo chị gu gồ đọc cho %s nghe' % my_name_vn) news_jp = self.knowledge.get_news('jp') if news_jp: # requests.post("http://localhost:8080/news", data=json.dumps({"articles":headlines})) interest = self.nlg.article_interest(news_jp) if interest is not None: say(interest, 'ja') self.decide_action(langague=language, oldintent=intent, pre_message=interest) else: self.__text_action('không tìm thấy tin nào hết, %s ạ' % my_name_vn) elif oldintent is None: self.__text_action( 'Anh muốn nghe tin gì, nước nào nhật bản hay việt nam hả %s ?' % my_name_vn) self.decide_action(langague=language, oldintent=intent) def __weather_action(self, nlu_entities=None): current_dtime = datetime.datetime.now() skip_weather = False # used if we decide that current weather is not important weather_obj = self.knowledge.find_weather(my_address) temperature = weather_obj['temperature'] icon = weather_obj['icon'] wind_speed = weather_obj['windSpeed'] weather_speech = self.nlg.weather(temperature, current_dtime, "present") forecast_speech = None if nlu_entities is not None: if 'datetime' in nlu_entities: if 'grain' in nlu_entities['datetime'][0] and nlu_entities[ 'datetime'][0]['grain'] == 'day': dtime_str = nlu_entities['datetime'][0][ 'value'] # 2016-09-26T00:00:00.000-07:00 dtime = dateutil.parser.parse(dtime_str) if current_dtime.date() == dtime.date(): # hourly weather forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['hourly_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) elif current_dtime.date() < dtime.date( ): # sometime in the future ... get the weekly forecast/ handle specific days forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True if 'Weather_Type' in nlu_entities: weather_type = nlu_entities['Weather_Type'][0]['value'] print(weather_type) if weather_type == "current": forecast_obj = { 'forecast_type': 'current', 'forecast': weather_obj['current_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'today': forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['hourly_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) elif weather_type == 'tomorrow': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['tomorrow_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True elif weather_type == '3 day' or weather_type == '7 day': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) skip_weather = True else: forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['hourly_forecast'], 'location': my_address } forecast_speech = self.nlg.forecast(forecast_obj) weather_data = { "temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour } # requests.post("http://localhost:8080/weather", data=json.dumps(weather_data)) if not skip_weather: # self.speech.synthesize_text(weather_speech) say(weather_speech) if forecast_speech is not None: # self.speech.synthesize_text(forecast_speech) say(forecast_speech) def __weather_action_vn(self, nlu_entities=None): current_dtime = datetime.datetime.now() skip_weather = False # used if we decide that current weather is not important weather_obj = self.knowledge.find_weather(my_address) temperature = weather_obj['temperature'] icon = weather_obj['icon'] wind_speed = weather_obj['windSpeed'] weather_speech = self.nlg_vn.weather(temperature, current_dtime, "present") forecast_speech = None if nlu_entities is not None: if 'timely' in nlu_entities: timely = nlu_entities['timely'][0]['value'] print(timely) if timely == 'hôm nay': forecast_obj = { 'forecast_type': 'hourly', 'forecast': weather_obj['hourly_forecast'], 'location': my_address } forecast_speech = self.nlg_vn.forecast(forecast_obj) elif timely == 'bây giờ': forecast_obj = { 'forecast_type': 'current', 'forecast': weather_obj['current_forecast'], 'location': my_address } forecast_speech = self.nlg_vn.forecast(forecast_obj) elif timely == 'ngày mai': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['tomorrow_forecast'], 'location': my_address } forecast_speech = self.nlg_vn.forecast(forecast_obj) skip_weather = True elif 'tuần' in timely or timely == 'cuối tuần': forecast_obj = { 'forecast_type': 'daily', 'forecast': weather_obj['weekly_forecast'], 'location': my_address } forecast_speech = self.nlg_vn.forecast(forecast_obj) skip_weather = True weather_data = { "temperature": temperature, "icon": icon, 'windSpeed': wind_speed, "hour": datetime.datetime.now().hour } # requests.post("http://localhost:8080/weather", data=json.dumps(weather_data)) if not skip_weather: # self.speech.synthesize_text(weather_speech) say(weather_speech, 'vi', self.bot_vn) if forecast_speech is not None: # self.speech.synthesize_text(forecast_speech) say(forecast_speech, 'vi', self.bot_vn) def __maps_action(self, nlu_entities=None): location = None map_type = None origin = None destination = None maps_url = None if self.bot_vn in ['ty', 'be', 'xuka']: if nlu_entities is not None: if 'city' in nlu_entities: location = nlu_entities['city'][0]["value"] if 'country' in nlu_entities: country = nlu_entities['country'][0]["value"] if 'from' in nlu_entities: origin = nlu_entities['from'][0]["value"] if 'to' in nlu_entities: destination = nlu_entities['to'][0]["value"] if origin is not None and destination is not None: maps_url = self.knowledge.get_direction_map( origin, destination) location = 'đường đi' else: if location is not None: maps_url = self.knowledge.get_map_url(location, map_type) else: self.__text_action( "Em xin lỗi, em không biết nơi anh muốn tìm") if maps_url is not None: maps_action = "chắc chắn rồi, đây là bản đồ %s." % location body = {'url': maps_url} # requests.post("http://localhost:8080/image", data=json.dumps(body)) say(maps_action, 'vi', self.bot_vn) else: if nlu_entities is not None: if 'location' in nlu_entities: location = nlu_entities['location'][0]["value"] if "Map_Type" in nlu_entities: map_type = nlu_entities['Map_Type'][0]["value"] if location is not None: maps_url = self.knowledge.get_map_url(location, map_type) maps_action = "Sure. Here's a map of %s." % location body = {'url': maps_url} # requests.post("http://localhost:8080/image", data=json.dumps(body)) self.speech.synthesize_text(maps_action) else: self.__text_action( "I'm sorry, I couldn't understand what location you wanted." ) def __playmusic(self, nlu_entities=None): chanel = 'youtube' action = None region_code = 'VN' location = None search_query = None if self.bot_vn in ['ty', 'be', 'xuka']: # vn bot if nlu_entities is not None: if 'chanel' in nlu_entities: chanel = nlu_entities['chanel'][0]['value'] if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'] if 'search_query' in nlu_entities: search_query = nlu_entities['search_query'][0]['value'] # Play music if chanel is not None: if 'youtube' == chanel: if 'mở' == action: stop() if search_query is not None and search_query != 'nghe': YouTube_Autoplay(search_query, region_code) else: YouTube_Autoplay('Le quyen', region_code) elif 'tắt' == action: stop() self.__text_action("ok, em tắt rồi") else: self.__text_action("em chưa hiểu, thưa %s" % my_name) else: # en bot if nlu_entities is not None: if 'chanel' in nlu_entities: chanel = nlu_entities['chanel'][0]['value'] if 'action' in nlu_entities: action = nlu_entities['action'][0]['value'] if 'local_search_query' in nlu_entities: local_search_query = nlu_entities['local_search_query'][0][ 'value'] if 'location' in nlu_entities: location = nlu_entities['location'][0]['value'] region_code = self.knowledge.get_country_code(location) if chanel is not None: if 'youtube' == chanel: if 'play' == action: stop() if local_search_query is not None: YouTube_Autoplay(local_search_query, region_code) else: YouTube_Autoplay('Le quyen', region_code) elif 'stop' == action: stop() self.__text_action("OK, stoped it") else: self.__text_action("what are you want to do %s" % my_name) def __holidays_action(self, nlu_entities=None): country = None if nlu_entities is not None: if 'country' in nlu_entities: country = nlu_entities['country'][0]["value"] print(nlu_entities['country'][0]["value"]) if country is not None: holidays = self.knowledge.get_holidays(country) else: holidays = self.knowledge.get_holidays(country='vi') if not holidays: self.__text_action("No upcoming events found.") else: i = 0 words = '' while i < 3: dayho = dateutil.parser.parse(holidays[i]['start']) print(dayho.weekday()) dayofweek = self.__dayofweek(dayho.weekday()) words = "%s . %s Ngày %s, tháng %s, năm %s, %s" % ( words, dayofweek, dayho.day, dayho.month, dayho.year, holidays[i]['summary']) i = i + 1 self.__text_action(words) def __dayofweek(self, i): days = [ 'Thứ hai', 'Thứ ba', 'Thứ tư', 'Thứ năm', 'Thứ sáu', 'Thứ bảy', 'Chủ nhật' ] return days[i] def __alarm(self, nlu_entities=None): __datetime = None __action = None current_ts = time.time() if nlu_entities is not None: if 'datetime' in nlu_entities: __datetime = nlu_entities['datetime'][0]['value'] if __datetime is not None: dtime = dateutil.parser.parse(__datetime) str = '%s giờ, %s, Ngày %s, Tháng %s' % (dtime.hour, dtime.minute, dtime.day, dtime.month) timestamp2 = time.mktime(dtime.timetuple()) delay = timestamp2 - current_ts if delay > 0: _thread.start_new_thread(self.__start_alarm, ( "alarm", delay, )) say('đã cài báo thức %s', str) # Function to check if mpv is playing def __ismpvplaying(self): for pid in psutil.pids(): p = psutil.Process(pid) if 'mpv' in p.name(): mpvactive = True break else: mpvactive = False return mpvactive # registe alarm def __start_alarm(self, threadName, delay): time.sleep(delay) alarm_file = "/home/pi/AI-Smart-Mirror/sample-audio-files/martian-gun.mp3" song = AudioSegment.from_mp3(alarm_file) i = 0 while i <= 30: play(song) time.sleep(2) i += 1