Bot-reminder for Telegram
And contains examples of usage:
- Akka for actors;
- Akka persistence for persistence;
- Akka finite state machine for contextual behavior;
- Scala telegram bot api for bot engine;
- Flyway for migration;
- Twitter text for hashtag extraction;
- Typesafe config for configuration;
- Sbt native packager to publish docker images;
- Docker for deployment (docker repository);
This bot should accept your messages, parse hashtags and let you navigate across messages and hashtags
To use this bot locally you need:
- Install java 8
- Optional Set up IntelliJ Idea (tip: you can use EAP for free for home projects)
- Clone this project
- Install scala and sbt
- Optional Tip: use IntelliJ Idea Grep Console to view color logs
- Install and set up Postgres (read more below)
- Create bot and specify token with
This application uses sbt native plugin to package jar and docker images
You can publish your application to docker hub using:
You should have docker installed and make
docker login before to push to docker hub (by default it's public).
I use logging to check the status of my application. In most cases logging is the only way to find out that users are facing issues in the application
Logging is used for debugging sakes only, so text logging is enough for me. I'm not tracking the activity of users at the current stage
For text logging i use default scala stack:
- slf4j as a logger facade
- typesafe logging for syntactic sugar above slf4j
- logback as a logger backend
This technology list might overwhelm at first, but it's easy, once you have set up everything.
To set up logging you should specify its' settings using
You can create it in resources and logback will find it automatically.
My development settings are in
My production settings are in
To use logging you should implement
StrictLogging trait and then i can use logger variable from it.
String interpolation will be calculated lazily.
logger.debug(s"Message [$message] snoozed for [$duration]")
I print my logs in console and use ANSI colors to separate different levels (ERROR from INFO). If you use console, ANSI colors are working by default. If you use IntelliJ Idea, use Grep Console to enable it.
In production i separate debug and error logs in different files. Use rollback policies to zip my logs and move it to archive directory.
You can see it in
To run and update my application in production i use docker.
Docker images are stateless, i should expose volume from my hard drive to write logs there.
-v argument for it, like this:
-v ~/lise-bot/logs:/logs -v ~/lise-bot/production-conf:/config
You can run bot from docker repository using:
sudo docker run \ --name=lise-bot --net=host \ -v /path/to/lise-bot/logs:/logs \ -v /path/to/lise-bot/journal:/journal \ -v /path/to/lise.bot/production-conf:/config \ eliseealex/lise-bot:1.0-SNAPSHOT \ -Dbot.token=TOKEN \ -Ddb.default.user=LISE_DB_USER \ -Ddb.default.password=LISE_DB_PASS \ -Ddb.default.url=LISE_DB_URL \ -Dakka.persistence.journal.leveldb.dir=/journal \ -Dakka.persistence.snapshot-store.leveldb.dir=/journal/snapshot \ -Dlogback.configurationFile=/config/logback.xml >> lise-bot/bot.log &
--net=host access Postgres through localhost.
Hint: use explicit tag.
Postgres set up
You will need new database
lise, new user
lise_root that have privileges to this database:
# create database lise; CREATE DATABASE # create user lise_root with password 'root'; CREATE ROLE # GRANT ALL privileges ON DATABASE lise TO lise_root; GRANT
To provide access from localhost with password change
pb_hba.conf to match:
# "local" is for Unix domain socket connections only local all all md5
We are using flyway for migration.
To initialize or migrate database use:
To get info about migration:
To your migrations:
Be careful you should clean with the same scripts that you initialized.
Don't commit production settings, instead use:
sbt flywayMigrate -Dflyway.user=prodUser -Dflyway.password=prodPassword -Dflyway.url=prodUrl
Or you can use console flyway utility with:
./flyway -user=prodUser -password=prodPass -url=jdbc:postgresql://localhost:5432/prodDatabase -locations=filesystem:/home/user/path/to/scripts/ migrate
To install it on linux:
- unarchive with
tar -xvf flyway-commandline-4.0.3-linux-x64.tar.gz;
- change directory to anarchived.