A simple statistics plugin for Telegram bots build with the python-telegram-bot library
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 …
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()
To create your own, customized statistics, subclass BaseStats.