Back to Parent

def determine_response (body)
    body = body.downcase.strip
    no = "i didnt quite understand what you mean"
    
    project_id = ENV["GOOGLE_CLOUD_PROJECT"]
    query_result = detect_intent_texts project_id: project_id,
                            session_id: SecureRandom.uuid,
                            texts: [body],
                            language_code:"en-US"
    intent = query_result.intent
    parameters = JSON.parse(query_result.parameters.to_json)
    fullfillment_text = query_result.fulfillment_text
        
    if intent.blank?
      response = fullfillment_text
    else
      intent = intent.display_name
      
      if intent == "EventIntent"
        service = Google::Apis::CalendarV3::CalendarService.new
        service.client_options.application_name = APPLICATION_NAME
        service.authorization = authorize

        # Fetch the next 10 events for the user
        calendar_id = 'primary'
        reply = service.list_events(calendar_id,
                                       max_results: 10,
                                       single_events: true,
                                       order_by: 'startTime',
                                       time_min: Time.now.iso8601)
        puts = 'Upcoming events:'
        puts = 'No upcoming events found' if reply.items.empty?
        reply.items.each do |event|
          start = event.start.date || event.start.date_time
          reply =  "- #{event.summary} (#{start})"
          response = reply
        end
      
      elsif intent == "WelcomeIntent"
        time = Time.new 
        time_today = time.strftime("%A %B %d, %Y %H:%M")
        evening_greetings = ["Toodles", "Namaste", "Accio says hi", "Hej hej", "Hola!", "good evening friend!"]
        morning_greeting = ["good morning", "hello sunshine", "wakey wakey", "good morgon", "lumos"]
        media = "https://media.giphy.com/media/13ZHjidRzoi7n2/giphy.gif"
      
        if time.hour >= 12
            message = evening_greetings.sample 
        elsif time.hour <12
            message = morning_greeting.sample
        end
        response = message
    
      elsif intent == "AboutIntent"
        response = "Hello, I am Accio! I can help you to buy or sell things. If you're stuck, type help."
    
      elsif intent == "SearchIntent"
        items = RakutenWebService::Ichiba::Product.search(keyword: body) # This returns Enumerable object
        items.first(10).each do |item|
          puts "#{item['productName']}, #{item['maxPrice'] * 0.0089} USD" # You can refer to values as well as Hash.
          response = "#{item['productName']}, #{item['maxPrice'] * 0.0089} USD" 
        end
    
      elsif intent == "JokeIntent"
        array_of_lines = IO.readlines("jokes.txt")
        response = array_of_lines.sample
    
      elsif intent == "HelpIntent"
        response = "To buy or sell a product, type buy or sell followed by the name of the product. You can also type Accio followed by the product's name for buying"
      
      elsif intent == "DefaultFallbackIntent"
        response = fullfillment_text
        
      elsif intent == "productNameIntent"
        # puts parameters
        # if parameters.blank?
          # response = fullfillment_text
        # else
        # puts parameters['product_name']
        product_name = parameters['fields']['productName']['listValue']['values']
        number = parameters['fields']['number']['listValue']['values']
        
        puts product_name
        puts number 
        if !product_name.empty?
          product_name = product_name[0]['stringValue'].to_s
        end
        if !number.empty?
          number = number[0]['numberValue'].to_s
        end
        if product_name.empty? || number.empty?
          response = fullfillment_text
        else
          # response = product_name + " " + number
          response = "I found these items : "
          items = RakutenWebService::Ichiba::Product.search(keyword: product_name) # This returns Enumerable object
          items.first(5).each do |item|
            puts item.to_s
            puts "#{item['productName']}, #{item['maxPrice'] * 0.0089 * number.to_i} USD" # You can refer to values as well as Hash.
            response = response + "
            #{item['productName']}, #{item['maxPrice'] * 0.0089 * number.to_i} USD
            #{item['productUrlMobile']}
            " 
          end
        end
        # end
      else
        response = intent + " " + fullfillment_text
      end 
    end
    return response
end
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0