pub fn switch_bot(
    data: &mut ConversationInfo,
    bot: &mut CsmlBot,
    next_bot: SwitchBot,
    bot_opt: &mut BotOpt,
    event: &mut Event,
) -> Result<(), EngineError> {
    // update data info with new bot |ex| client bot_id, create new conversation
    *bot_opt = match next_bot.version_id {
        Some(version_id) => BotOpt::Id {
            version_id,
            bot_id: next_bot.bot_id,
            apps_endpoint: bot.apps_endpoint.take(),
            multibot: bot.multibot.take(),
        },
        None => BotOpt::BotId {
            bot_id: next_bot.bot_id,
            apps_endpoint: bot.apps_endpoint.take(),
            multibot: bot.multibot.take(),
        },
    };

    let mut new_bot = bot_opt.search_bot(&mut data.db)?;
    new_bot.custom_components = bot.custom_components.take();
    new_bot.native_components = bot.native_components.take();

    *bot = new_bot;

    set_bot_ast(bot)?;

    data.context.step = ContextStepInfo::UnknownFlow(next_bot.step);
    data.context.flow = match next_bot.flow {
        Some(flow) => flow,
        None => bot.get_default_flow_name(),
    };

    // update client with the new bot id
    data.client.bot_id = bot.id.to_owned();

    let (flow, step) = match get_flow_by_id(&data.context.flow, &bot.flows) {
        Ok(flow) => (flow, data.context.step.clone()),
        Err(_) => {
            let error_message = format!(
                "flow: [{}] not found in bot: [{}], switching to start@default_flow",
                data.context.flow, bot.name
            );

            let message = Message {
                content_type: "error".to_owned(),
                content: serde_json::json!(error_message.clone()),
            };

            // save message
            data.messages.push(message.clone());
            // send message
            send_msg_to_callback_url(data, vec![message], 0, false);

            // setting default step && flow
            data.context.step = ContextStepInfo::Normal("start".to_owned());
            data.context.flow = bot.get_default_flow_name();

            (
                get_flow_by_id(&bot.default_flow, &bot.flows)?,
                ContextStepInfo::Normal("start".to_owned()),
            )
        }
    };

    // update event to flow trigger
    event.content_type = "flow_trigger".to_owned();
    event.content = serde_json::json!({
            "flow_id": flow.id,
            "step_id": step
        }
    );

    // create new conversation for the new client
    data.conversation_id = create_conversation(
        &flow.id,
        &step.get_step(),
        &data.client,
        data.ttl.clone(),
        &mut data.db,
    )?;

    // and get memories of the new bot form db,
    // clearing the permanent memories form scope of the previous bot
    data.context.current = get_hashmap_from_mem(
        &internal_use_get_memories(&data.client, &mut data.db)?,
        &data.context.flow,
    );

    Ok(())
}
/**
 * Switch bot find next bot in DB and create new Client and new conversation
 */
