Back to Parent

AabioFinal
require "sinatra"
require 'sinatra/reloader' if development?

require 'alexa_skills_ruby'
require 'httparty'
require 'iso8601'

require 'twilio-ruby'
require 'giphy'

require 'sinatra/activerecord'
require 'rake'
require 'json'

require 'active_support/all'
require "active_support/core_ext"

require 'date'


# ----------------------------------------------------------------------


# Load environment variables using Dotenv. If a .env file exists, it will

# set environment variables from that file (useful for dev environments)

configure :development do
  require 'dotenv'
  Dotenv.load
end

# require models

require_relative './models/user'
require_relative './models/sleep_log'
require_relative './models/feed_log'
require_relative './models/poop_log'

# enable sessions for this project

enable :sessions

@client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]




# ----------------------------------------------------------------------

#     How you handle your Alexa

# ----------------------------------------------------------------------


class CustomHandler < AlexaSkillsRuby::Handler

  on_intent("RecordFeedIntent") do
    response.set_output_speech_text("Well fed baby, happy baby")

    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_gif_for ("feeding baby")

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: " πŸ‘Ά 🍼 Just had a nice feed πŸ‘Ό ",
      media_url: gif_url
    )

    logger.info 'RecordFeedIntent processed'
    #add a record to feed_log

    feed_log.create(phone_number: "+18646337057")
  end

  on_intent("RecordDiaperChangeIntent") do
    response.set_output_speech_text("your baby is feeling much better")

    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_gif_for ("diaper change")

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: " πŸ‘Ά Got my diapers changed, feeling better ✌️",
      media_url: gif_url
    )

    logger.info 'RecordDiaperChangeIntent processed'

    #add a record to diaper_log

    poop_log.create(phone_number: "+18646337057")
  end

  on_intent("RecordSleepIntent") do
    response.set_output_speech_text("sleep well baby")

    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_gif_for ("sleeping baby")

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: "πŸ‘Ό πŸ’€ I am sleeping πŸ”‡*DO NOT DISTURB*πŸ“΅ ",
      media_url: gif_url
    )

    logger.info 'RecordFeedIntent processed'

    #add a record to sleep_log

    sleep_log.create(phone_number: "+18646337057")
  end

  on_intent("AMAZON.HelpIntent") do

    response.set_output_speech_text("Howdy! I am Aabio, a skill that will record your baby stats, like feed, sleep and diaper change")
    logger.info 'AMAZON.HelpIntent processed'

  end



end

# ----------------------------------------------------------------------

#     ROUTES, END POINTS AND ACTIONS

# ----------------------------------------------------------------------



get '/' do
  "Hello from Aabio"
end



post '/alexa/incoming' do
  content_type :json

  handler = CustomHandler.new(application_id: ENV['ALEXA_APPLICATION_ID'], logger: logger)

  begin
    hdrs = { 'Signature' => request.env['HTTP_SIGNATURE'], 'SignatureCertChainUrl' => request.env['HTTP_SIGNATURECERTCHAINURL'] }
    handler.handle(request.body.read, hdrs)
  rescue AlexaSkillsRuby::Error => e
    logger.error e.to_s
    403
  end

end


get "/sms/incoming" do
  session["last_intent"] ||= nil

  session["counter"] ||= 1
  count = session["counter"]

  sender = params[:From] || ""
  body = params[:Body] || ""
  body = body.downcase.strip

  message =""
  media = nil

  if is_help_intent? body
    message = get_help_message
  elsif is_casual_intent? body
      message = get_greeting
  elsif body == "status" or body.include? "status"
    message = get_status
  elsif body == "how are you" or body.include? "feel"
    FEEL = ["Happy baby", "Sad baby", "Baby smile", "baby cry"]
    # message = FEEL.sample


    how_many_poops = PoopLog.where( phone_number: sender ).where( "created_at > ?" , Time.now - 4.hours ).count
    how_many_bottles = FeedLog.where( phone_number: sender ).where( "created_at > ?" , Time.now - 4.hours ).count

    if how_many_poops > 3
      message = "I'm feeling shitty"
    end
    elsif how_many_bottles > 3
      message = "I'm hungry today"
    end

    media = get_gif_for(message)

  elsif body == "how many poops today" or body.include? "poop" or body.include? "πŸ’©"
    POOP = ["1πŸ’©", "2πŸ’©πŸ’©", "3πŸ’©πŸ’©πŸ’©", "4πŸ’©πŸ’©πŸ’©πŸ’©", "5πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©","6πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©"]

    how_many_poops = PoopLog.where( phone_number: sender ).where( "created_at > ?" , Time.now - 24.hours ).count

    message = "#{how_many_poops}" + ("πŸ’©"*how_many_poops)

    #message = POOP.sample

  elsif body == "how many feed today"  or body.include? "feed" or body.include? "🍼"
    FEED = ["1🍼", "2🍼🍼", "3🍼🍼🍼", "4🍼🍼🍼🍼", "5🍼🍼🍼🍼🍼", "6🍼🍼🍼🍼🍼🍼"]
    message = FEED.sample
  elsif body == "how much sleep today" or body.include? "sleep" or body.include? "βŒ›"
    SLEEP = ["😴1hrβŒ›", "😴2hrβŒ›βŒ›", "😴3hrβŒ›βŒ›βŒ›", "😴4hrβŒ›βŒ›βŒ›βŒ›"]
    message = SLEEP.sample

  else
    media = get_gif_for ("confused baby")
    message = "Type [poop] or [sleep] or [feed] or [Status] or [How are you]"
  end


  twiml = Twilio::TwiML::MessagingResponse.new do |r|
    r.message do |m|
      m.body( message )
      unless media.nil?
        m.media( media )
      end
    end
  end


  content_type 'text/xml'
  twiml.to_s

