Class: Facebook::Messenger::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook/messenger/server.rb

Overview

This module holds the server that processes incoming messages from the Facebook Messenger Platform.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(env) ⇒ Object


9
10
11
# File 'lib/facebook/messenger/server.rb', line 9

def self.call(env)
  new.call(env)
end

Instance Method Details

#call(env) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/facebook/messenger/server.rb', line 13

def call(env)
  @request = Rack::Request.new(env)
  @response = Rack::Response.new

  case
  when @request.get? then verify
  when @request.post? then receive
  else @response.status = 405
  end

  @response.finish
end

#receiveObject


38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/facebook/messenger/server.rb', line 38

def receive
  hash = JSON.parse(@request.body.read)

  # Facebook may batch several items in the 'entry' array during
  # periods of high load.
  hash['entry'].each do |entry|
    # Facebook may batch several items in the 'messaging' array during
    # periods of high load.
    entry['messaging'].each do |messaging|
      Facebook::Messenger::Bot.receive(messaging)
    end
  end
end

#verifyObject


26
27
28
29
30
31
32
# File 'lib/facebook/messenger/server.rb', line 26

def verify
  if @request['hub.verify_token'] == verify_token
    @response.write @request['hub.challenge']
  else
    @response.write 'Error; wrong verify token'
  end
end

#verify_tokenObject


34
35
36
# File 'lib/facebook/messenger/server.rb', line 34

def verify_token
  Facebook::Messenger.config.verify_token
end