Skip to main content

A logging and statistics bot for Telegram based on python-telegram-bot.

Project description

telegram-stats-bot

PyPi Package Version Supported Python versions GitHub Commit Activity LGPLv3 License

Telegram-stats-bot is a simple bot that lives in your Telegram group, logging messages to a Postgresql database and serving statistical tables and plots to users as Telegram messages.

Bot conversation example

Table of contents

Introduction

This software is intended to be run on a server, handling updates for a bot user with a single bot per channel (multi-channel support could be added at some point if there is interest), using the excellent Python-telegram-bot library.

The bot is still in active development but at the moment, it features:

  • Message logging to Postgresql database with optional JSON file backup

  • Statistics output for users in group as Telegram messages, with optional filtering by date or limiting to the querying user. Some statistics are more useful than others, but they are mainly intended to be fun for users to play with.

    • Tables:

      • Most active users

      • A user’s message time correlation with other users

      • A user’s median message time difference with other users

    • Plots:

      • Message activity by hour of day

      • Message activity by day of week

      • Message activity over the week by hour and day

      • Message activity history

Basic Requirements

  • Python 3.7+

  • A Telegram bot token with privacy mode disabled (needed to log messages)

    • See here for details

  • Postgresql (Tested with 12.3, but there shouldn’t be anything that won’t work with 9.4 or up)

    • This can be on a different system than telegram-stats-bot and requires either table creation permissions on a database or database can be pre-initialized following the setup in db.py

Installation

The easiest way to install or upgrade is with pip:

$ pip install telegram-stats-bot --upgrade

Or you can install the latest git version using poetry (installed to Python 3.7 or later):

$ git clone https://github.com/mkdryden/telegram-stats-bot.git
$ poetry install

Installing directly with setup.py should also work from the Pypi sdist (but why?).

Setup

Once installed, you can run the bot by calling the main module with a few required arguments:

$ python -m telegram_stats_bot.main BOT_TOKEN CHAT_ID POSTGRESQL_URL
  • BOT_TOKEN: Your bot’s token e.g., 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw

  • CHAT_ID: The chat id to monitor (will be a large integer, possibly negative, if unknown, set to 0 and see below)

  • POSTGRESQL_URL: Connection information in the form: postgresql://USERNAME:PASSWORD@ADDRESS/DB_NAME

    • if DB_NAME exists, there must not be tables called messages_utc, user_events, or user_names with incorrect columns

On startup, the bot will attempt to create the database and tables, if they do not already exist. If you do not know the chat’s id and have set it to 0 as mentioned above, you can send the /chatid command inside the group, and the bot will reply with it, then restart the bot with the id. If you have forgotten to disable privacy mode, an error will be logged in the terminal.

The bot will now log all messages in the group, but will only respond to users who have sent a message that has been logged previously (and this list is only updated once an hour, so if you’re impatient, you can restart the bot after you’ve sent a message to trigger the update). You can see if messages are being logged correctly by reviewing the terminal output. You should see a line like 2020-06-04 02:08:39,212 - __main__ - INFO - 8, whenever a message is logged.

Fetching Stats

To fetch stats, simply message the bot, either inside the group being logged, or in a direct message, using the /stats command. /stats with no arguments prints the table of most active users and other statistics are available through various subcommands. All commands are documented and the built in help can be displayed with /stats -h or stats <subcommand> -h.

Most commands have optional arguments that change the behaviour of the output. Nearly all have:

  • -start and -end followed by a timestamp (e.g., 2019, 2019-01, 2019-01-01, “2019-01-01 14:21”) specify the range of data to fetch, otherwise all available data will be used. Either or both options can be given.

  • -me calculates statistics for the user sending the command, rather than all chat users.

Sample outputs of each available subcommand follow.

counts

/stats counts returns a list of the most active users in the group.

              User  Total Messages  Percent
        @ACoolUser           42150      7.0
        @NumberOne           37370      6.2
 @WinstonChurchill           32668      5.4
          @AAAAAAA           32134      5.4
