-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgifbot.rb
34 lines (31 loc) · 1023 Bytes
/
gifbot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ruby
require 'bundler/setup'
require 'json'
require 'net/http'
require 'sinatra'
SLACK_TOKEN="..."
GIPHY_KEY="dc6zaTOxFJmzC"
TRIGGER_WORD="#"
IMAGE_STYLE="fixed_height" # or "fixed_width" or "original"
post "/gif" do
return 401 unless request["token"] == SLACK_TOKEN
q = request["text"]
return 200 unless q.start_with? TRIGGER_WORD
q = URI::encode q[TRIGGER_WORD.size..-1]
url = "http://api.giphy.com/v1/gifs/search?q=#{q}&api_key=#{GIPHY_KEY}&limit=50"
# $stderr.puts "querying giphy: #{url}"
resp = Net::HTTP.get_response(URI.parse(url))
buffer = resp.body
result = JSON.parse(buffer)
images = result["data"].map {|item| item["images"]}
# filter out images > 2MB(?) because Slack
images.select! {|image| image["original"]["size"].to_i < 1<<21}
if images.empty?
text = ":cry:"
else
selected = images[rand images.size]
text = "<" + selected[IMAGE_STYLE]["url"] + ">"
end
reply = {username: "giphy", icon_emoji: ":monkey_face:", text: text}
return JSON.generate(reply)
end