|
| 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>`. |
0 commit comments