random response with web hook
This commit is contained in:
parent
200d95588b
commit
13e7ae2abd
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
config.rb
|
4
Gemfile
4
Gemfile
@ -1,5 +1,9 @@
|
||||
source 'https://rubygems.org'
|
||||
gem 'thin'
|
||||
gem 'sinatra'
|
||||
|
||||
gem 'faraday'
|
||||
gem 'faraday_middleware'
|
||||
gem 'json'
|
||||
|
||||
gem 'awesome_print'
|
||||
|
10
Gemfile.lock
10
Gemfile.lock
@ -1,8 +1,15 @@
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
awesome_print (1.6.1)
|
||||
daemons (1.2.2)
|
||||
eventmachine (1.0.7)
|
||||
faraday (0.9.1)
|
||||
multipart-post (>= 1.2, < 3)
|
||||
faraday_middleware (0.9.1)
|
||||
faraday (>= 0.7.4, < 0.10)
|
||||
json (1.8.2)
|
||||
multipart-post (2.0.0)
|
||||
rack (1.6.0)
|
||||
rack-protection (1.5.3)
|
||||
rack
|
||||
@ -20,6 +27,9 @@ PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
awesome_print
|
||||
faraday
|
||||
faraday_middleware
|
||||
json
|
||||
sinatra
|
||||
thin
|
||||
|
68
app/meme_bot.rb
Normal file
68
app/meme_bot.rb
Normal file
@ -0,0 +1,68 @@
|
||||
require 'json'
|
||||
require 'workers/random_salutation'
|
||||
require 'workers/get_giphy'
|
||||
|
||||
get '/?' do
|
||||
content_type :json
|
||||
greeting = RandomSalutation.new
|
||||
{
|
||||
text: greeting.short,
|
||||
}.to_json
|
||||
end
|
||||
|
||||
get '/greet/:length/?' do
|
||||
content_type :json
|
||||
greeting = RandomSalutation.new
|
||||
{ text: greeting.send(params[:length]) }.to_json
|
||||
end
|
||||
|
||||
post '/random/?' do
|
||||
raise(InvalidTokenError) unless params[:token] == Config.team_key
|
||||
|
||||
giphy = GetGiphy.new
|
||||
gif = giphy.random( params['text'] )
|
||||
|
||||
connection.post do |c|
|
||||
c.url Config.in_hook
|
||||
c.headers['Content-Type'] = 'application/json'
|
||||
c.body = {
|
||||
channel: "##{params['channel_name']}",
|
||||
text: params['text'],
|
||||
attachments: [{
|
||||
title_link: gif['url'],
|
||||
image_url: gif['image_url'],
|
||||
fallback: 'An amazing random giphy from MemeBot',
|
||||
}]
|
||||
|
||||
}.to_json
|
||||
end
|
||||
end
|
||||
|
||||
post '/search/?' do
|
||||
raise(InvalidTokenError) unless params[:token] == Config.team_key
|
||||
|
||||
giphy = GetGiphy.new
|
||||
gif = giphy.search( params['text'] )
|
||||
|
||||
connection.post do |c|
|
||||
c.url Config.in_hook
|
||||
c.headers['Content-Type'] = 'application/json'
|
||||
c.body = {
|
||||
channel: "##{params['channel_name']}",
|
||||
text: params['text'],
|
||||
attachments: [{
|
||||
title_link: gif['images']['original']['url'],
|
||||
image_url: gif['images']['original']['url'],
|
||||
fallback: 'An amazing random giphy from MemeBot',
|
||||
}]
|
||||
|
||||
}.to_json
|
||||
end
|
||||
end
|
||||
|
||||
def connection
|
||||
Faraday.new(url: "https://hooks.slack.com/services/") do |c|
|
||||
c.use FaradayMiddleware::ParseJson, content_type: 'application/json'
|
||||
c.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
@ -1,7 +0,0 @@
|
||||
class Animations
|
||||
|
||||
def find string
|
||||
"looking for a gif related to: #{string}"
|
||||
end
|
||||
|
||||
end
|
48
app/workers/get_giphy.rb
Normal file
48
app/workers/get_giphy.rb
Normal file
@ -0,0 +1,48 @@
|
||||
require 'faraday'
|
||||
require 'faraday_middleware'
|
||||
|
||||
class GetGiphy
|
||||
|
||||
def random search=""
|
||||
url = "random?api_key=#{api_key}&rating=pg&tag=#{clean_search(search)}"
|
||||
response = connection.get(url)
|
||||
|
||||
response.body['data']
|
||||
end
|
||||
|
||||
def search search=""
|
||||
url = "search?api_key=#{api_key}&rating=pg&q=#{clean_search(search)}&limit=1&offset=#{rand(0..5)}"
|
||||
response = connection.get(url)
|
||||
|
||||
response.body['data']
|
||||
end
|
||||
|
||||
def translate search=""
|
||||
url = "translate?api_key=#{api_key}&rating=pg&tag=#{search}"
|
||||
response = connection.get(url)
|
||||
|
||||
response.body['data']
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_search search
|
||||
search.gsub(/ */, '+')
|
||||
end
|
||||
|
||||
def connection
|
||||
Faraday.new(url: giphy_url) do |c|
|
||||
c.use FaradayMiddleware::ParseJson, content_type: 'application/json'
|
||||
c.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
||||
|
||||
def giphy_url
|
||||
"http://api.giphy.com/v1/gifs/"
|
||||
end
|
||||
|
||||
def api_key
|
||||
"dc6zaTOxFJmzC"
|
||||
end
|
||||
|
||||
end
|
@ -1,15 +1,23 @@
|
||||
class RandomSalutation
|
||||
|
||||
def short
|
||||
'Hi there!'
|
||||
[
|
||||
'Hi there!',
|
||||
'Hello',
|
||||
'Greetings!'
|
||||
].shuffle.first
|
||||
end
|
||||
|
||||
def medium
|
||||
"Well hello! So nice to see you here."
|
||||
[
|
||||
"Well hello! So nice to see you here.",
|
||||
].shuffle.first
|
||||
end
|
||||
|
||||
def long
|
||||
"My, what a wonderful day. Wouldn't you agree? I mean, it's even better now that you are here! Amirite?"
|
||||
[
|
||||
"My, what a wonderful day. Wouldn't you agree? I mean, it's even better now that you are here! Amirite?",
|
||||
].shuffle.first
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -3,11 +3,14 @@ require 'bundler'
|
||||
require 'thin'
|
||||
require 'sinatra'
|
||||
|
||||
require 'awesome_print'
|
||||
|
||||
$LOAD_PATH.unshift( File.join( File.dirname(__FILE__), 'app' ) )
|
||||
|
||||
Bundler.require
|
||||
|
||||
require './meme_bot'
|
||||
require './config'
|
||||
require './app/meme_bot'
|
||||
|
||||
#\ -p 3000
|
||||
run Sinatra::Application
|
||||
|
23
meme_bot.rb
23
meme_bot.rb
@ -1,23 +0,0 @@
|
||||
require 'json'
|
||||
require 'workers/random_salutation'
|
||||
require 'workers/animations'
|
||||
|
||||
|
||||
get '/?' do
|
||||
content_type :json
|
||||
greeting = RandomSalutation.new
|
||||
{ message: greeting.short }.to_json
|
||||
end
|
||||
|
||||
get '/greet/:length/?' do
|
||||
content_type :json
|
||||
greeting = RandomSalutation.new
|
||||
{ message: greeting.send(params[:length]) }.to_json
|
||||
end
|
||||
|
||||
post '/animate/?' do
|
||||
content_type :json
|
||||
gif = Animations.new
|
||||
@json = JSON.parse(request.body.read)
|
||||
{ message: gif.find( @json['search'] ) }.to_json
|
||||
end
|
Loading…
Reference in New Issue
Block a user