Skip to content

A simple statistics plugin for Telegram bots build with the python-telegram-bot library

Notifications You must be signed in to change notification settings

Bibo-Joshi/ptbstats

Repository files navigation

PTB Stats

Supported Python versions Supported PTB versions Documentation pre-commit.ci status

A simple statistics plugin for Telegram bots build with the python-telegram-bot library

Installation

Install via:

pip install git+https://github.com/Bibo-Joshi/[email protected]

ptbstats does not have a proper package (yet), because the author is too lazy for unittests and stuff …

Quickstart

Here is an example setup using the very basic SimpleStats statistics instance delivered along with ptbstats.

from telegram.ext import Application, PicklePersistence, filters, MessageHandler
from ptbstats import set_application, register_stats, SimpleStats


def main():
    """Start the bot."""

    async def post_init(app):
        # Set up stats
        set_application(application)
        # Count number of text messages
        register_stats(
            SimpleStats(
                "text", lambda u: bool(u.message and (filters.TEXT & ~filters.COMMAND).check_update(u))
            )
        )
        # Count number of inline queries
        register_stats(SimpleStats("ilq", lambda u: bool(u.inline_query and u.inline_query.query)))

    persistence = PicklePersistence("persistence.pickle")
    application = Application.builder().token("TOKEN").persistence(persistence).post_init(post_init).build()

    # Register handlers
    async def callback(u, c):
        await u.message.reply_text(u.message.text)

    application.add_handler(MessageHandler(filters.TEXT, callback))

    # Start the Bot
    application.run_polling()
if __name__ == "__main__":
main()

Advanced Usage

To create your own, customized statistics, subclass BaseStats.

About

A simple statistics plugin for Telegram bots build with the python-telegram-bot library

Topics

Resources

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •  

Languages