| 1 |
Basic Flow Definition |
| 2 |
===================== |
| 3 |
|
| 4 |
Flows are like plugins |
| 5 |
---------------------- |
| 6 |
|
| 7 |
They are defined by a ``.flow`` file, similar to the plugin ones: |
| 8 |
|
| 9 |
.. code-block:: ini |
| 10 |
|
| 11 |
[Core] |
| 12 |
Name = MyFlows |
| 13 |
Module = myflows |
| 14 |
|
| 15 |
[Documentation] |
| 16 |
Description = my documentation. |
| 17 |
|
| 18 |
[Python] |
| 19 |
Version = 3 |
| 20 |
|
| 21 |
|
| 22 |
Now in the ``myflows.py`` file you will have pretty familiar structure with a ``BotFlow`` as type and @botflow |
| 23 |
as flow decorator: |
| 24 |
|
| 25 |
.. code-block:: python |
| 26 |
|
| 27 |
from errbot import botflow, FlowRoot, BotFlow |
| 28 |
|
| 29 |
class MyFlows(BotFlow): |
| 30 |
""" Conversation flows for Errbot""" |
| 31 |
|
| 32 |
@botflow |
| 33 |
def example(self, flow: FlowRoot): |
| 34 |
""" Docs for the flow example comes here """ |
| 35 |
# [...] |
| 36 |
|
| 37 |
Errbot will pass the root of the flow as the only parameter to your flow definition so you can build your graph |
| 38 |
from there. |
| 39 |
|
| 40 |
|
| 41 |
Making a simple graph |
| 42 |
--------------------- |
| 43 |
|
| 44 |
Within your flow, you can connect commands together. |
| 45 |
For example, to make a simple linear flow between ``!first``, ``!second`` and ``!third``: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
@botflow |
| 50 |
def example(self, flow: FlowRoot): |
| 51 |
first_step = flow.connect('first') # first is a command name from any loaded plugin. |
| 52 |
second_step = first_step.connect('second') |
| 53 |
third_step = second_step.connect('third') |
| 54 |
|
| 55 |
You can represent this flow like this: |
| 56 |
|
| 57 |
.. figure:: basics_1.svg |
| 58 |
:align: center |
| 59 |
|
| 60 |
|
| 61 |
O is the state "not started" for the flow ``example``. |
| 62 |
|
| 63 |
You can start this flow manually by doing ``!flows start example``. |
| 64 |
|
| 65 |
The bot will tell you that it expects a ``!first`` command: |
| 66 |
|
| 67 |
.. figure:: basics_3.svg |
| 68 |
:align: center |
| 69 |
|
| 70 |
Once you have executed ``!first``, you will be in that state: |
| 71 |
|
| 72 |
.. figure:: basics_2.svg |
| 73 |
:align: center |
| 74 |
|
| 75 |
The bot will tell you that it expects ``!second``, etc. |
| 76 |
|
| 77 |
.. figure:: basics_4.svg |
| 78 |
:align: center |
| 79 |
|
| 80 |
Making a flow start automatically |
| 81 |
--------------------------------- |
| 82 |
|
| 83 |
Now, usually flows are linked to a first action your users want to do. For example: ``!poll new``, ``!vm create``, |
| 84 |
``!report init`` or first commands like that that suggests that you will have a follow-up. |
| 85 |
|
| 86 |
To trigger a flow automatically on those first commands, you can use ``auto_trigger``. |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
@botflow |
| 91 |
def example(self, flow: FlowRoot): |
| 92 |
first_step = flow.connect('first', auto_trigger=True) |
| 93 |
second_step = first_step.connect('second') |
| 94 |
third_step = second_step.connect('third') |
| 95 |
|
| 96 |
|
| 97 |
You can still represent this flow like this: |
| 98 |
|
| 99 |
.. figure:: basics_1.svg |
| 100 |
:align: center |
| 101 |
|
| 102 |
BUT, when a user will execute a ``!first`` command, the bot will instantly instantiate a Flow in this state: |
| 103 |
|
| 104 |
.. figure:: basics_2.svg |
| 105 |
:align: center |
| 106 |
|
| 107 |
And tell the user that ``!second`` is the follow-up. |
| 108 |
|
| 109 |
Flow ending |
| 110 |
----------- |
| 111 |
|
| 112 |
If a node has no more children and a user passed it, it will automatically end the flow. |
| 113 |
|
| 114 |
Sometimes, with loops etc., you might want to explicitly mark an END FlowNode with a predicate. You can do it like this, |
| 115 |
for example for a guessing game plugin: |
| 116 |
|
| 117 |
.. figure:: end.svg |
| 118 |
:align: center |
| 119 |
|
| 120 |
In the flow code... |
| 121 |
|
| 122 |
|
| 123 |
.. code-block:: python |
| 124 |
|
| 125 |
from errbot import botflow, FlowRoot, BotFlow, FLOW_END |
| 126 |
|
| 127 |
class GuessFlows(BotFlow): |
| 128 |
""" Conversation flows related to polls""" |
| 129 |
|
| 130 |
@botflow |
| 131 |
def guess(self, flow: FlowRoot): |
| 132 |
""" This is a flow that can set a guessing game.""" |
| 133 |
# setup Flow |
| 134 |
game_created = flow.connect('tryme', auto_trigger=True) |
| 135 |
one_guess = game_created.connect('guessing') |
| 136 |
one_guess.connect(one_guess) # loop on itself |
| 137 |
one_guess.connect(FLOW_END, predicate=lambda ctx: ctx['ended']) |