PilosusBot is a Telegram bot written in Python Flask. It reads Telegram chat messages, calculates sentiment scores (polarity score) and replies with the randomly selected message of the matching polarity.
Table of Contents
Application is based on Flask, a light-weight Python web framework that gets use of Werkzeug toolkit and Jinja2 template engine. Although Flask works fine with both Python 2 and 3, PilosusBot's written with Python 3 in focus.
Database-agnostic, application uses SQLAlchemy ORM, which enables user to choose between DBMS ranging from a simple SQLite to an enterprise solution of user's choice.
Asynchronous tasks tackled with Celery distributed task queue, which is to be used with a message broker RabbitMQ.
Redis is used for bounded in-memory queue for tracking messages processed so far.
The app relies on indicoio API for sentiment analysis as well as Telegram Bot API. It also uses Sentry for real-time error tracking. Make sure you get API tokens before you started app's deployment.
As a fallback for indicoio
API (paid service) app uses polyglot package for natural language processing. Installing it may be tricky requiring icu
and libicu-devel
packages being installed to your OS.
Based on Pili App, PilosusBot follows its same steps of deployment. manage.py
options are basically the same as in Pili
too. Please, refer to Pili
's documentation for help.
.env
file is kept under project's root directory. It's an entry point of app's configuration and deployment. Standard .env
file may look like this:
### Testing/Development/Production switch
FLASK_CONFIG=production
### Flask Variables
SECRET_KEY=...
DATABASE_URL=postgresql+psycopg2://user:passwd@your_db_server:5432/your_prod_db
TEST_DATABASE_URL=postgresql+psycopg2://user:passwd@127.0.0.1:5432/your_testing_db
### Server
SERVER_MAX_CONNECTIONS=30
### Mail server
MAIL_USERNAME=...
MAIL_PASSWORD=...
MAIL_SERVER=...
MAIL_PORT=587
MAIL_USE_TLS=True
### Celery
CELERY_BROKER_URL=amqp://guest@localhost//
CELERY_RESULT_BACKEND=rpc://
CELERY_QUEUE_ASSESS=assess
CELERY_QUEUE_SELECT=select
CELERY_QUEUE_SEND=send
## App
APP_ADMIN_EMAIL=...
APP_ADMIN_NAME=...
### Third-Party API
TELEGRAM_TOKEN=...
INDICO_TOKEN=...
SENTRY_DSN_SECRET=...
SENTRY_DSN_PUBLIC=...
See also config.py
for more configuration options.
The app partly relies on Miguel Grinberg's Flasky code (e.g. authentication module and DB models), which is a Flask application developed as an example for the excellent Flask Web Development book. Go grab your copy of the book, it's absolutely worth it!
See LICENSE file.