| 1 |
Backend-specifics |
| 2 |
================= |
| 3 |
|
| 4 |
Errbot uses external libraries for most backends, which may offer additional |
| 5 |
functionality not exposed by Errbot in a generic, backend-agnostic fashion. |
| 6 |
|
| 7 |
It is possible to access the underlying client used by the backend you are |
| 8 |
using in order to provide functionality that isn't otherwise available. |
| 9 |
Additionally, interacting directly with the bot internals gives you the freedom |
| 10 |
to control Errbot in highly specific ways that may not be officially supported. |
| 11 |
|
| 12 |
.. warning:: |
| 13 |
|
| 14 |
The following instructions describe how to interface directly with the underlying bot object and clients of backends. |
| 15 |
We offer no guarantees that these internal APIs are stable or that a given backend will continue to use a given client in the future. |
| 16 |
The following information is provided **as-is** without any official support. |
| 17 |
We can give **no** guarantees about API stability on the topics described below. |
| 18 |
|
| 19 |
|
| 20 |
Getting to the bot object |
| 21 |
------------------------- |
| 22 |
|
| 23 |
From within a plugin, you may access `self._bot` in order to get to the instance of the currently running bot class. |
| 24 |
For example, with the Telegram backend this would be an instance of :class:`~errbot.backends.telegram.TelegramBackend`: |
| 25 |
|
| 26 |
.. code-block:: python |
| 27 |
|
| 28 |
>>> type(self._bot) |
| 29 |
<class 'errbot.backends.TelegramBackend'> |
| 30 |
|
| 31 |
To find out what methods each bot backend has, you can take a look at the documentation of the various backends in the :mod:`errbot.backends` package. |
| 32 |
|
| 33 |
Plugins may use the `self._bot` object to offer tailored, backend-specific functionality on specific backends. |
| 34 |
To determine which backend is being used, a plugin can inspect the `self._bot.mode` property. |
| 35 |
The following table lists all the values for `mode` for the official backends: |
| 36 |
|
| 37 |
============================================ ========== |
| 38 |
Backend Mode value |
| 39 |
============================================ ========== |
| 40 |
:class:`~errbot.backends.irc` irc |
| 41 |
:class:`~errbot.backends.slack` slack |
| 42 |
:class:`~errbot.backends.telegram_messenger` telegram |
| 43 |
:class:`~errbot.backends.test` test |
| 44 |
:class:`~errbot.backends.text` text |
| 45 |
:class:`~errbot.backends.xmpp` xmpp |
| 46 |
============================================ ========== |
| 47 |
|
| 48 |
Here's an example of using a backend-specific feature. In Slack, emoji reactions can be added to messages the bot |
| 49 |
receives using the `add_reaction` and `remove_reaction` methods. For example, you could add an hourglass to messages |
| 50 |
that will take a long time to reply fully to. |
| 51 |
|
| 52 |
.. code-block:: python |
| 53 |
|
| 54 |
from errbot import BotPlugin, botcmd |
| 55 |
|
| 56 |
class PluginExample(BotPlugin): |
| 57 |
@botcmd |
| 58 |
def longcompute(self, mess, args): |
| 59 |
if self._bot.mode == "slack": |
| 60 |
self._bot.add_reaction(mess, "hourglass") |
| 61 |
else: |
| 62 |
yield "Finding the answer..." |
| 63 |
|
| 64 |
time.sleep(10) |
| 65 |
|
| 66 |
yield "The answer is: 42" |
| 67 |
if self._bot.mode == "slack": |
| 68 |
self._bot.remove_reaction(mess, "hourglass") |
| 69 |
|
| 70 |
|
| 71 |
Getting to the underlying client library |
| 72 |
---------------------------------------- |
| 73 |
|
| 74 |
Most of the backends use a third-party library in order to connect to their respective network. |
| 75 |
These libraries often support additional features which Errbot doesn't expose in a generic |
| 76 |
way so you may wish to make use of these in order to access advanced functionality. |
| 77 |
|
| 78 |
Backends set their own attribute(s) to point to the underlying libraries' client instance(s). |
| 79 |
The following table lists these attributes for the official backends, along with the library used by the backend: |
| 80 |
|
| 81 |
|
| 82 |
============================================ ========================= ================================================ |
| 83 |
Backend Library Attribute(s) |
| 84 |
============================================ ========================= ================================================ |
| 85 |
:class:`~errbot.backends.irc` `irc`_ ``self._bot.conn`` ``self._bot.conn.connection`` |
| 86 |
:class:`~errbot.backends.slack` `slackclient`_ ``self._bot.sc`` |
| 87 |
:class:`~errbot.backends.telegram_messenger` `telegram-python-bot`_ ``self._bot.telegram`` |
| 88 |
:class:`~errbot.backends.xmpp` `slixmpp`_ ``self._bot.conn`` |
| 89 |
============================================ ========================= ================================================ |
| 90 |
|
| 91 |
.. _hypchat: https://pypi.org/project/hypchat/ |
| 92 |
.. _irc: https://pypi.org/project/irc/ |
| 93 |
.. _`telegram-python-bot`: https://pypi.org/project/python-telegram-bot |
| 94 |
.. _slackclient: https://pypi.org/project/slackclient/ |
| 95 |
.. _slixmpp: https://pypi.org/project/slixmpp |