def handle(self, text, mic): """ Responds to user-input, typically speech text, by listing the user's Facebook friends with birthdays today. Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) """ oauth_access_token = self.profile['keys']["FB_TOKEN"] graph = facebook.GraphAPI(oauth_access_token) try: results = graph.request("me/friends", args={'fields': 'id,name,birthday'}) except facebook.GraphAPIError: mic.say( self.gettext( "I have not been authorized to query your Facebook. If you " + "would like to check birthdays in the future, please visit " + "the Naomi dashboard.")) return except: mic.say( self.gettext("I apologize, there's a problem with that " + "service at the moment.")) return needle = datetime.datetime.now( tz=app_utils.get_timezone(self.profile)).strftime("%m/%d") people = [] for person in results['data']: try: if needle in person['birthday']: people.append(person['name']) except: continue if len(people) > 0: if len(people) == 1: output = self.gettext("%s has a birthday today.") % people[0] else: output = (self.gettext( "Your friends with birthdays today are %s and %s.") % (", ".join(people[:-1]), people[-1])) else: output = self.gettext("None of your friends have birthdays today.") mic.say(output)
def handle(self, text, mic): """ Reports the current time based on the user's timezone. Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) """ tz = app_utils.get_timezone(self.profile) now = datetime.datetime.now(tz=tz) if now.minute == 0: fmt = "It is {t:%l} {t:%p} right now." else: fmt = "It is {t:%l}:{t:%M} {t:%p} right now." mic.say(self.gettext(fmt).format(t=now))
def handle(self, text, mic): """ Reports the current time based on the user's timezone. Arguments: intent -- intentparser result with the following layout: intent['action'] = the action to take when the intent is activated intent['input'] = the original words intent['matches'] = dictionary of lists with matching elements, each with a list of the actual words matched intent['score'] = how confident Naomi is that it matched the correct intent. mic -- used to interact with the user (for both input and output) """ tz = app_utils.get_timezone() now = datetime.datetime.now(tz=tz) if now.minute == 0: fmt = self.gettext("It is {t:%l} {t:%p} right now.") else: fmt = self.gettext("It is {t:%l}:{t:%M} {t:%p} right now.") mic.say(fmt.format(t=now))
def handleTime(self, intent, mic): print("handleTime") print(intent) if 'LocationKeyword' in intent['matches']: for location in intent['matches']['LocationKeyword']: geo = geocoder.osm(location) tzf = TimezoneFinder() tz = tzf.timezone_at(lng=geo.lng, lat=geo.lat) now = datetime.datetime.now(tz=timezone(tz)) if now.minute == 0: fmt = self.gettext("IT IS {t:%l} {t:%p} IN {l} RIGHT NOW.") else: fmt = self.gettext( "IT IS {t:%l}:{t:%M} {t:%p} IN {l} RIGHT NOW.") mic.say(fmt.format(l=location, t=now)) else: now = datetime.datetime.now(tz=app_utils.get_timezone()) if now.minute == 0: fmt = self.gettext("IT IS {t:%l} {t:%p}.") else: fmt = self.gettext("IT IS {t:%l}:{t:%M} {t:%p}.") mic.say(fmt.format(t=now)) return True
def getEventsTomorrow(self, mic): # Time Delta function for adding one day one_day = datetime.timedelta(days=1) tz = app_utils.get_timezone(profile.get_profile()) # Gets tomorrows Start and End Time in RFC3339 Format d = datetime.datetime.now(tz=tz) + one_day utcString = d.isoformat() m = re.search(r'((\+|\-)[0-9]{2}\:[0-9]{2})', str(utcString)) utcString = m.group(0) tomorrowStartTime = "".join( [str(d.strftime("%Y-%m-%d")), "T00:00:00", utcString]) tomorrowEndTime = str(d.strftime("%Y-%m-%d")) + "T23:59:59" + utcString page_token = None while True: # Gets events from primary calendar from each page # in tomorrow day boundaries events = self.service.events().list( calendarId='primary', pageToken=page_token, timeMin=tomorrowStartTime, timeMax=tomorrowEndTime).execute() if (len(events['items']) == 0): mic.say(self.gettext("You have no events scheduled Tomorrow")) return for event in events['items']: try: eventTitle = event['summary'] eventTitle = str(eventTitle) eventRawStartTime = event['start'] eventRawStartTime = eventRawStartTime['dateTime'].split( "T") temp = eventRawStartTime[1] startHour, startMinute, temp = temp.split(":", 2) startHour = int(startHour) appendingTime = self.gettext("am") if ((startHour - 12) > 0): startHour = startHour - 12 appendingTime = self.gettext("pm") startMinute = str(startMinute) startHour = str(startHour) mic.say(" ".join([ eventTitle, "at", startHour + ":" + startMinute, appendingTime ])) except KeyError: mic.say("Check Calendar that you added it correctly") page_token = events.get('nextPageToken') if not page_token: return
def getEventsToday(self, mic): tz = app_utils.get_timezone(profile.get_profile()) # Get Present Start Time and End Time in RFC3339 Format d = datetime.datetime.now(tz=tz) utcString = d.isoformat() m = re.search(r'((\+|\-)[0-9]{2}\:[0-9]{2})', str(utcString)) utcString = str(m.group(0)) todayStartTime = str(d.strftime("%Y-%m-%d")) + "T00:00:00" + utcString todayEndTime = str(d.strftime("%Y-%m-%d")) + "T23:59:59" + utcString page_token = None while True: # Gets events from primary calendar from each page # in present day boundaries events = self.service.events().list(calendarId='primary', timeMin=todayStartTime, timeMax=todayEndTime, singleEvents=True, orderBy='startTime').execute() if (len(events['items']) == 0): mic.say("You have no events scheduled for today") return for event in events['items']: try: eventTitle = event['summary'] eventTitle = str(eventTitle) eventRawStartTime = event['start'] eventRawStartTime = eventRawStartTime['dateTime'].split( "T") temp = eventRawStartTime[1] startHour, startMinute, temp = temp.split(":", 2) startHour = int(startHour) appendingTime = "am" if ((startHour - 12) > 0): startHour = startHour - 12 appendingTime = "pm" startMinute = str(startMinute) startHour = str(startHour) mic.say(" ".join([ eventTitle, self.gettext("at"), startHour + ":" + startMinute, appendingTime ])) except KeyError: mic.say( self.gettext( "Check Calendar that you added it correctly")) page_token = events.get('nextPageToken') if not page_token: return
def handle(self, intent, mic): """ Responds to user-input, typically speech text, by listing the user's Facebook friends with birthdays today. Arguments: intent -- intentparser result with the following layout: intent['action'] = the action to take when the intent is activated intent['input'] = the original words intent['matches'] = dictionary of lists with matching elements, each with a list of the actual words matched intent['score'] = how confident Naomi is that it matched the correct intent. mic -- used to interact with the user (for both input and output) """ oauth_access_token = profile.get(['keys', 'FB_TOKEN']) _ = self.gettext graph = facebook.GraphAPI(oauth_access_token) try: results = graph.request( "me/friends", args={'fields': 'id,name,birthday'} ) except facebook.GraphAPIError: mic.say( " ".join([ _("I have not been authorized to query your Facebook."), _("If you would like to check birthdays in the future,"), _("please visit the Naomi dashboard.") ]) ) return except Exception: mic.say( " ".join([ _("I apologize,"), _("there's a problem with that service at the moment.") ]) ) return needle = datetime.datetime.now( tz=app_utils.get_timezone() ).strftime("%m/%d") people = [] for person in results['data']: try: if needle in person['birthday']: people.append(person['name']) except KeyError: continue if len(people) > 0: if len(people) == 1: output = _("%s has a birthday today.") % people[0] else: output = _( "Your friends with birthdays today are {} and {}." ).format(", ".join(people[:-1]), people[-1]) else: output = _("None of your friends have birthdays today.") mic.say(output)