Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions bots/lunch-bot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
require 'slackbot_frd'
require 'securerandom'

require_relative '../lib/lunch/search'

class LunchBot < SlackbotFrd::Bot

def contains_trigger(message)
message =~ /(!lunch)/i
end

def does_not_contain(message)
!(message =~ /(template)/i && message =~ /(example)/i)
end

def send_invalid_command_error(slack_connection, channel, thread_ts)
slack_connection.send_message(
channel: channel,
message: "*Template:* !lunch <distance in miles> <cost in $-$$$$> \n *Example:* !lunch 5 $$",
parse: 'none',
thread_ts: thread_ts
)
end

def parse_url(url)
url.split("?")[0]
end

def edit_url(url)
url.sub('biz', "map")
end

def add_callbacks(slack_connection)
slack_connection.on_message do |user:, channel:, message:, timestamp:, thread_ts:|
if message && user != 'angel' && timestamp != thread_ts && contains_trigger(message) && does_not_contain(message)
handle_lunch_request(slack_connection, user, channel, message, thread_ts)
end
end
end

def handle_lunch_request(slack_connection, user, channel, message, thread_ts)
message.slice! "!lunch"
if message == ""
send_invalid_command_error(slack_connection, channel, thread_ts)
else
message = message.split(" ")
if message.count != 2
send_invalid_command_error(slack_connection, channel, thread_ts)
return
end
distance = message[0].to_i
cost = message[1].length
search_api = Lunch::Search.new
if distance > 24
distance = 24
end
if cost > 4
cost = 4
end
restaurant_list = search_api.get(distance, cost)
slack_connection.send_message(
channel: channel,
message: parse_places(restaurant_list),
parse: 'none',
thread_ts: thread_ts
)
end
end

def parse_places(restaurants)
if restaurants['total'] > 0
random = rand(restaurants['businesses'].length)
response = []
response << "<#{parse_url(restaurants['businesses'][random]['url'])}|*#{restaurants['businesses'][random]['name']}*>"
response << "*Directions:* <#{edit_url(restaurants['businesses'][random]['url'])}|:oncoming_automobile:>"
response << "*Rating*: #{restaurants['businesses'][random]['rating']}"
type = []
restaurants['businesses'][random]['categories'].each do |category|
type << "#{category["title"]}"
end
response << "*Type:* #{type.join(", ")}"
response.join("\n")
else
"Looks like there aren't any places that match your criteria :sherlock:"
end
end
end
18 changes: 18 additions & 0 deletions lib/lunch/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

require 'httparty'
require 'json'

module Lunch
class Search
include HTTParty
base_uri "https://api.yelp.com/"
headers 'Authorization' => 'Bearer FMgmMCHUrSSLBuetlQnI0TEFlOFoRV94TEEwkRafuoaZXac7WmocnNPBZDZOtyEF1Og8f2sYdapl_QfBD7DDxKDGCz4iSWf2QW-tGgnCBNea5We8Sw94JJhluoo6W3Yx'

def get(distance, cost)
uri = self.class.get("/v3/businesses/search", query: "term=restaurant&location=6330+S+3000+E+Salt+Lake+City+UT+84121&price=#{cost}&radius=#{distance.to_i*1609}&limit=50").body
JSON.parse(uri)
rescue
{ total: 0 }
end
end
end