KateBu / bot-1.3

BSD 3-Clause "New" or "Revised" License
0 stars 1 forks source link

Echo bot for Telegram and VK

The Telegram bot works properly and supports any types of messages (with or without attachments).

The VK bot can process text messages, /repeat and /help commands, map coordinates, links, stickers, audio and video attachments. If there is an attachment VK bot cannot process, the attachment will be ignored.

Project structure description

The project is devided into three parts: app (Main module), src (see the structure of this part below), test (contains unit tests).

Src structure

The src part contains the Bot module and the following folders:

Bot module

The Bot module exports runBot function (called in the Main module). The function holds a chain of actions: the bot gets updates, handles them and then call the nextLoop function (it prints the log message and calls runBot again).

API

The API folder contains three folders: PureStructs, VK and Telegram. The aim of the functions, actions and structures of these folders is to decode bytestrings into specific structures (Telegram structures or VK ones) and then build PureMessages.

The PureStructs folder contains some data structures the Telegram or VK messaged supposed to be decoded to. The folder contains the following modules:

The Telegram folder contains the following:

The VK folder contains the following:

Config

The Config folder holds all the necessary things to initialize and use Config. There are the following modules inside:

Environment

Environment is a data structure that holds all the necessary information such as current bot config, logger, and some other data from .config file. The Environment folder contains the following modules:

Exceptions

The Exceptions folder contains BotException data structs, MonadThrow typeclass definition, a set of functions that throw different types of exceptions and an exception handle function. The folder contains the following modules:

Logger

The Logger folder holds all the necessary things to initialize and use logger. It contains the following modules:

Logic

The Logic folder contains all the necessary things for handling pure messages. Depending on the message type, the message may be ignored (for Empty messages), sent back to user several times (for Common messages). If it is a UserCommand message, user gets a special answer, and if it is a Callback message some information should be changed in a database. The folder contains the following:

Services

The Services folder provides all the necessary services for connecting to a server (for getting updates and sending messages) and connecting to a database (for looking for users, adding new users and updating user information). The Services folder contains the following:

TextMessages

The TextMessages folder contains all the text messages used in the project. The folder contains the following modules:

Wrapper

The Wrapper folder contains all the necessary instruments for building requests to server and decoding server responses. The folder contains the following:

What the bot can do

The bot can get a message from user (any kinds of messages) and then send the message back to the user several times. If the user sends "/help" command, the bot sends some information about itself. If the user sends "/repeat" command, the bot sends an inline keyboard, so the user can choose the number of repetitions.

How to run

How to fill in the Config File (config.config):

botType: Either "VK" or "Telegram" (quotes are required);

repetition: an Int (recommended from 1 to 5);

telegramToken: put your Telegram Token here if you want to run a Telegram Bot (quotes are required);

VKToken: put your VK Token here if you want to run a VK Bot (quotes are required);

VKGroupID: an Int, put your VK Group ID here if you want to run a VK Bot;

helpMessage: you can change the message a user gets if sends /help command (quotes are required);

logPriority: use "Debug", "Info", "Warning" or "Error" (quotes are required).

dbConnectString: a string for connecting to a database (quotes are required)

How to create a database:

The bot works with a primitive database (PostreSQL), that contains one table to store user IDs and number of repetition for every user. The database supposed to be hosted locally. So to create the database you just have to write a few strings in psql command line:

How it works

You can see some screenshots below.

Telegram bot

Usual text message (default number of repetition is 2) and /help command response

/repeat command response and text message (number of repetition is 3)

VK bot

Usual text message (default number of repetition is 2) and /help command response

/repeat command response and text message (number of repetition is 5)