Switch bot find next bot in DB and create new Client and new conversation
Switch bot find next bot in DB and create new Client and new conversation
pub fn switch_bot(
    data: &mut ConversationInfo,
    bot: &mut CsmlBot,
    next_bot: SwitchBot,
    bot_opt: &mut BotOpt,
    event: &mut Event,
) -> Result<(), EngineError> {
    // update data info with new bot |ex| client bot_id, create new conversation
    *bot_opt = match next_bot.version_id {
        Some(version_id) => BotOpt::Id {
            version_id,
            bot_id: next_bot.bot_id,
            apps_endpoint: bot.apps_endpoint.take(),
            multibot: bot.multibot.take(),
        },
        None => BotOpt::BotId {
            bot_id: next_bot.bot_id,
            apps_endpoint: bot.apps_endpoint.take(),
            multibot: bot.multibot.take(),
        },
    };
    let mut new_bot = bot_opt.search_bot(&mut data.db)?;
    new_bot.custom_components = bot.custom_components.take();
    new_bot.native_components = bot.native_components.take();
    *bot = new_bot;
    set_bot_ast(bot)?;
    data.context.step = ContextStepInfo::UnknownFlow(next_bot.step);
    data.context.flow = match next_bot.flow {
        Some(flow) => flow,
        None => bot.get_default_flow_name(),
    };
    // update client with the new bot id
    data.client.bot_id = bot.id.to_owned();
    let (flow, step) = match get_flow_by_id(&data.context.flow, &bot.flows) {
        Ok(flow) => (flow, data.context.step.clone()),
        Err(_) => {
            let error_message = format!(
                "flow: [{}] not found in bot: [{}], switching to start@default_flow",
                data.context.flow, bot.name
            );
            let message = Message {
                content_type: "error".to_owned(),
                content: serde_json::json!(error_message.clone()),
            };
            // save message
            data.messages.push(message.clone());
            // send message
            send_msg_to_callback_url(data, vec![message], 0, false);
            // setting default step && flow
            data.context.step = ContextStepInfo::Normal("start".to_owned());
            data.context.flow = bot.get_default_flow_name();
            (
                get_flow_by_id(&bot.default_flow, &bot.flows)?,
                ContextStepInfo::Normal("start".to_owned()),
            )
        }
    };
    // update event to flow trigger
    event.content_type = "flow_trigger".to_owned();
    event.content = serde_json::json!({
            "flow_id": flow.id,
            "step_id": step
        }
    );
    // create new conversation for the new client
    data.conversation_id = create_conversation(
        &flow.id,
        &step.get_step(),
        &data.client,
        data.ttl.clone(),
        &mut data.db,
    )?;
    // and get memories of the new bot form db,
    // clearing the permanent memories form scope of the previous bot
    data.context.current = get_hashmap_from_mem(
        &internal_use_get_memories(&data.client, &mut data.db)?,
        &data.context.flow,
    );
    Ok(())
}
pub fn switch_bot ( data : & mut ConversationInfo , bot : & mut CsmlBot , next_bot : SwitchBot , bot_opt : & mut BotOpt , event : & mut Event , ) -> Result < ( ) , EngineError > { // update data info with new bot |ex| client bot_id, create new conversation * bot_opt = match next_bot . version_id { Some ( version_id ) => BotOpt :: Id { version_id , bot_id : next_bot . bot_id , apps_endpoint : bot . apps_endpoint . take ( ) , multibot : bot . multibot . take ( ) , } , None => BotOpt :: BotId { bot_id : next_bot . bot_id , apps_endpoint : bot . apps_endpoint . take ( ) , multibot : bot . multibot . take ( ) , } , } ; let mut new_bot = bot_opt . search_bot ( & mut data . db ) ? ; new_bot . custom_components = bot . custom_components . take ( ) ; new_bot . native_components = bot . native_components . take ( ) ; * bot = new_bot ; set_bot_ast ( bot ) ? ; data . context . step = ContextStepInfo :: UnknownFlow ( next_bot . step ) ; data . context . flow = match next_bot . flow { Some ( flow ) => flow , None => bot . get_default_flow_name ( ) , } ; // update client with the new bot id data . client . bot_id = bot . id . to_owned ( ) ; let ( flow , step ) = match get_flow_by_id ( & data . context . flow , & bot . flows ) { Ok ( flow ) => ( flow , data . context . step . clone ( ) ) , Err ( _ ) => { let error_message = format ! ( " " data context flow bot name ) ; let message = Message { content_type : " " . to_owned ( ) , content : serde_json :: json ! ( error_message clone ( ) ) , } ; // save message data . messages . push ( message . clone ( ) ) ; // send message send_msg_to_callback_url ( data , vec ! [ message ] , 0 , false ) ; // setting default step && flow data . context . step = ContextStepInfo :: Normal ( " " . to_owned ( ) ) ; data . context . flow = bot . get_default_flow_name ( ) ; ( get_flow_by_id ( & bot . default_flow , & bot . flows ) ? , ContextStepInfo :: Normal ( " " . to_owned ( ) ) , ) } } ; // update event to flow trigger event . content_type = " " . to_owned ( ) ; event . content = serde_json :: json ! ( { " " flow id " " step } ) ; // create new conversation for the new client data . conversation_id = create_conversation ( & flow . id , & step . get_step ( ) , & data . client , data . ttl . clone ( ) , & mut data . db , ) ? ; // and get memories of the new bot form db, // clearing the permanent memories form scope of the previous bot data . context . current = get_hashmap_from_mem ( & internal_use_get_memories ( & data . client , & mut data . db ) ? , & data . context . flow , ) ; Ok ( ( ) ) }
Switch bot find next bot in DB and create new Client and new conversation
Switch bot find next bot in DB and create new Client and new conversation
data
&mut ConversationInfo
bot
&mut CsmlBot
next_bot
SwitchBot
bot_opt
&mut BotOpt
event
&mut Event
data
&mut ConversationInfo
bot
&mut CsmlBot
next_bot
SwitchBot
bot_opt
&mut BotOpt
event
&mut Event