1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
pub mod data;
pub use csml_interpreter::data::{
ast::{Expr, Flow, InstructionScope},
error_info::ErrorInfo,
warnings::Warnings,
Client, CsmlResult,
};
use serde_json::json;
mod db_connectors;
mod error_messages;
mod encrypt;
mod init;
mod interpreter_actions;
mod send;
mod utils;
use data::*;
use db_connectors::{
bot, conversations::*, init_db, messages::*, state::*, BotVersion, BotVersionCreated,
DbConversation,
};
use init::*;
use interpreter_actions::interpret_step;
use utils::*;
use csml_interpreter::data::{csml_bot::CsmlBot, csml_flow::CsmlFlow, Context, Hold, IndexInfo, Memory};
use std::{collections::HashMap, env, time::SystemTime};
pub fn start_conversation(
request: CsmlRequest,
bot_opt: BotOpt,
) -> Result<serde_json::Map<String, serde_json::Value>, EngineError> {
let now = SystemTime::now();
let formatted_event = format_event(json!(request))?;
let mut db = init_db()?;
let mut bot = bot_opt.search_bot(&mut db);
init_bot(&mut bot)?;
let mut data = init_conversation_info(
get_default_flow(&bot)?.name.to_owned(),
&formatted_event,
&request,
&bot,
db,
)?;
let msgs = vec![request.payload.to_owned()];
add_messages_bulk(&mut data, msgs, 0, "RECEIVE")?;
check_for_hold(&mut data, &bot)?;
let res = interpret_step(&mut data, formatted_event.to_owned(), &bot);
if let Ok(var) = env::var(DEBUG) {
if var == "true" {
let el = now.elapsed()?;
println!("Total time Manager - {}.{}", el.as_secs(), el.as_millis());
}
}
res
}
pub fn get_open_conversation(client: &Client) -> Result<Option<DbConversation>, EngineError> {
let mut db = init_db()?;
get_latest_open(client, &mut db)
}
pub fn create_bot_version(csml_bot: CsmlBot) -> Result<BotVersionCreated, EngineError> {
let mut db = init_db()?;
let bot_id = csml_bot.id.clone();
match validate_bot(csml_bot.clone()) {
CsmlResult {
errors: Some(errors),
..
} => Err(EngineError::Interpreter(format!("{:?}", errors))),
CsmlResult { .. } => {
let version_id = bot::create_bot_version(bot_id, csml_bot, &mut db)?;
let engine_version = env!("CARGO_PKG_VERSION").to_owned();
Ok(BotVersionCreated {
version_id,
engine_version,
})
}
}
}
pub fn get_last_bot_version(bot_id: &str) -> Result<Option<BotVersion>, EngineError> {
let mut db = init_db()?;
bot::get_last_bot_version(bot_id, &mut db)
}
pub fn get_bot_by_version_id(id: &str, bot_id: &str) -> Result<Option<BotVersion>, EngineError> {
let mut db = init_db()?;
bot::get_by_version_id(id, bot_id, &mut db)
}
pub fn get_bot_versions(
bot_id: &str,
limit: Option<i64>,
last_key: Option<String>,
) -> Result<serde_json::Value, EngineError> {
let mut db = init_db()?;
bot::get_bot_versions(bot_id, limit, last_key, &mut db)
}
pub fn delete_bot_version_id(id: &str, bot_id: &str) -> Result<(), EngineError> {
let mut db = init_db()?;
bot::delete_bot_version(bot_id, id, &mut db)
}
pub fn delete_all_bot_versions(bot_id: &str) -> Result<(), EngineError> {
let mut db = init_db()?;
bot::delete_bot_versions(bot_id, &mut db)
}
pub fn get_steps_from_flow(bot: CsmlBot) -> HashMap<String, Vec<String>> {
csml_interpreter::get_steps_from_flow(bot)
}
pub fn validate_bot(bot: CsmlBot) -> CsmlResult {
csml_interpreter::validate_bot(&bot)
}
pub fn user_close_all_conversations(client: Client) -> Result<(), EngineError> {
let mut db = init_db()?;
delete_state_key(&client, "hold", "position", &mut db)?;
close_all_conversations(&client, &mut db)
}
fn check_for_hold(data: &mut ConversationInfo, bot: &CsmlBot) -> Result<(), EngineError> {
match get_state_key(&data.client, "hold", "position", &mut data.db) {
Ok(Some(string)) => {
let hold = serde_json::to_value(string)?;
match hold.get("hash") {
Some(hash_value) => {
let flow_hash = get_current_step_hash(&data, bot)?;
if flow_hash != *hash_value {
data.context.step = "start".to_owned();
return clean_hold_and_restart(data);
}
flow_hash
}
_ => return Ok(()),
};
let index = match serde_json::from_value::<IndexInfo>(hold["index"].clone()) {
Ok(index) => index,
Err(_) => {
delete_state_key(&data.client, "hold", "position", &mut data.db)?;
return Ok(())
}
};
data.context.hold = Some(Hold {
index,
step_vars: hold["step_vars"].clone(),
step_name: data.context.step.to_owned(),
flow_name: data.context.flow.to_owned(),
});
delete_state_key(&data.client, "hold", "position", &mut data.db)?;
}
Ok(None) => (),
Err(_) => (),
};
Ok(())
}