-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.rb
54 lines (40 loc) · 1.19 KB
/
server.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
require 'sinatra'
require 'json'
require 'httparty'
require 'pp'
require './api_ai'
#Bound to this address so that external hosts can access it, VERY IMPORTANT!
set :bind, '0.0.0.0'
set :logging, true
URL = "https://graph.facebook.com/v2.6/me/messages?access_token=#{ENV["PAGE_ACCESS_TOKEN"]}"
post '/page_webhook' do
body = request.body.read
payload = JSON.parse(body)
# get the sender of the message
sender = payload["entry"].first["messaging"].first["sender"]["id"]
# get the message text
message = payload["entry"].first["messaging"].first["message"]
message = message["text"] unless message.nil?
pp message
# ask Api.ai NLP api if it isn't a confirmation message from Facebook messenger API
unless message.nil?
@result = HTTParty.post(URL,
:body => { :recipient => { :id => sender},
:message => { :text => ApiAi.chat(message)}
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
end
end
get '/page_webhook' do
params['hub.challenge'] if ENV["VERIFY_TOKEN"] == params['hub.verify_token']
end
get '/' do
html = <<-HTML
<html>
<body>
Hello robots OverLords!
</body>
</html>
HTML
html
end