ptbstats.BaseStats

class ptbstats.BaseStats(command, async_stats=False, async_command=False)

Bases: Generic[telegram.ext.handler.UT], abc.ABC

Base class for storing and displaying statistics.

command

The command that produces the statistics associated with this instance.

async_command

Whether reply_statistics() should be run asynchronously using telegram.ext.Disptacher.run_async().

Parameters
  • command (str) – The command that should produce the statistics associated with this instance.

  • async_stats (bool) – Whether the process_update() should be run asynchronously using telegram.ext.Disptacher.run_async(). Defaults to False.

  • async_command (bool) – Whether reply_statistics() should be run asynchronously using telegram.ext.Disptacher.run_async(). Defaults to False.

Warning

command must not appear twice between statistics instances!

abstract check_update(update)

This method is called to determine if an update should be processed by this statistics instance. It must always be overridden.

Note

You can imitate Filters by this method by something like:

def check_update(self, update: Update):
    return update.effective_message and (Filters.text | Filters.sticker)(update)
Parameters

update (telegram.Update) – The update to be tested.

Return type

Optional[bool]

Returns

Either None or False if the update should not be handled, True otherwise.

persistent_data()

In order to make statistics instances persistent, you will have to do two things:

  1. Use persistence with your PTB setup

  2. Override this method.

Warning

ptbstats uses stores data in bot_data['PTBStats']. Do not store any other data there.

Returns

Dictionary of attributes that are to be persisted. On reboot, this instances __dict__ will be merged with this return value.

Return type

Dict[str, Any]

abstract process_update(update)

This method is called on every update, that passes check_update(). The telegram.ext.CallbackContext argument is deliberately not passed, as this method should be independent from it.

This method must be overridden to update the statistics in an appropriate manner.

Parameters

update (Update) – The telegram.Update.

Return type

None

abstract reply_statistics(update, context)

This method will be used as the callback of the telegram.ext.CommandHandler associated with this statistics instance. It must be overridden to reply with the current status of the statistics in an appropriate manner.

Parameters
Return type

None