-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started
The steps to get this framework ready and to start developing your Telegram Bot are:
- Download and install PHP and Composer. You'll use Composer to eventually update packages.
- Clone this repository on your server (or locally) with the following command:
git clone https://github.com/giuseppetrivi/StatefulBot-framework.git- Create a Telegram Bot via @BotFather (here is a simple guide).
- Set the webhook to the
hook.phpfile and fill theconfig.jsonfile with necessary information (like Telegram Bot API token). - Before making the first changes, choose your Telegram Bot name and execute the following command (using Git Bash) to change the main namespace name (better explained in Chapter 3), putting it in the place of
<HERE_YOUR_BOT_NAME>:
cd /path/to/project/
find . -type f -name "*.php" -exec sed -i 's/CustomBotName/<HERE_YOUR_BOT_NAME>/g' {} +The Composer packages in this framework are the following:
-
telegram-bot-sdk: a PHP Telegram Bot SDK to handle the requests from/to Telegram Bot -
meekro: a PHP library to easily interface with database and SQL queries -
phpunit: a PHP framework for writing tests and a command-line tool for running these tests
The code style followed in the files of this project (except for the external library classes) is the following:
| TYPE | STYLE |
|---|---|
| class instance variable | $_InstanceName |
| class definition | ClassName |
| attribute or variable | snake_case_var |
| method or function | camelCaseFun |
| constants | SNAKE_UPPERCASE_CONST |
The architecture of the framework is shown in the following diagram and resembles an MVC (Model-View-Controller). The three white squares are the three architectural main components: the controller (in the middle) calls the view (left) to show the "UI" and calls the entities of model (right) to use data from database, Telegram Bot API service and any other external service (low, outside the box).
The ConfigurationHandler class is where the configuration properties are handled (Chapter 5) and the Init class is the class where the request's processing starts (Chapter 4).
These are the main folders of the framework project:
-
config/contains configuration files and a class to handle it -
control/contains classes that handles the logic of the state management of the bot -
docs/contains some useful documents about the project [not necessary] -
entities/contains classes that communicate with external services (like APIs or a database), or classes that implements some specific objects -
exceptions/contains custom exceptions -
init/contains theInitclass, where the request's processing starts -
logs/is a folder to store logs -
vendor/contains Composer's downloaded packages -
view/contains classes to handle the messages shown to the final user on the bot and everything about the "UI" of the bot (messages, emoticons, buttons, etc...)
You can read the folders entities/, view/ and control/processes/ respectively as the MVC folders.
Instead, these are the files in the main folder of the framework project:
-
hook.phpis the file to be set as Telegram Bot webhook and is the first file to be called for each bot's request -
file_to_test_snippets.phpis a simple file to test the server or some specific functionality [not necessary] -
composer.jsonandcomposer.lockcontain the configurations of Composer packages -
project_autoloader.phpis the autoloader file that sets up the autoloading rules using the classPsr4AutoloaderClass, which follows the PSR-4 standard