end

# Casual intent

CASUAL_INTENTS = ["hi", "hello", "yo", "whatsup", "what's up", "hey"]

def is_casual_intent? incoming_text

  CASUAL_INTENTS.each do |word|
    if incoming_text.start_with?(word)
      return true
    end
  end

  return false

end

GREETINGS = ["Hi","Yo", "Hey","Howdy", "Hello", "Ahoy", "β€˜Ello", "Aloha", "Hola", "Bonjour", "Hallo", "Ciao", "Konnichiwa", "Vanakam!"]
INTROS = ["I am Aabio, a baby bot that is going to replicate your baby. You can communicate with me as you would with your baby and I will do my best to respond. Some sample commands are [Status],[How are you]"]

def get_greeting
  GREETINGS.sample + ", " + INTROS.sample
end


# Help intent

HELP_INTENTS = ["help", "i am confused", "h", "please help", "what", "what are you"]

def is_help_intent? incoming_text

  HELP_INTENTS.each do |word|
    if incoming_text.start_with?(word)
      return true
    end
  end

  return false

end

def get_help_message
  "Howdy! I am Aabio, a bot that is going to replicate your baby. You can communicate with me as you would with your baby and I will do my best to respond."
end

#=========================================================

#Status intent

#=========================================================

POOP = ["1πŸ’©", "2πŸ’©πŸ’©", "3πŸ’©πŸ’©πŸ’©", "4πŸ’©πŸ’©πŸ’©πŸ’©", "5πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©","6πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©πŸ’©"]
FEED = ["1🍼", "2🍼🍼", "3🍼🍼🍼", "4🍼🍼🍼🍼", "5🍼🍼🍼🍼🍼", "6🍼🍼🍼🍼🍼🍼"]
SLEEP = ["😴1hrβŒ›", "😴2hrβŒ›βŒ›", "😴3hrβŒ›βŒ›βŒ›", "😴4hrβŒ›βŒ›βŒ›βŒ›"]

def get_status
  POOP.sample + ", " + FEED.sample + ", " +SLEEP.sample
end

# THE APPLICATION ID CAN BE FOUND IN THE



get "/test/:action" do

  if params[:action] == "feed"

    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_trending_gif

    puts gif_url

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: "feeding gif",
      media_url: gif_url
    )

    "Recorded feeding and sent a random gif to your phone"

  elsif params[:action] == "diaper"
    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_trending_gif

    puts gif_url

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: "diaper gif",
      media_url: gif_url
    )

    "Recorded diaper change and sent a random gif to your phone"


  elsif params[:action] == "sleep"

    @client = Twilio::REST::Client.new ENV["TWILIO_ACCOUNT_SID"], ENV["TWILIO_AUTH_TOKEN"]

    gif_url = get_trending_gif

    puts gif_url

    @client.api.account.messages.create(
      from: ENV['TWILIO_FROM'],
      to: "+18646337057",
      body: "sleeping gif",
      media_url: gif_url
    )

    "Recorded sleeping and sent a random gif to your phone"

  else
    #slots = request.intent.slots

    "Howdy! I am Aabio, a skill that will record your baby stats, like feed, sleep and diaper change"
  end

end



# ----------------------------------------------------------------------

#     ERRORS

# ----------------------------------------------------------------------

error 401 do
  "Not allowed!!!"
end



#--------------------------------------------------

#--------------------------------------------------

#--------------------------------------------------

#

# =>  GIPHY

#

#--------------------------------------------------

#--------------------------------------------------

#--------------------------------------------------


def get_gif_for query


  Giphy::Configuration.configure do |config|
    config.api_key = ENV["GIPHY_API_KEY"]
  end

  results = Giphy.search( query, {limit: 10})
  gif = nil

  #puts results.to_yaml

  unless results.empty?
    gif = results.sample.fixed_width_downsampled_image.url.to_s
  end

  gif

end



def get_trending_gif


  Giphy::Configuration.configure do |config|
    config.api_key = ENV["GIPHY_API_KEY"]
  end

  results = Giphy.trending(limit: 10)
  gif = nil

  #puts results.to_yaml

  unless results.empty?
    gif = results.sample.fixed_width_downsampled_image.url.to_s
  end

  gif

end


# Method to return the current date time


def get_current_time_date
    current_time = DateTime.now
    current_time.strftime "%d/%m/%Y %H:%M"
    # => "14/09/2011 17:02"

  end
Click to Expand

Content Rating

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

0