Class: Facebook::Interface::Interface
- Inherits:
-
Charyf::Interface::Base
- Object
- Charyf::Interface::Base
- Facebook::Interface::Interface
show all
- Defined in:
- lib/facebook/interface/interface.rb
Defined Under Namespace
Classes: InvalidConfiguration
Class Method Summary
collapse
Class Method Details
._storage ⇒ Object
121
122
123
|
# File 'lib/facebook/interface/interface.rb', line 121
def _storage
@storage ||= Charyf.application.storage_provider.get_for(self)
end
|
.init ⇒ Object
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
HTTParty.post 'https://graph.facebook.com/v2.9/me/subscribed_apps', query: { access_token: ENV["ACCESS_TOKEN"] }
Facebook::Messenger::Bot.on :message do |message|
Thread.new do
message.mark_seen
sender = message.sender['id']
request = Charyf::Engine::Request.new(self, sender, message.id)
request.text = message.text
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
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
|
.start ⇒ Object
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
sleep 1
@thread.join unless @thread.alive?
end
|
.stop ⇒ Object
43
44
45
|
# File 'lib/facebook/interface/interface.rb', line 43
def stop
@thread.kill if @thread
end
|
.terminate ⇒ Object
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
|
.validate ⇒ Object
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
|