Debian Codegraph @

docs/user_guide/flow_development/advanced.rst

raw
1 Advanced Flow Definitions
2 =========================
3
4 Storing something in the flow context
5 -------------------------------------
6
7 Flows have a state the plugins can use to store some contextual information.
8 Let's take back out simple linear flow:
9
10 .. code-block:: python
11
12 @botflow
13 def example(self, flow: FlowRoot):
14 first_step = flow.connect('first')
15 second_step = first_step.connect('second')
16 third_step = second_step.connect('third')
17
18 You can represent this flow like this:
19
20 .. figure:: basics_1.svg
21 :align: center
22
23 You can store something in the context, for example in ``!first`` and retrieve it in ``!second``. Like this:
24
25 .. code-block:: python
26
27 @botcmd
28 def first(self, msg, args):
29 msg.ctx['mydata'] = 'Hello'
30 return 'First done!'
31
32 @botcmd
33 def second(self, msg, args):
34 return msg.ctx['mydata'] + ' World!'
35
36 ``msg.ctx`` is a dictionary created every time a flow starts.
37
38 Making a step execute automatically
39 -----------------------------------
40
41 In our previous example, if ``msg.ctx['mydata']`` is populated, we can arguably expect that Errbot should not wait for
42 the user to enter ``!second`` to execute it and just proceed by itself.
43
44 You can do that by defining a **predicate**, which is a simple function that returns ``True`` if the conditions
45 to proceed to the next step are met. The function takes only one parameter, the context, the same one you get from ``msg.ctx``.
46
47 .. code-block:: python
48
49 @botflow
50 def example(self, flow: FlowRoot):
51 first_step = flow.connect('first')
52 second_step = first_step.connect('second',
53 predicate=lambda ctx: 'mydata' in ctx)
54 third_step = second_step.connect('third')
55
56
57 Now, after starting the flow with ``!flows start example``, the state will be:
58
59 .. figure:: basics_3.svg
60 :align: center
61
62 Errbot will wait for ``!first``. But then, once the user executes ``!first``, it will see that the predicate between
63 ``!first`` and ``!second`` is verified, so will go on and execute ``!second``, displaying 'Hello World' and proceed to wait
64 for ``!third``:
65
66 .. figure:: basics_4.svg
67 :align: center
68
69
70 Branching in the graph
71 ----------------------
72
73 It is perfectly possible to branch out to several possibilities (possibly with different predicates).
74
75 .. code-block:: python
76
77 @botflow
78 def example(self, flow: FlowRoot):
79 first_step = flow.connect('first')
80 second_step = first_step.connect('second',
81 predicate=lambda ctx: 'mydata' in ctx)
82 other = first_step.connect('other_second',
83 predicate= lambda ctx: 'otherdata' in ctx)
84
85
86 This will do something like that:
87
88 .. figure:: advanced_1.svg
89 :align: center
90
91 In manual mode, the bot will tell the user about his 2 possible options to continue.
92
93 Making a looping graph
94 ----------------------
95
96 You can also perfectly re-execute a part of a graph in a "loop". You can branch directly the node object
97 instead of the command name in that case.
98
99 .. code-block:: python
100
101 @botflow
102 def example(self, flow: FlowRoot):
103 first_step = flow.connect('first')
104 second_step = first_step.connect('second')
105 third_step = second_step.connect('third')
106 third_step.connect(first_step, predicate=...)
107 final_step = third_step.connect('final', predicate=...)
108
109 You can represent this flow like this:
110
111 .. figure:: advanced_2.svg
112 :align: center
113
114 The typical use case is to repeatedly ask something to the user.