Class: Facebook::Interface::Interface

Inherits:
Charyf::Interface::Base
  • Object
show all
Defined in:
lib/facebook/interface/interface.rb

Defined Under Namespace

Classes: InvalidConfiguration

Class Method Summary collapse

Class Method Details

._storageObject



121
122
123
# File 'lib/facebook/interface/interface.rb', line 121

def _storage
  @storage ||= Charyf.application.storage_provider.get_for(self)
end

.initObject



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
# File 'lib/facebook/interface/interface.rb', line 53

def init
  validate
  @initialized = true

  # Facebook::Messenger::Subscriptions.subscribe(access_token: ENV['ACCESS_TOKEN'])
  # TODO - temporal error with FB platform - old API is not working
  HTTParty.post 'https://graph.facebook.com/v2.9/me/subscribed_apps', query: { access_token: ENV["ACCESS_TOKEN"] }


  Facebook::Messenger::Bot.on :message do |message|

    # message.id          # => 'mid.1457764197618:41d102a3e1ae206a38'
    # message.sender      # => { 'id' => '1008372609250235' }
    # message.seq         # => 73
    # message.sent_at     # => 2016-04-22 21:30:36 +0200
    # message.text        # => 'Hello, bot!'
    # message.attachments # => [ { 'type' => 'image', 'payload' => { 'url' => 'https://www.example.com/1.jpg' } } ]

    # message.reply(text: "Hello, i was still not configured to reply to your messages. I will keep responding with this meaningless bullshit " +
    # "untill you find some time to finally finish me.")

    # TODO errors in this thread are lost :/
    Thread.new do
      # Mark the message as seen once bot gets the message
      message.mark_seen

      sender = message.sender['id']
      request = Charyf::Engine::Request.new(self, sender, message.id)

      request.text = message.text

      # TODO support more formats
      unless message.text && !message.text.empty?
        message.reply(text: 'Only text is supported in this version.')
      end

      return if repost?(message)

      self.dispatcher.dispatch_async(request)
    end.abort_on_exception = true
  end

end

.reply(conversation_id, message_id, response) ⇒ Object



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

def reply(conversation_id, message_id, response)
  Facebook::Messenger::Bot.deliver({
                                       recipient: {
                                           id: conversation_id
                                       },
                                       message: {
                                           text: response.text
                                       },
                                       message_type: 'RESPONSE'.freeze
                                   }, access_token: ENV['ACCESS_TOKEN'])
end

.repost?(message) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/facebook/interface/interface.rb', line 105

def repost?(message)
  messages = _storage.get(message.sender['id']) || []

  repost = messages.include? message.id

  messages.push message.id

  if messages.size > 10
    messages.delete_at(0)
  end

  _storage.store(message.sender['id'], messages)

  repost
end

.startObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/facebook/interface/interface.rb', line 26

def start
  init unless @initialized
  return false if @thread && @thread.alive?

  @thread = Thread.new do
    Rack::Handler::WEBrick.run(
        Facebook::Messenger::Server,
        Port: Facebook::Interface.config.port, Host: Facebook::Interface.config.host
    )
  end
  @thread.abort_on_exception = true

  # Currently this call blocks to check if the interface starts
  sleep 1
  @thread.join unless @thread.alive?
end

.stopObject



43
44
45
# File 'lib/facebook/interface/interface.rb', line 43

def stop
  @thread.kill if @thread
end

.terminateObject

If stop does not finish till required timeout Terminate is called



49
50
51
# File 'lib/facebook/interface/interface.rb', line 49

def terminate
  @thread.terminate if @thread
end

.validateObject



97
98
99
100
101
102
103
# File 'lib/facebook/interface/interface.rb', line 97

def validate
  unless ENV['ACCESS_TOKEN'] && ENV['APP_SECRET'] && ENV['VERIFY_TOKEN']
    raise InvalidConfiguration.new("ENV variables are not properly configured. Ensure that ENV['ACCESS_TOKEN'], " +
                                       "ENV['APP_SECRET'] and ENV['VERIFY_TOKEN'] are set."
    )
  end
end