Skip to content

Instantly share code, notes, and snippets.

@ghiculescu
Last active November 1, 2017 23:32
Show Gist options
  • Save ghiculescu/39681321b543f88c0a3ea9e7ea3aaeb1 to your computer and use it in GitHub Desktop.
Save ghiculescu/39681321b543f88c0a3ea9e7ea3aaeb1 to your computer and use it in GitHub Desktop.
SMS you a reminder to shave when you clock in to work
require_relative 'env' # store my secrets in a separate file
require 'sinatra'
require 'open-uri'
require 'aws-sdk'
require 'httparty'
require 'twilio-ruby'
post '/webhook' do
data = JSON.parse(request.body.read)
user_id = data['payload']['body']['user_id']
photo_url = data['payload']['body']['photo']
file = open(photo_url).read
client = Aws::Rekognition::Client.new(region: "us-east-1")
resp = client.detect_faces(image: {bytes: file}, attributes: ['ALL'])
has_beard = resp.face_details.first.beard.value || resp.face_details.first.beard.confidence < 0.8
has_mustache = resp.face_details.first.mustache.value || resp.face_details.first.mustache.confidence < 0.8
if has_beard || has_mustache
user_data = HTTParty.get("https://my.tanda.co/api/v2/users/#{user_id}", headers: {"Authorization" => "bearer #{ENV['TANDA_API_KEY']}"})
if user_data['phone']
phone = user_data['phone'].delete(" ").sub("0", "+61")
client = Twilio::REST::Client.new(ENV['TWILIO_SID'], ENV['TWILIO_TOKEN'])
client.account.messages.create(from: ENV['TWILIO_FROM_NUMBER'], to: phone, body: "You need to shave!")
end
end
end
source 'https://rubygems.org'
gem 'sinatra'
gem 'aws-sdk', '~> 3'
gem 'httparty'
gem 'twilio-ruby'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment