beards Module

beards Module

Handles the loading and running of skybeard plugins. architecture inspired by: http://martyalchin.com/2008/jan/10/simple-plugin-framework/ and http://stackoverflow.com/a/17401329

class beards.Beard(name, bases, attrs)[source]

Bases: type

Metaclass for creating beards.

beards = []
register(beard)[source]

Add beard to internal list of beards.

class beards.BeardChatHandler(*args, **kwargs)[source]

Bases: telepot.aio.helper.ChatHandler

Chat handler for beards.

This is the primary interface between skybeard and any plug-in. The plug-in must define a class that inherets from BeardChatHandler.

This class should overwrite __commands__ with a list of tuples that route messages containing commands, or if they pass certain “Filters” (see skybeard.beards.Filters). E.g:

```Python __commands__ = [

(‘mycommand’, ‘my_func’, ‘this is a help message’), (Filters.location, ‘my_other_func’, ‘another help message’)]

```

In this case, when the bot receives the command “/mycommand”, it will call self.my_func(msg) where msg is a dict containing all the message information. The filter (from skybeard.beards) will call self.my_other_func(msg) whenever “msg” contains a location. The help messages are collected by the help functions and automatically formatted and sent when a user sends /help to the bot.

Instances of the plug-in classes are created when required (such as when a filter is passed, a command or a regex pattern for the bot is matched etc.) and they are destructed after a set timeout. The default is 10 seconds, but this can be overwritten with, for example

_timeout = 90

The class should also define a __userhelp__ string which will be used in the auto help message generation.

deserialize(data)[source]

Deserializes the callback data

classmethod get_name()[source]

Get the name of the beard (e.g. cls.__name__).

get_username()[source]

Returns the username of the bot

on_chat_message(msg)[source]

Default on_chat_message for beards.

Can be overwritten in order to define the behaviour of the plug-in whenever any message is received.

NOTE: super().on_chat_message(msg) must be called in the overwrite to preserve default behaviour. This is usually done after custom behaviour, e.g.

```Python async def on_chat_message(self, msg):

await self.sender.sendMessage(“I got your message!”)

super().on_chat_message(msg)

```

on_close(e)[source]

Removes per beard logger handler and calls telepot default on_close.

register_command(pred_or_cmd, coro, hlp=None)[source]

Registers an instance level command.

This can be used to create instance specific commands e.g. if a user needs to type /cmdSOMEAPIKEY:

` self.register_commmand('cmd{}'.format(SOMEAPIKEY), 'name_of_coro') `

serialize(data)[source]

Serialises data to be specific for each beard instance.

Serialize callback data (such as with inline keyboard buttons). The id of the plug-in is encoded into the callback data so ownership of callbacks can be easily checked when it is deserialized. Also avoids the same plug-in receiving callback data from another chat

classmethod setup_beards(key)[source]

Perform setup necessary for all beards.

class beards.Command(pred, coro, hlp=None)[source]

Bases: object

Holds information to determine whether a function should be triggered.

class beards.Filters[source]

Bases: object

Filters used to call plugin methods when particular types of messages are received.

For usage, see description of the BeardChatHandler.__commands__ variable.

classmethod document(chat_handler, msg)[source]

Filters for sent documents

classmethod location(chat_handler, msg)[source]

Filters for sent locations

classmethod text(chat_handler, msg)[source]

Filters for text messages

class beards.SlashCommand(cmd, coro, hlp=None)[source]

Bases: object

Holds information to determine whether a telegram command was sent.

class beards.TelegramHandler(bot, parse_mode=None)[source]

Bases: logging.Handler

A logging handler that posts directly to telegram

emit(record)[source]
exception beards.ThatsNotMineException[source]

Bases: Exception

Raised if data does not match beard.

Used to check if serialized callback data belongs to the plugin. See BeardChatHandler.serialize()

beards.command_predicate(cmd)[source]

Returns a predicate coroutine which returns True if command is sent.

beards.create_command(cmd_or_pred, coro, hlp=None)[source]

Creates a Command or SlashCommand object as appropriate.

Used to make __commands__ tuples into Command objects.

beards.regex_predicate(pattern)[source]

Returns a predicate function which returns True if pattern is matched.