-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
executable file
·113 lines (97 loc) · 2.49 KB
/
app.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require 'sinatra'
require 'sinatra/cross_origin'
require 'json'
require 'jwt'
require 'data_mapper'
require 'giphy'
require_relative 'launcher.rb'
$FLIF_APP_ID = ENV['FLIF_APP_ID']
# Setting some constraints
set :public_folder, 'public'
#set :protection, :except => [:http_origin]
#use Rack::Protection::HttpOrigin, :origin_whitelist => ['https://web.flock.co']
set :protection, :except => :frame_options
# Confiuring Corssing Origin and Datamapper
configure do
enable :cross_origin
DataMapper.setup :default ,"sqlite://#{Dir.pwd}/database.db"
end
class User
include DataMapper::Resource
property :id, Serial
property :userId, String
property :token, String
end
DataMapper.finalize.auto_upgrade!
def decode_token token
JWT.decode(token,nil,false).first["appId"]
end
def verified? decoded_token
$FLIF_APP_ID == decoded_token
end
def parse_body body
JSON.parse body.read
end
def respond_to_slash_command response
puts "Name is #{response["userName"]}"
puts "userId is #{response["chat"]}"
puts response
end
get '/' do
"Hello World"
end
post '/events' do
response = parse_body request.body
puts response
case response["name"]
when "app.install"
@user = User.new(userId: response["userId"],token: response["token"])
@user.save!
when "app.uninstall"
User.first(userId: response["userId"]).destroy
else
puts "Unknown event #{response["name"]} : #{response}"
end
status 200
end
get '/events' do
puts params
status 200
"Events"
end
get '/test' do
puts request.env['rack.request.query_hash']
unless params['flockEventToken'] and params['flockEvent']
"Flock Exclusive App Only"
halt 200
end
if verified?(decode_token(params["flockEventToken"]))
puts "Verified Token"
@flockEvent = JSON.parse params['flockEvent']
search_text = @flockEvent['text'].strip
if search_text.empty?
# If no text string passed, show top 25 from trending list
@gifs = Giphy.trending(limit: 25)
else
@gifs = Giphy.search(search_text,{limit: 25})
end
# File.read(File.join('public', 'flock.html'))
@userId = "35"
erb :index
else
@error = "Sorry this app works with Flock Only"
end
end
post '/send' do
payload = parse_body request.body
puts payload
# byebug
token = User.first(userId: payload['data']['user_id']).token
puts token
if token.nil?
{"error" => "Not such user", "status" => "failure"}.to_json
else
send_attachments payload['data']["gif_list"],payload["data"]["chat_id"],token
{"status" => "success"}.to_json
end
end