Class: Sinbotra::Messenger::Bot
Defined Under Namespace
Classes: Conversation, Listener
Constant Summary
collapse
- GET_STARTED_ID =
"GET_STARTED"
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(message) ⇒ Bot
Returns a new instance of Bot.
48
49
50
51
52
53
54
|
# File 'lib/sinbotra/messenger/bot.rb', line 48
def initialize(message)
@current_user = UserPresenter.new(message.sender)
@message = MessagePresenter.new(message)
@platform = Platform.new(@current_user.id)
@phrases = setup_phrases
log_message!
end
|
Class Attribute Details
._conversations ⇒ Object
Returns the value of attribute _conversations.
6
7
8
|
# File 'lib/sinbotra/messenger/bot.rb', line 6
def _conversations
@_conversations
end
|
._default_listener ⇒ Object
Returns the value of attribute _default_listener.
6
7
8
|
# File 'lib/sinbotra/messenger/bot.rb', line 6
def _default_listener
@_default_listener
end
|
._dont_understand_phrases ⇒ Object
Returns the value of attribute _dont_understand_phrases.
6
7
8
|
# File 'lib/sinbotra/messenger/bot.rb', line 6
def _dont_understand_phrases
@_dont_understand_phrases
end
|
._listeners ⇒ Object
Returns the value of attribute _listeners.
6
7
8
|
# File 'lib/sinbotra/messenger/bot.rb', line 6
def _listeners
@_listeners
end
|
.get_started_method ⇒ Object
Returns the value of attribute get_started_method.
6
7
8
|
# File 'lib/sinbotra/messenger/bot.rb', line 6
def get_started_method
@get_started_method
end
|
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
36
37
38
|
# File 'lib/sinbotra/messenger/bot.rb', line 36
def current_user
@current_user
end
|
#get_started_method ⇒ Object
Returns the value of attribute get_started_method.
39
40
41
|
# File 'lib/sinbotra/messenger/bot.rb', line 39
def get_started_method
@get_started_method
end
|
#message ⇒ Object
Returns the value of attribute message.
37
38
39
|
# File 'lib/sinbotra/messenger/bot.rb', line 37
def message
@message
end
|
Returns the value of attribute platform.
38
39
40
|
# File 'lib/sinbotra/messenger/bot.rb', line 38
def platform
@platform
end
|
Class Method Details
.conversations(conversation_map) ⇒ Object
24
25
26
27
28
29
|
# File 'lib/sinbotra/messenger/bot.rb', line 24
def conversations(conversation_map)
c = conversation_map.each_with_object({}) { |(id, klass), h|
h[id] = Conversation.new(id, klass)
}
@_conversations = c
end
|
.default_listener(listener) ⇒ Object
20
21
22
|
# File 'lib/sinbotra/messenger/bot.rb', line 20
def default_listener(listener)
@_default_listener = listener
end
|
.dont_understand_phrases(phrases) ⇒ Object
31
32
33
|
# File 'lib/sinbotra/messenger/bot.rb', line 31
def dont_understand_phrases(phrases)
@_dont_understand_phrases = phrases
end
|
.get_started(method_name) ⇒ Object
11
12
13
14
|
# File 'lib/sinbotra/messenger/bot.rb', line 11
def get_started(method_name)
@get_started_method = method_name
Sinbotra::Messenger::Platform.get_started(GET_STARTED_ID)
end
|
.listeners(methods) ⇒ Object
16
17
18
|
# File 'lib/sinbotra/messenger/bot.rb', line 16
def listeners(methods)
@_listeners = methods.map { |name, matcher| Listener.new(matcher, name) }
end
|
Instance Method Details
#conversations ⇒ Object
43
|
# File 'lib/sinbotra/messenger/bot.rb', line 43
def conversations; self.class._conversations; end
|
#default_listener ⇒ Object
42
|
# File 'lib/sinbotra/messenger/bot.rb', line 42
def default_listener; self.class._default_listener; end
|
#get_intent(msg) ⇒ Object
110
111
112
|
# File 'lib/sinbotra/messenger/bot.rb', line 110
def get_intent(msg)
end
|
#get_started_message? ⇒ Boolean
92
93
94
95
|
# File 'lib/sinbotra/messenger/bot.rb', line 92
def get_started_message?
pb = message.postback
pb.to_s == self.class::GET_STARTED_ID
end
|
#handle_default! ⇒ Object
101
102
103
|
# File 'lib/sinbotra/messenger/bot.rb', line 101
def handle_default!
send(default_listener, message)
end
|
#handle_get_started! ⇒ Object
97
98
99
|
# File 'lib/sinbotra/messenger/bot.rb', line 97
def handle_get_started!
send(get_started_method, message)
end
|
#has_conversation?(intent) ⇒ Boolean
105
106
107
108
|
# File 'lib/sinbotra/messenger/bot.rb', line 105
def has_conversation?(intent)
false
end
|
#in_conversation? ⇒ Boolean
61
|
# File 'lib/sinbotra/messenger/bot.rb', line 61
def in_conversation?; current_user.in_conversation?; end
|
#listeners ⇒ Object
41
|
# File 'lib/sinbotra/messenger/bot.rb', line 41
def listeners; self.class._listeners; end
|
#make_conversation(convo_id) ⇒ Object
70
71
72
73
|
# File 'lib/sinbotra/messenger/bot.rb', line 70
def make_conversation(convo_id)
convo = conversations[convo_id]
convo.klass.new(self, platform)
end
|
#next_dont_understand_phrase ⇒ Object
114
115
116
117
118
119
120
|
# File 'lib/sinbotra/messenger/bot.rb', line 114
def next_dont_understand_phrase
return "I don't understand" unless @phrases.any?
@phrase_index ||= 0
phrase = @phrases[@phrase_index % @phrases.size]
@phrase_index += 1
phrase
end
|
#respond ⇒ Object
56
57
58
59
|
# File 'lib/sinbotra/messenger/bot.rb', line 56
def respond
$logger.debug("Responding...")
in_conversation? ? send_to_conversation! : send_to_listeners!
end
|
#send_to_conversation! ⇒ Object
75
76
77
78
79
80
|
# File 'lib/sinbotra/messenger/bot.rb', line 75
def send_to_conversation!
$logger.debug("Send message to conversation")
id = current_user.conversation.id
convo = make_conversation(id)
convo.continue_dialogue
end
|
#send_to_listeners! ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/sinbotra/messenger/bot.rb', line 82
def send_to_listeners!
$logger.debug("Send message through handler stack")
listener = listeners.find { |l| matches_message?(l) }
if listener.nil?
get_started_message? ? handle_get_started! : handle_default!
else
send(listener.method_name, message)
end
end
|
#start_conversation(convo_id) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/sinbotra/messenger/bot.rb', line 63
def start_conversation(convo_id)
current_user.start_conversation(convo_id)
convo = make_conversation(convo_id)
$logger.debug("Start Conversation: #{convo}")
convo.start
end
|