From 27ec60830e3c2cd93d4fbeaf9b9ff0d90fe60ef1 Mon Sep 17 00:00:00 2001 From: Luke Kingsley Date: Mon, 30 Jul 2018 09:03:34 -0600 Subject: [PATCH] Added lunch bot that randomizes lunch suggestions given a cost and a distance --- bots/lunch-bot.rb | 87 +++++++++++++++++++++++++++++++++++++++++++++ lib/lunch/search.rb | 18 ++++++++++ 2 files changed, 105 insertions(+) create mode 100644 bots/lunch-bot.rb create mode 100644 lib/lunch/search.rb diff --git a/bots/lunch-bot.rb b/bots/lunch-bot.rb new file mode 100644 index 0000000..b9ca555 --- /dev/null +++ b/bots/lunch-bot.rb @@ -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 \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 diff --git a/lib/lunch/search.rb b/lib/lunch/search.rb new file mode 100644 index 0000000..098288d --- /dev/null +++ b/lib/lunch/search.rb @@ -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 \ No newline at end of file