kogno 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/kogno +92 -0
- data/lib/boot.rb +9 -0
- data/lib/core/bin_helpers/messenger_ctl.rb +48 -0
- data/lib/core/bin_helpers/scaffolding.rb +203 -0
- data/lib/core/bin_helpers/scheduled_messages_ctl.rb +95 -0
- data/lib/core/bin_helpers/sequences_ctl.rb +95 -0
- data/lib/core/bin_helpers/server_ctl.rb +127 -0
- data/lib/core/bin_helpers/telegram_ctl.rb +48 -0
- data/lib/core/bin_helpers/webhook_ctl.rb +96 -0
- data/lib/core/db.rb +8 -0
- data/lib/core/extensions/array.rb +28 -0
- data/lib/core/extensions/hash.rb +12 -0
- data/lib/core/extensions/logger.rb +70 -0
- data/lib/core/extensions/string.rb +6 -0
- data/lib/core/extensions/wit.rb +60 -0
- data/lib/core/global_methods.rb +67 -0
- data/lib/core/helpers/string.rb +105 -0
- data/lib/core/lib/base_config.rb +17 -0
- data/lib/core/lib/block_params.rb +4 -0
- data/lib/core/lib/context.rb +1573 -0
- data/lib/core/lib/error_handler.rb +36 -0
- data/lib/core/lib/message.rb +182 -0
- data/lib/core/lib/messenger/api.rb +281 -0
- data/lib/core/lib/messenger/facebook_graph.rb +32 -0
- data/lib/core/lib/messenger/message.rb +202 -0
- data/lib/core/lib/messenger/notification.rb +351 -0
- data/lib/core/lib/messenger/post_comment.rb +104 -0
- data/lib/core/lib/messenger/recurring_notification.rb +81 -0
- data/lib/core/lib/nlp.rb +191 -0
- data/lib/core/lib/notification.rb +371 -0
- data/lib/core/lib/spelling.rb +13 -0
- data/lib/core/lib/telegram/api.rb +197 -0
- data/lib/core/lib/telegram/chat_activity.rb +111 -0
- data/lib/core/lib/telegram/inline_query.rb +112 -0
- data/lib/core/lib/telegram/message.rb +327 -0
- data/lib/core/lib/telegram/notification.rb +507 -0
- data/lib/core/lib/whatsapp/api.rb +153 -0
- data/lib/core/lib/whatsapp/message.rb +132 -0
- data/lib/core/lib/whatsapp/notification.rb +206 -0
- data/lib/core/lib/whatsapp/status_message.rb +58 -0
- data/lib/core/loaders/config_files.rb +15 -0
- data/lib/core/models/chat_log.rb +4 -0
- data/lib/core/models/long_payload.rb +25 -0
- data/lib/core/models/matched_message.rb +5 -0
- data/lib/core/models/messenger_recurring_notification.rb +16 -0
- data/lib/core/models/scheduled_message.rb +40 -0
- data/lib/core/models/sequence.rb +29 -0
- data/lib/core/models/telegram_chat_group.rb +26 -0
- data/lib/core/models/user.rb +285 -0
- data/lib/core/web/webhook.rb +198 -0
- data/lib/kogno.rb +130 -0
- data/scaffolding/new_project/Gemfile +3 -0
- data/scaffolding/new_project/application.rb +5 -0
- data/scaffolding/new_project/bot/contexts/main_context.rb +10 -0
- data/scaffolding/new_project/bot/conversation.rb +14 -0
- data/scaffolding/new_project/bot/models/user.rb +3 -0
- data/scaffolding/new_project/config/application.rb +28 -0
- data/scaffolding/new_project/config/database.yml +8 -0
- data/scaffolding/new_project/config/locales/en.yml +4 -0
- data/scaffolding/new_project/config/locales/es.yml +3 -0
- data/scaffolding/new_project/config/nlp.rb +23 -0
- data/scaffolding/new_project/config/platforms/messenger.rb +74 -0
- data/scaffolding/new_project/config/platforms/telegram.rb +45 -0
- data/scaffolding/new_project/config/platforms/whatsapp.rb +13 -0
- data/scaffolding/new_project/web/routes.rb +10 -0
- metadata +220 -0
@@ -0,0 +1,285 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
|
3
|
+
has_many :sequences
|
4
|
+
has_many :chat_logs
|
5
|
+
has_many :scheduled_messages
|
6
|
+
has_many :matched_messages
|
7
|
+
has_many :actions, foreign_key: :user_id, :class_name => "UserAction"
|
8
|
+
has_many :telegram_groups, foreign_key: :inviter_user_id, :class_name => "TelegramChatGroup"
|
9
|
+
has_one :messenger_recurring_notification
|
10
|
+
|
11
|
+
|
12
|
+
attr_accessor :just_created, :vars
|
13
|
+
before_create :set_default_for_session_vars
|
14
|
+
|
15
|
+
def set_default_for_session_vars
|
16
|
+
self.session_vars = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def type
|
20
|
+
:user
|
21
|
+
end
|
22
|
+
|
23
|
+
def chat_id
|
24
|
+
self.psid
|
25
|
+
end
|
26
|
+
|
27
|
+
def fetch_profile_information
|
28
|
+
Kogno::FacebookGraph.fetch_user_data(self.psid, self.page_id)
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def reset
|
33
|
+
self.exit_context
|
34
|
+
self.save
|
35
|
+
end
|
36
|
+
|
37
|
+
def mark_last_message_as_read
|
38
|
+
self.last_message_read = true
|
39
|
+
self.save
|
40
|
+
end
|
41
|
+
|
42
|
+
def mark_last_message_as_unread
|
43
|
+
self.last_message_read = false
|
44
|
+
self.save
|
45
|
+
end
|
46
|
+
|
47
|
+
def set_context(context, params={})
|
48
|
+
|
49
|
+
self.context = context
|
50
|
+
self.context_params = params.to_json.encode('utf-8')
|
51
|
+
self.save
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_locale(locale)
|
56
|
+
self.locale = locale
|
57
|
+
I18n.locale = locale
|
58
|
+
self.save
|
59
|
+
end
|
60
|
+
|
61
|
+
def get_context_params
|
62
|
+
(JSON.parse(self.context_params,{:symbolize_names => true}) rescue {})
|
63
|
+
end
|
64
|
+
|
65
|
+
def add_context(context)
|
66
|
+
context_a = self.context.to_s.split(Regexp.union(["/","."]))
|
67
|
+
context_a.push(context)
|
68
|
+
self.context = context_a.join(".")
|
69
|
+
self.save
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
def exit_context
|
74
|
+
self.set_context(nil) if !self.context.nil?
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
def self.find_or_create_by_psid(psid,platform,page_id=nil,psid_from_post_comment=nil)
|
79
|
+
user = User.find_by_psid(psid)
|
80
|
+
if user.nil?
|
81
|
+
begin
|
82
|
+
new_user = User.create(psid: psid, platform: platform, page_id: page_id, last_usage_at: Time.now.utc, psid_from_post_comment: psid_from_post_comment)
|
83
|
+
user = User.find(new_user.id)
|
84
|
+
user.just_created = true
|
85
|
+
rescue ActiveRecord::RecordNotUnique => e
|
86
|
+
user = User.find_by_psid(psid)
|
87
|
+
user.just_created = false
|
88
|
+
end
|
89
|
+
else
|
90
|
+
user.just_created = false
|
91
|
+
if user.psid_from_post_comment.nil? and !psid_from_post_comment.nil?
|
92
|
+
user.psid_from_post_comment = psid_from_post_comment
|
93
|
+
user.save
|
94
|
+
end
|
95
|
+
end
|
96
|
+
return user
|
97
|
+
end
|
98
|
+
|
99
|
+
def set_last_usage
|
100
|
+
self.last_usage_at = Time.now.utc
|
101
|
+
end
|
102
|
+
|
103
|
+
def last_usage
|
104
|
+
Time.now.utc - self.last_usage_at
|
105
|
+
end
|
106
|
+
|
107
|
+
def dummy?
|
108
|
+
self.id.nil?
|
109
|
+
end
|
110
|
+
|
111
|
+
def first_time?
|
112
|
+
self.just_created.nil? ? false : self.just_created
|
113
|
+
end
|
114
|
+
|
115
|
+
#Sequences
|
116
|
+
|
117
|
+
def set_sequence(stage, context)
|
118
|
+
sequence = sequences.find_by_context(context)
|
119
|
+
if sequence.nil?
|
120
|
+
sequence = sequences.create(stage:stage, context: context)
|
121
|
+
else
|
122
|
+
if stage != sequence.stage
|
123
|
+
sequence.stage = stage
|
124
|
+
sequence.last_hit_at = Time.now.utc
|
125
|
+
sequence.last_executed = 0
|
126
|
+
sequence.execution_time = nil
|
127
|
+
else
|
128
|
+
sequence.updated_at = Time.now.utc
|
129
|
+
end
|
130
|
+
sequence.save
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def exit_sequence(stage, context)
|
135
|
+
sequence = self.sequences.where("stage = '#{stage}' and context = '#{context}'").first
|
136
|
+
unless sequence.nil?
|
137
|
+
sequence.destroy
|
138
|
+
true
|
139
|
+
else
|
140
|
+
false
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Session Vars
|
145
|
+
|
146
|
+
def save_session_vars
|
147
|
+
self.last_usage_at = Time.now.utc
|
148
|
+
new_session_vars = self.vars.to_json.encode('utf-8')
|
149
|
+
if self.session_vars != new_session_vars
|
150
|
+
self.session_vars = new_session_vars
|
151
|
+
end
|
152
|
+
self.save
|
153
|
+
end
|
154
|
+
|
155
|
+
def get_session_vars
|
156
|
+
|
157
|
+
self.vars = (JSON.parse(self.session_vars,{:symbolize_names => true}) rescue {})
|
158
|
+
|
159
|
+
end
|
160
|
+
|
161
|
+
def save_current_context
|
162
|
+
self.get_session_vars if self.vars.nil?
|
163
|
+
unless self.context.to_s.empty?
|
164
|
+
self.vars[:saved_context] = {
|
165
|
+
context: self.context.to_s,
|
166
|
+
params: self.context_params
|
167
|
+
}
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def previous_context
|
172
|
+
self.get_session_vars if self.vars.nil?
|
173
|
+
self.vars[:previous_context]
|
174
|
+
end
|
175
|
+
|
176
|
+
def delete_previous_context
|
177
|
+
self.get_session_vars if self.vars.nil?
|
178
|
+
self.vars.delete :previous_context
|
179
|
+
end
|
180
|
+
|
181
|
+
def log_message(message, message_type = :received)
|
182
|
+
if message_type == :post_comment_received
|
183
|
+
self.chat_logs.create({
|
184
|
+
message_type: message_type,
|
185
|
+
body: message.webhook_data.to_json,
|
186
|
+
context: self.context,
|
187
|
+
message: message.text.to_s[0..1000],
|
188
|
+
nlp_entities: (message.nlp.entities.to_json rescue {}),
|
189
|
+
new_user: self.first_time?
|
190
|
+
})
|
191
|
+
else
|
192
|
+
self.chat_logs.create({
|
193
|
+
message_type: message_type,
|
194
|
+
body: message.webhook_data.to_json,
|
195
|
+
context: self.context,
|
196
|
+
payload: message.payload,
|
197
|
+
payload_params: message.params,
|
198
|
+
message: message.text.to_s[0..1000],
|
199
|
+
nlp_entities: (message.nlp.entities.to_json rescue {}),
|
200
|
+
user_vars: self.session_vars,
|
201
|
+
new_user: self.first_time?
|
202
|
+
})
|
203
|
+
end
|
204
|
+
|
205
|
+
end
|
206
|
+
|
207
|
+
def log_response(response,scheduled=false)
|
208
|
+
unless response.message_log.empty?
|
209
|
+
response_log = self.chat_logs.create({
|
210
|
+
message_type: :sent,
|
211
|
+
body: response.message_log.to_json,
|
212
|
+
response: response.response_log.to_json,
|
213
|
+
context: self.context,
|
214
|
+
user_vars: self.vars.to_json,
|
215
|
+
scheduled: scheduled
|
216
|
+
})
|
217
|
+
response.destroy_message_log
|
218
|
+
return(response_log)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def destroy_scheduled_messages(tag=nil)
|
223
|
+
if tag.nil?
|
224
|
+
self.scheduled_messages.destroy_all
|
225
|
+
else
|
226
|
+
self.scheduled_messages.where(tag: tag).destroy_all
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def scheduled_message?(tag)
|
231
|
+
if self.scheduled_messages.where(tag: tag).empty?
|
232
|
+
return false
|
233
|
+
else
|
234
|
+
return true
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
def reschedule_message(tag, send_at)
|
239
|
+
self.scheduled_messages.where(tag: tag).update_all(send_at: send_at)
|
240
|
+
end
|
241
|
+
|
242
|
+
def notification
|
243
|
+
if @notification.nil?
|
244
|
+
self.vars = {}
|
245
|
+
case self.platform.to_sym
|
246
|
+
when :messenger
|
247
|
+
@notification = Kogno::Messenger::Notification.new(self)
|
248
|
+
when :telegram
|
249
|
+
@notification = Kogno::Telegram::Notification.new(self)
|
250
|
+
when :whatsapp
|
251
|
+
@notification = Kogno::WhatsApp::Notification.new(self)
|
252
|
+
end
|
253
|
+
end
|
254
|
+
@reply = @notification
|
255
|
+
@user = self
|
256
|
+
return @notification
|
257
|
+
end
|
258
|
+
|
259
|
+
def update_messenger_recurring_notification(data)
|
260
|
+
return false unless self.platform == "messenger"
|
261
|
+
params = {
|
262
|
+
token: data[:notification_messages_token],
|
263
|
+
frecuency: data[:notification_messages_frequency],
|
264
|
+
expires_at: Time.at(data[:token_expiry_timestamp]/1000),
|
265
|
+
token_status: data[:user_token_status],
|
266
|
+
timezone: data[:notification_messages_timezone],
|
267
|
+
active: data[:notification_messages_status] == "STOP_NOTIFICATIONS" ? false : true
|
268
|
+
}
|
269
|
+
if self.messenger_recurring_notification.nil?
|
270
|
+
self.messenger_recurring_notification = MessengerRecurringNotification.create(params)
|
271
|
+
else
|
272
|
+
self.messenger_recurring_notification.update(params)
|
273
|
+
end
|
274
|
+
self.messenger_recurring_notification
|
275
|
+
end
|
276
|
+
|
277
|
+
def messenger_recurring_notification_data
|
278
|
+
self.messenger_recurring_notification.data rescue {}
|
279
|
+
end
|
280
|
+
|
281
|
+
def subscribed_to_messenger_recurring_notification?
|
282
|
+
messenger_recurring_notification_data[:status] == :active ? true : false
|
283
|
+
end
|
284
|
+
|
285
|
+
end
|
@@ -0,0 +1,198 @@
|
|
1
|
+
|
2
|
+
require 'kogno'
|
3
|
+
require 'core/loaders/config_files'
|
4
|
+
|
5
|
+
require 'sinatra'
|
6
|
+
require 'sinatra/namespace'
|
7
|
+
require 'sinatra/cross_origin'
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
Logger.set(:webhook) if ARGV[0] == "daemon"
|
11
|
+
|
12
|
+
require File.join(Kogno::Application.project_path,'application.rb')
|
13
|
+
|
14
|
+
set :port, Kogno::Application.config.http_port || 1337
|
15
|
+
set :bind, '0.0.0.0'
|
16
|
+
set :root, File.join(Kogno::Application.project_path,'web')
|
17
|
+
set :public_folder, File.join(Kogno::Application.project_path,'web','public')
|
18
|
+
set :environment, Kogno::Application.config.environment
|
19
|
+
set :logging, false
|
20
|
+
|
21
|
+
configure do
|
22
|
+
enable :cross_origin
|
23
|
+
end
|
24
|
+
|
25
|
+
before do
|
26
|
+
reload!(false) if Kogno::Application.config.environment == :development
|
27
|
+
end
|
28
|
+
|
29
|
+
# before '/*' do
|
30
|
+
# allowed_paths = [
|
31
|
+
# Kogno::Application.config.messenger.webhook_route,
|
32
|
+
# Kogno::Application.config.telegram.webhook_route,
|
33
|
+
# Kogno::Application.config.whatsapp.webhook_route ,
|
34
|
+
# "/"
|
35
|
+
# ]
|
36
|
+
# unless allowed_paths.include?(request.path_info)
|
37
|
+
# $logger.debug_json request.path_info, :red
|
38
|
+
# end
|
39
|
+
|
40
|
+
get "/" do
|
41
|
+
Kogno::Application.config.app_name
|
42
|
+
end
|
43
|
+
|
44
|
+
post Kogno::Application.config.messenger.webhook_route do
|
45
|
+
|
46
|
+
request.body.rewind
|
47
|
+
params = JSON.parse(request.body.read, {:symbolize_names => true})
|
48
|
+
$logger.write "MESSENGER INCOMING WEBHOOK: #{request.fullpath}", :bright
|
49
|
+
$logger.debug "PARAMS: #{params}"
|
50
|
+
if params[:object] == "page"
|
51
|
+
params[:entry].each do |entry|
|
52
|
+
events = []
|
53
|
+
event_type = nil
|
54
|
+
if !entry[:messaging].nil?
|
55
|
+
events = entry[:messaging]
|
56
|
+
event_type = :message
|
57
|
+
elsif !entry[:changes].nil?
|
58
|
+
events = entry[:changes]
|
59
|
+
event_type = :post_comment
|
60
|
+
end
|
61
|
+
|
62
|
+
unless event_type.nil?
|
63
|
+
events.each do |event|
|
64
|
+
msg = nil
|
65
|
+
if event_type == :message
|
66
|
+
unless event[:optin].nil?
|
67
|
+
msg = Kogno::Messenger::RecurringNotification.new(event)
|
68
|
+
else
|
69
|
+
msg = Kogno::Messenger::Message.new(event)
|
70
|
+
end
|
71
|
+
elsif event_type == :post_comment
|
72
|
+
msg = Kogno::Messenger::PostComment.new(event,entry[:id])
|
73
|
+
end
|
74
|
+
unless msg.nil?
|
75
|
+
Thread.new {
|
76
|
+
begin
|
77
|
+
msg.handle_event()
|
78
|
+
rescue StandardError => e
|
79
|
+
$logger.write e.message, :red
|
80
|
+
$logger.write "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
|
81
|
+
end
|
82
|
+
}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
status 200
|
88
|
+
"EVENT_RECEIVED"
|
89
|
+
else
|
90
|
+
status 404
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
get Kogno::Application.config.messenger.webhook_route do
|
96
|
+
|
97
|
+
mode = params["hub.mode"]
|
98
|
+
token = params["hub.verify_token"]
|
99
|
+
challenge = params["hub.challenge"]
|
100
|
+
if !mode.nil? and !token.nil?
|
101
|
+
if mode == 'subscribe' and token == Kogno::Application.messenger.webhook_verify_token
|
102
|
+
status 200
|
103
|
+
challenge
|
104
|
+
else
|
105
|
+
status 403
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
post Kogno::Application.config.telegram.webhook_route do
|
112
|
+
request.body.rewind
|
113
|
+
params = JSON.parse(request.body.read, {:symbolize_names => true})
|
114
|
+
$logger.write "TELEGRAM INCOMING WEBHOOK: #{request.fullpath}", :bright
|
115
|
+
$logger.debug "PARAMS: #{params}"
|
116
|
+
|
117
|
+
message = {data: params[:message], type: :message} if !params[:message].nil?
|
118
|
+
message = {data: params[:callback_query], type: :callback_query} if !params[:callback_query].nil? and message.nil?
|
119
|
+
message = {data: params[:inline_query], type: :inline_query} if !params[:inline_query].nil? and message.nil?
|
120
|
+
message = {data: params[:my_chat_member], type: :chat_activity} if !params[:my_chat_member].nil? and message.nil?
|
121
|
+
|
122
|
+
if !message.nil?
|
123
|
+
msg = nil
|
124
|
+
if [:message, :callback_query].include?(message[:type])
|
125
|
+
msg = Kogno::Telegram::Message.new(message[:data], message[:type])
|
126
|
+
elsif message[:type] == :inline_query
|
127
|
+
msg = Kogno::Telegram::InlineQuery.new(message[:data])
|
128
|
+
elsif message[:type] == :chat_activity
|
129
|
+
msg = Kogno::Telegram::ChatActivity.new(message[:data])
|
130
|
+
end
|
131
|
+
if !msg.nil?
|
132
|
+
Thread.new {
|
133
|
+
begin
|
134
|
+
msg.handle_event()
|
135
|
+
rescue StandardError => e
|
136
|
+
$logger.write e.message, :red
|
137
|
+
$logger.write "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
|
138
|
+
end
|
139
|
+
}
|
140
|
+
status 200
|
141
|
+
"True"
|
142
|
+
else
|
143
|
+
status 404
|
144
|
+
end
|
145
|
+
else
|
146
|
+
status 404
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
post Kogno::Application.config.whatsapp.webhook_route do
|
151
|
+
request.body.rewind
|
152
|
+
params = JSON.parse(request.body.read, {:symbolize_names => true})
|
153
|
+
$logger.write "WHATSAPP INCOMING WEBHOOK: #{request.fullpath}", :bright
|
154
|
+
$logger.debug "PARAMS: #{params}"
|
155
|
+
entries = params[:entry]
|
156
|
+
entries.each do |entry|
|
157
|
+
entry[:changes].each do |change|
|
158
|
+
msg = nil
|
159
|
+
if !change[:value][:messages].nil?
|
160
|
+
msg = Kogno::WhatsApp::Message.new(change[:value], :message)
|
161
|
+
elsif !change[:value][:statuses].nil?
|
162
|
+
msg = Kogno::WhatsApp::StatusMessage.new(change[:value], :message)
|
163
|
+
end
|
164
|
+
unless msg.nil?
|
165
|
+
Thread.new {
|
166
|
+
begin
|
167
|
+
msg.handle_event()
|
168
|
+
rescue StandardError => e
|
169
|
+
$logger.write e.message, :red
|
170
|
+
$logger.write "Backtrace:\n\t#{e.backtrace.join("\n\t")}"
|
171
|
+
end
|
172
|
+
$logger.write "\n"
|
173
|
+
}
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
status 200
|
179
|
+
"EVENT_RECEIVED"
|
180
|
+
end
|
181
|
+
|
182
|
+
get Kogno::Application.config.whatsapp.webhook_route do
|
183
|
+
|
184
|
+
mode = params["hub.mode"]
|
185
|
+
token = params["hub.verify_token"]
|
186
|
+
challenge = params["hub.challenge"]
|
187
|
+
if !mode.nil? and !token.nil?
|
188
|
+
if mode == 'subscribe' and token == Kogno::Application.whatsapp.webhook_verify_token
|
189
|
+
status 200
|
190
|
+
challenge
|
191
|
+
else
|
192
|
+
status 403
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
end
|
197
|
+
|
198
|
+
require File.join(Kogno::Application.project_path,'web','routes.rb')
|
data/lib/kogno.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
$VERBOSE = nil
|
2
|
+
require 'active_record'
|
3
|
+
require 'core/lib/base_config'
|
4
|
+
require 'logger'
|
5
|
+
require 'core/extensions/logger'
|
6
|
+
|
7
|
+
Logger.set(:stdout)
|
8
|
+
|
9
|
+
module Kogno
|
10
|
+
|
11
|
+
require "active_support/configurable"
|
12
|
+
|
13
|
+
class Application
|
14
|
+
|
15
|
+
include ActiveSupport::Configurable
|
16
|
+
|
17
|
+
config_accessor :sequences do
|
18
|
+
Kogno::BaseConfig.new(:time_elapsed_after_last_usage)
|
19
|
+
end
|
20
|
+
|
21
|
+
config_accessor :error_notifier do
|
22
|
+
Kogno::BaseConfig.new(:slack)
|
23
|
+
end
|
24
|
+
|
25
|
+
config_accessor :api do
|
26
|
+
Kogno::BaseConfig.new(:enable, :key)
|
27
|
+
end
|
28
|
+
|
29
|
+
config_accessor :messenger do
|
30
|
+
Kogno::BaseConfig.new(:graph_url, :pages, :webhook_verify_token, :persistent_menu, :welcome_screen_payload, :greeting, :ice_breakers, :stickers, :webhook_route, :whitelisted_domains)
|
31
|
+
end
|
32
|
+
|
33
|
+
config_accessor :whatsapp do
|
34
|
+
Kogno::BaseConfig.new(:graph_url, :phone_number_id, :access_token, :webhook_verify_token)
|
35
|
+
end
|
36
|
+
|
37
|
+
config_accessor :nlp do
|
38
|
+
Kogno::BaseConfig.new(:wit, :enable)
|
39
|
+
end
|
40
|
+
|
41
|
+
config_accessor :telegram do
|
42
|
+
Kogno::BaseConfig.new(:bot_name, :api_url, :token, :webhook_https_server, :webhook_drop_pending_updates, :webhook_route)
|
43
|
+
end
|
44
|
+
|
45
|
+
config_accessor :routes do
|
46
|
+
Kogno::BaseConfig.new(:default, :post_comment, :inline_query, :chat_activity, :commands, :recurring_notification)
|
47
|
+
end
|
48
|
+
|
49
|
+
class << self
|
50
|
+
|
51
|
+
def core_path
|
52
|
+
File.expand_path('../../',__FILE__)
|
53
|
+
end
|
54
|
+
|
55
|
+
def project_path
|
56
|
+
File.expand_path(Dir.pwd)
|
57
|
+
end
|
58
|
+
|
59
|
+
# def create_project_folders
|
60
|
+
# tmp = File.join(project_path,"tmp")
|
61
|
+
# logs = File.join(project_path,"logs")
|
62
|
+
# web_public = File.join(project_path,"web","logs")
|
63
|
+
# Dir.mkdir(tmp,0755) unless File.exist?(tmp)
|
64
|
+
# Dir.mkdir(logs,0755) unless File.exist?(logs)
|
65
|
+
# end
|
66
|
+
|
67
|
+
def load_core
|
68
|
+
ignore_files = Dir[File.join(core_path,'lib','core','web','inc','*.rb')]
|
69
|
+
files_to_load_later = []
|
70
|
+
Dir[File.join(core_path,'lib','core','**','*.rb')].each do |required_file|
|
71
|
+
if !required_file.include?("lib/messenger/") and !required_file.include?("lib/telegram/") and !required_file.include?("lib/whatsapp/")
|
72
|
+
load required_file unless ignore_files.include?(required_file)
|
73
|
+
else
|
74
|
+
files_to_load_later << required_file
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
files_to_load_later.each do |required_file|
|
79
|
+
load required_file unless ignore_files.include?(required_file)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def load_locales
|
84
|
+
I18n.load_path = Dir[File.join(self.project_path,'config','locales','*.yml')]
|
85
|
+
I18n.config.available_locales = self.config.available_locales
|
86
|
+
I18n.default_locale = self.config.default_locale
|
87
|
+
end
|
88
|
+
|
89
|
+
def load_app # Load App's files: templates/*, controllers/*, helpers/* and models/*
|
90
|
+
$context_blocks = {}
|
91
|
+
$context_html_templates = {}
|
92
|
+
$contexts = Dir[File.join(self.project_path,'bot','contexts','*.rb')].map{|c| c.split("/").last.sub("_context.rb","")} rescue []
|
93
|
+
conversation_file = Dir[File.join(self.project_path,'bot','conversation.rb')]
|
94
|
+
app_files = Dir[File.join(self.project_path,'bot','**','*.rb')]
|
95
|
+
notification_template_files = Dir[File.join(self.project_path,'bot','**','*.erb')]
|
96
|
+
html_template_files = Dir[File.join(self.project_path,'bot','**','*.rhtml')]
|
97
|
+
lib_files = Dir[File.join(self.project_path,'lib','**','*.rb')]
|
98
|
+
(conversation_file+app_files+notification_template_files+html_template_files+lib_files).each do |required_file|
|
99
|
+
if required_file.include?("bot/templates/")
|
100
|
+
file_extension = required_file.split(".").last.to_sym
|
101
|
+
context = File.dirname(required_file).split("/").last.to_sym
|
102
|
+
case file_extension
|
103
|
+
when :erb
|
104
|
+
action = File.basename(required_file,".erb").to_sym
|
105
|
+
$context_blocks[context] = {} if $context_blocks[context].nil?
|
106
|
+
# $context_blocks[context][action] = File.read(required_file)
|
107
|
+
$context_blocks[context][action] = Tilt.new(required_file, nil, default_encoding: "utf-8")
|
108
|
+
when :rhtml
|
109
|
+
action = File.basename(required_file,".rhtml").to_sym
|
110
|
+
$context_html_templates[context] = {} if $context_html_templates[context].nil?
|
111
|
+
$context_html_templates[context][action] = Tilt.new(required_file, nil, default_encoding: "utf-8")
|
112
|
+
end
|
113
|
+
else
|
114
|
+
load required_file
|
115
|
+
end
|
116
|
+
end
|
117
|
+
return true
|
118
|
+
end
|
119
|
+
|
120
|
+
def file_path(file)
|
121
|
+
if File.exist?(file)
|
122
|
+
else
|
123
|
+
File.open(file, "w") {|f| f.write("") }
|
124
|
+
end
|
125
|
+
return file
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Conversation < Kogno::Context
|
2
|
+
|
3
|
+
# before_blocks :do_something_before_blocks
|
4
|
+
# after_blocks :do_something_after_blocks
|
5
|
+
|
6
|
+
def do_something_before_blocks
|
7
|
+
# This will be called before the blocks method in the current context will be executed
|
8
|
+
end
|
9
|
+
|
10
|
+
def do_something_after_blocks
|
11
|
+
# This will be called after the blocks method in the current context will be executed
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
Kogno::Application.configure do |config|
|
2
|
+
|
3
|
+
config.app_name = "Kogno"
|
4
|
+
|
5
|
+
config.environment = :development
|
6
|
+
|
7
|
+
config.http_port = 3000
|
8
|
+
|
9
|
+
# Internacionalization (I18n)
|
10
|
+
config.available_locales = [:en]
|
11
|
+
config.default_locale = :en
|
12
|
+
|
13
|
+
# Default context for any arrived message or event
|
14
|
+
config.routes.default = :main
|
15
|
+
|
16
|
+
config.sequences.time_elapsed_after_last_usage = 900 # 15 minutes
|
17
|
+
|
18
|
+
config.store_log_in_database = false
|
19
|
+
|
20
|
+
config.typed_postbacks = false
|
21
|
+
|
22
|
+
config.error_notifier.slack = {
|
23
|
+
enable: false,
|
24
|
+
webhook: "<YOUR SLACK WEBHOOOK HERE>"
|
25
|
+
}
|
26
|
+
|
27
|
+
end
|
28
|
+
|