Python Chatbot Analytics

In 2020, I wrote an article Analytics for Telegram bots with a fairly simple solution, with 2 metrics and suboptimal data storage. After the advent of Telegram ads, analytics has become even more necessary, as it is now possible to advertise bots directly to users. The advantages of my new solution in comparison with others on the market: it is free, easy to implement, 20 metrics for analytics, you can create your own metrics, data is stored only with you, suitable for any number of messages per month, keeps a history for the entire connection time, can store data on any number of bots in one place. Also, this solution can be implemented not only for Telegram chat bots, but also for any other chat bots (Slack, Discord, Vk, and so on).

Named the library LoraBot. Analytics is connected to your bot as a third-party library, and all data is stored in PostgreSQL. You can deploy the base on your server using the docker-compose file that is in the repository. If you have your own database or want to deploy it in the cloud as SaaS, you need to create a user yourself, run the file create_tables.sql and register accesses in the .env file. also in repositories you can find Telegram, Slack, Discord, Vk chatbots with already implemented analytics and a way to get analytics from LoraBot.

How to implement analytics in your chatbot?

  1. You need to download LoraBot as zip or using git:

    git clone
  2. Start the database for LoraBot with the following command:
    If you don’t have docker-compose installed, you can read how to install it here:

    docker-compose up -d
  3. Install the required libraries for LoraBot:

    pip3 install -r requirements.txt
  4. Set a password to access analytics in the .env file:

  5. Initialize LoraBot in your bot:

    from lorabot import LoraBot
    lora_bot = LoraBot("your bot's name")
  6. Install LoraBot’s data collection features to start getting insights for analytics:

    #для отслеживания новых пользователей
    #для отслеживания команд, сообщений из меню и обычных сообщений
    lora_bot.message(TEXT, TEXT_TYPE, USER_ID)
    #для отслеживания событий
    lora_bot.event(EVENT, EVENT_TYPE, USER_ID)
    #для отслеживания отзывов о боте, USER_ID)
    #для отслеживания оценок бота
    lora_bot.assessment(RATING_IN_INT_FORMAT, USER_ID)
  7. To get analytics, make some kind of conditional branch, for example, in the example, a condition is set for receiving a message with a keyword, after that the password is checked, and after that the bot owner gets access to the analytics:

Analytical metrics

An example of data visualization returned by LoraBot
An example of data visualization returned by LoraBot

By link you can find all the metrics that are analyzed in LoraBot. Please note that some metrics have options to set the analytics period, the amount of data returned, or to set the type of messages/events. Also, some functions return only textual information, but there are functions that return both data visualization and textual information. Here are some examples of how to return information from analytics in a Telegram bot:

#Возвращает общую информацию о пользователях бота(только текст)
info = lora_bot.analyze_total(START_PERIOD, END_PERIOD)
bot.send_message(, info)
#Возвращает информацию о ежедневных активных пользователях(график + текст)
photo, info = lora_bot.analyze_dau(START_PERIOD, END_PERIOD)
bot.send_message(, info)
bot.send_photo(, photo)

There are 20 metrics in LoraBot and they can be divided into the following groups:

There is also a separate function, where you can pass your SQL query directly from the bot. It will be useful for creating custom metrics.

Database schema where information on users, messages, events and reviews is stored:


I think in this article I will close this topic for myself, at the moment the solution has been tested with me for a month and the result satisfied me. Hope this solution helps you too.

PS Analytics is of course good, but if you need your bot to work without manual launch, wrap it in a Dockerfile, as advised in this article Your own Telegram bot to monitor the uptime of your servers in python and docker, and run it in the same docker- compose file, where the database also starts. Hosting a bot on a server will become much easier.

Similar Posts

Leave a Reply