@WhereAreMyManners           30481      5.1
@TheWorstOfTheBest           28705      4.8

hours

/stats hours returns a plot of message frequency for the hours of the day.

Example of hours plot

days

/stats days returns a plot of message frequency for the days of the week.

Example of days plot

week

/stats week returns a plot of total messages over the data period by day of week and hour of day.

Example of week plot

history

/stats history returns a plot of messages versus date.

Example of history plot

titles

/stats titles returns a plot of group titles over time.

Example of title history plot

corr

/stats corr returns a list of users with the highest and lowest message time correlations with the requesting user.

User Correlations for @TheManWhoWasThursday
HIGHEST CORRELATION:
@MyGoodFriend         0.335
@Rawr                 0.302
@MangesUnePoutine     0.284
@GreenBlood           0.251
@TooMuchVacuum        0.235

LOWEST CORRELATION:
@Shiny                0.146
@BlueDog              0.142
@CoolCat              0.122
@EatMe                0.116
@JustPassingBy        0.106

delta

/stats delta returns a list of users with the shortest differences in message times with the requesting user.

Median message delays for @KingLeer and:
@PolyamorousPasta     00:03:23
@AggressiveArgon      00:04:43
@AdjectiveNoun        00:08:27
@SuperSalad           00:09:05
@ABoredProgrammer     00:09:06

types

/stats types returns a table of messages by type, comparing the requesting user with the full group.

Messages by type, @AUser vs group:
      type  Group Count  Group Percent  User Count  User Percent
      text     528813.0           88.3     13929.0          83.4
   sticker      34621.0            5.8      1226.0           7.3
     photo      25995.0            4.3      1208.0           7.2
 animation       6983.0            1.2       274.0           1.6
     video       1325.0            0.2        48.0           0.3
     voice        475.0            0.1         2.0           0.0
  location        252.0            0.0         2.0           0.0
video_note         84.0            0.0         1.0           0.0
     audio         62.0            0.0         1.0           0.0
      poll         29.0            0.0         1.0           0.0
  document          1.0            0.0         1.0           0.0
     Total     598640.0          100.0     16693.0         100.0

random

/stats random prints a random message from the database.

The Future

Telegram-stats-bot is a work in progress. New stats will be added, but no guarantees that the database structure will stay constant if Telegram’s message structure changes or I need to change something to make a new statistic work.

License

Telegram-stats-bot is free software: You can redistribute it and/or modify it under the terms of the GNU General Public License v3.0 or later. Derivative works must also be redistributed under the GPL v3 or later.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

telegram-stats-bot-0.3.1.tar.gz (532.3 kB view details)

Uploaded Source

Built Distribution

telegram_stats_bot-0.3.1-py3-none-any.whl (532.2 kB view details)

Uploaded Python 3

File details

Details for the file telegram-stats-bot-0.3.1.tar.gz.

File metadata

  • Download URL: telegram-stats-bot-0.3.1.tar.gz
  • Upload date:
  • Size: 532.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.6 Windows/10

File hashes

Hashes for telegram-stats-bot-0.3.1.tar.gz
Algorithm Hash digest
SHA256 c3d55b9f4ef9dd636b75f0cfc596dcd0ef6f0e967798fd8d4ff43c3091947898
MD5 c37d9d8e6226b50c5415bd7cd4a37bae
BLAKE2b-256 5ef07de2e830d34e489fedf931402c3e4269900b19a9b4999e626da4c4e92df8

See more details on using hashes here.

File details

Details for the file telegram_stats_bot-0.3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for telegram_stats_bot-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6efef90ee6e4bea232ef3d304b9d65f655137a3cfa77794c7b2bd3b2aa31b3d9
MD5 ffac1e587289f879cdec23e7109e89d8
BLAKE2b-256 f26feb28634f19f0a164a09540d2eb1c0f56e313ed7629ed14e968cc04b239a0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page