hubtype / botonic

Build chatbots and conversational experiences using React
https://botonic.io
MIT License
519 stars 76 forks source link

plugin-flow-builder: bot action as follow up, first node in a flow, after conditionals etc #2898

Closed Iru89 closed 2 weeks ago

Iru89 commented 1 month ago

Description

Be able to use bot-action nodes at any point in the flow builder. For example as follow ups, after a conditional, at the start of a flow etc

The request.input should be immutable. To avoid modifying the request.input in the lambda execution, FlowBuilderAction.botonicInit function accepts as a second parameter the contentID to indicate which content has to be rendered. This contentID is the unique text that is defined in the flow builder frontend. This way you don't need to get the UUID of the content and modify the request.input.payload to indicate to the plugin which content to render.

Context

Bot-action nodes could only be used connected to a button or after a handoff. With this solution it will be possible to make flow in a simpler way. There are several use cases in which the flow is greatly simplified by being able to put bot actions after a conditional or at the beginning of a flow.

Flow example: Captura de pantalla 2024-09-03 a las 17 11 40

Approach taken / Explain the design

The third commit makes the core bot run again (prePlugins, routes, render, postPlugins). I think this should be the backend in charge of re-executing the lambda of the bot to avoid timeouts if the actions are asynchronous and more than one is chained. This way we would also avoid modifying the request.input.payload in a lambda execution.

IMPORTANT With this change in the custom actions written in the flow it is necessary to obtain the following contents using the function FlowBuilderAction.botonicInit(request) since this is the one in charge of putting the variable in the session so that if there is another bot action connected to the flow, the bot will continue executing.

To document / Usage example

WelcomeAction example

export class WelcomeAction extends FlowBuilderMultichannelAction {
  static contextType = RequestContext
  static async botonicInit(
    request: BotRequest
  ): Promise<FlowBuilderActionProps> {
    const { cmsPlugin, botContext } = getRequestData(request)
    const contents = await cmsPlugin.getStartContents(botContext.locale)

     return FlowBuilderMultichannelAction.botonicInit(request, contents[0].code)
  }
}

Testing

For now I have not checked the tests, I am aware that they are failing.

github-actions[bot] commented 2 weeks ago

Test Results

30 tests   26 :white_check_mark:  35s :stopwatch:  6 suites   0 :zzz:  1 files     4 :x:

For more details on these failures, see this check.

Results for commit aa03d621.

:recycle: This comment has been updated with latest results.