Skip to content

Commit fe45dfb

Browse files
author
Pietro Albini
committed
Add narrative documentation about a bot's structure
1 parent d6fd2d6 commit fe45dfb

File tree

3 files changed

+99
-33
lines changed

3 files changed

+99
-33
lines changed

docs/bot-structure.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
.. Copyright (c) 2016 Pietro Albini <[email protected]>
2+
Released under the MIT license
3+
4+
.. _bot-structure:
5+
6+
===========================
7+
Structure of a botogram bot
8+
===========================
9+
10+
.. TODO - Rewrite this
11+
12+
As a framework, botogram requires you to code your bot in a specific way. This
13+
means, you must follow a simple structure, explained here.
14+
15+
.. _bot-structure-skeleton:
16+
17+
A bot's skeleton
18+
================
19+
20+
Even if you write two completly different bots, you'll have the same
21+
boilerplate code in both of them. This is because you need to create the bot
22+
instance, and start the runner after you executed all of your bot's code.
23+
24+
botogram tries to reduce the boilerplate code as much as possible, but some of
25+
it is needed anyway. Here there is all the required code:
26+
27+
.. code-block:: python
28+
29+
import botogram
30+
bot = botogram.create("YOUR-API-KEY")
31+
32+
# Your code goes here
33+
34+
if __name__ == "__main__":
35+
bot.run()
36+
37+
As you can see, there are two required code blocks in botogram:
38+
39+
* The first one imports botogram in your program, and creates a bot instance
40+
you can start to customize.
41+
42+
* The second one executes the bot. The if clausole is needed only if you plan
43+
to run your bot on Windows, so if you're using Linux or OSX you can omit it.
44+
45+
So, that's all the code required to create a bot with botogram. You can now
46+
start the actual bot creation!
47+
48+
.. _bot-structure-hooks:
49+
50+
Introduction to hooks
51+
=====================
52+
53+
Hooks are the way to go to add some logic to your bot: they allows you to react
54+
when an user calls a command, write something or a lot more situations. Hooks
55+
are added to your bot with decorated functions, or :ref:`with components
56+
<custom-components>`.
57+
58+
For example, this is an hook which is executed when someone calls the ``/test``
59+
command:
60+
61+
.. code-block:: python
62+
63+
@bot.command("test")
64+
def test_command(chat, message, args):
65+
# Your hook's code
66+
67+
You can put any code you want in each hook, but remember hooks might be
68+
executed in different processes by the runner, so avoid using global variables.
69+
If you need to store global state check out :ref:`shared memory
70+
<shared-memory>`.
71+
72+
.. _bot-structure-hooks-args:
73+
74+
Dynamic hooks arguments
75+
=======================
76+
77+
Hooks are usually called with a lot of useful information, but you don't need
78+
all of it every time. For example, you might not need shared memory in a whole
79+
bot, but you have to use it everytime in another.
80+
81+
In order to avoid having to write everytime a really long list of arguments,
82+
botogram is smart enought to figure out what you need and provide only that.
83+
So, if in a command you just need the message and the shared memory, you can
84+
define your hook this way:
85+
86+
.. code-block:: python
87+
88+
@bot.command("test")
89+
def test_command(message, shared):
90+
# Your hook's code
91+
92+
In addition to the arguments provided by each hook, botogram allows you to
93+
request those additional arguments:
94+
95+
* **bot**, which is an instance of the current bot.
96+
97+
* **shared**, which is an instance of your bot/component's :ref:`shared memory
98+
<shared-memory>`.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Narrative documentation
5353
:maxdepth: 2
5454

5555
bot-creation
56+
bot-structure
5657
shared-memory
5758
tasks
5859
custom-components

docs/tricks.rst

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,6 @@ The whole point of botogram is to make your life easier when creating Telegram
1111
bots. In order to do so in a better way, there are some tricks you can use to
1212
speed the development up.
1313

14-
.. _tricks-dynamic-arguments:
15-
16-
Dynamic arguments
17-
=================
18-
19-
There are a lot of information and methods you can get from every decorator,
20-
but you don't always need them. botogram is smart enough to know which
21-
arguments you want and in which order you want them. This means you can request
22-
only the ones you need:
23-
24-
.. code-block:: python
25-
26-
@botogram.command("test")
27-
def test(args, chat):
28-
chat.send(" ".join(args))
29-
30-
By default, the :py:meth:`~botogram.Bot.command` decorator provides three
31-
arguments: ``chat``, ``message`` and ``args``. Instead, the function above
32-
requested only two of them, and botogram is able to provide only them in the
33-
right order.
34-
35-
Please remember this will work only if a function is called directly by
36-
botogram (for example when a command is issued by a user). If you call it by
37-
hand, you'll need to provide the arguments by hand.
38-
39-
There are some extra arguments you can request from every function called by
40-
botogram, without being directly provided by the decorator:
41-
42-
* **bot**, which is an instance of the current bot.
43-
44-
* **shared**, which is an instance of the bot's
45-
:ref:`shared memory <shared-memory>`.
46-
4714
.. _tricks-messages-syntax:
4815

4916
Rich formatting with message syntaxes

0 commit comments

Comments
 (0)