/Karaka

Telegram bot to send message to a user via Telegram. Its original use case is to send alert about abnormal condition from farm control system to farmkeepers.

Primary LanguagePython

Karaka

Stack

  • Python 3
  • Django 3.0+ as backend framework
  • PostgreSQL for database

Setup for development

  • Requires python 3.7+

  • create a python virtual environment for project

  • Install dependency package from Ubuntu repository

    sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib
  • Create a database name alarmbot in PostgreSQL. To ease development, we connect PostgreSQL via Unix socket. This method let you access database without specifying username and password in project's config.

    sudo -u postgres createuser --superuser $USER
    
    sudo -u postgres createdb alarmbot --owner $USER
  • Install Poetry . We use it to manage Python dependencies for this project.

  • Create a Python virtual environment for this project. There are many tools for this. You can also use Poetry (but please not use Pipenv).

  • Go to the source folder, activate the virtual environment.

    (venv)$ poetry install --no-root

    to install Python packages.

Deploy

  • Run:

    ./runbot.py

    to run Telegram bot (which chats with user)

  • Run:

    ./runserver.py

    to launch REST API server, for other application to send message to user

Configuration

Telegram bot

Register a Telegram bot to get token.

Authentication

Add to .secrets.toml file. Example

[default]
TELEGRAM_TOKEN = 'TOKEN_THAT_TELEGRAM_GAVE_YOU'

API_USERS = [
    ['abc', '123456']
]

Translation

pybabel extract alarmbot/*.py -o locales/alarmbot.pot
pybabel compile -d locales -l vi -D alarmbot

API

Create user

POST /users/
{
    'username': 'someone',
    'first_name': '',
    'last_name': '',
    'language_code': 'vi'
}

with username being Telegram username.

Send message to user

POST /users/[username]/message
{
    'message': 'Your farm is on fire!'
}