Skip to content

Instantly share code, notes, and snippets.

@gbin
Last active July 24, 2019 21:16
Show Gist options
  • Save gbin/3f040c537e497145dd9d to your computer and use it in GitHub Desktop.
Save gbin/3f040c537e497145dd9d to your computer and use it in GitHub Desktop.
This is an example plugin for errbot generating plugins and commands dynamically
from errbot import BotPlugin, botcmd
class PluginMaker(BotPlugin):
""" Example demonstrating how to create an errbot plugin out of thin air.
This basically generates a plugin from scratch and registers it at activation.
"""
def activate(self):
super().activate()
commands = {}
for command, result in [('toto', 'titi'), ('foo', 'bar')]:
# a silly loop generating commands
f = lambda self, msg, args: result
f.__name__ = command
f.__doc__ = "The documentation for %s command" % command
commands[command] = botcmd(f) # decorate it manually
# create the plugin with the generated commands
plugin_class = type('DynaPlug', (BotPlugin,), commands)
plugin_class.__errdoc__ = 'This is the doc for the dynamic plugin'
self.dynamic_plugin = plugin_class(self._bot)
# register the plugin commands
self._bot.inject_commands_from(self.dynamic_plugin)
def deactivates(self):
super().deactivate()
self._bot.remove_commands_from(self.dynamic_plugin)
>>> !help
[...]
!help DynaPlug - This is the doc for the dynamic plugin
>>> !help DynaPlug
Available commands for DynaPlug
• !foo - The documentation for foo command
• !toto - The documentation for toto command
>>> !foo
bar
>>> !toto
titi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment