def find_trends_handler(request): uid = request.access_token() user_cache = twitter_cache.get_user_state(uid) resolved_location = False message = "" location = request.get_slot_value("Location") if not location: # Get trends for user's current location user_details = get_user_twitter_details(uid) location = user_details[0]['location'] if location: message += "Finding trends near you . " else: message += "I could not figure out where you are, please set it up on your twitter account . " if location: response = geo_search(request.access_token(), location) # convert natural language text to location top_result = response['result']['places'][0] lon, lat = top_result['centroid'] trend_params = {"lat" : lat, "long" : lon} trend_location = closest_trend_search(request.access_token(), trend_params) # find closest woeid which has trends woeid = trend_location[0]['woeid'] trends = list_trends(request.access_token(), trend_location[0]['woeid']) # List top trends trend_lst = [trend['name'] for trend in trends[0]['trends']] message += "The top trending topics near {0} are, ".format(trend_location[0]['name']) message += "\n".join(["{0}, {1}, ".format(index+1, trend) for index, trend in enumerate(trend_lst)]) return r.create_response(message=message)
def find_trends_handler(request): uid = request.access_token() user_cache = twitter_cache.get_user_state(uid) resolved_location = False message = "" location = request.get_slot_value("Location") should_end_session = True if not location: # Get trends for user's current location user_details = get_user_twitter_details(uid) location = user_details[0]['location'] if location: message += "Finding trends near you . " else: message += "I could not figure out where you are, please set it up on your twitter account . " if location: response = geo_search(request.access_token(), location) # convert natural language text to location top_result = response['result']['places'][0] lon, lat = top_result['centroid'] trend_params = {"lat" : lat, "long" : lon} trend_location = closest_trend_search(request.access_token(), trend_params) # find closest woeid which has trends woeid = trend_location[0]['woeid'] trends = list_trends(request.access_token(), trend_location[0]['woeid']) # List top trends trend_lst = [trend['name'] for trend in trends[0]['trends']] message += "The top trending topics near {0} are, ".format(trend_location[0]['name']) message += "\n".join(["{0}, {1}, ".format(index+1, trend) for index, trend in enumerate(trend_lst)]) return r.create_response(message=message, end_session=should_end_session)
def launch_request_handler(request): """ Annotate functions with @VoiceHandler so that they can be automatically mapped to request types. Use the 'request_type' field to map them to non-intent requests """ user_id = request.access_token() get_user_twitter_details(user_id) if user_id in twitter_cache.users(): user_cache = twitter_cache.get_user_state(user_id) user_cache["amzn_id"]= request.user_id() base_message = "Welcome to Twitter, {} .".format(user_cache["screen_name"]) print (user_cache) if 'pending_action' in user_cache: base_message += " You have one pending action . " print ("Found pending action") if 'description' in user_cache['pending_action']: print ("Found description") base_message += user_cache['pending_action']['description'] return r.create_response(base_message) card = r.create_card(title="Please log into twitter") response = r.create_response(message="Welcome to twitter, looks like you haven't logged in!" " Log in via the alexa app.", card_obj=card) return response