StatefulBot is a PHP framework to easily create a Telegram Bot. This framework is specifically built to solve the state management problems!
Through the wiki you'll find out how to use it, some basic architectural aspects and all the implementation specifications.
You'll finally understand how to use the framework to easily create complex Telegram Bots.
Here an example of a Telegram Bot project made using StatefulBot.
When you are dealing with Telegram Bot development, you notice that each bot request is stateless, so every request is new to the Telegram Bot webhook. If you want to create a complex procedure in your Telegram Bot, you need to handle it manually, checking that each request is in the right moment of procedure and responding accordingly, changing the state.
To give a practical example, imagine you want to develop a Telegram Bot that asks you for your name, your nickname and your email address, in this specific order, one by one.
Starting from a /start
command, the bot sends you the message Send your name
, so you send your name Giuseppe
. When you send your name, the hook basically doesn't know that you are in this specific procedure, but it sees every command as equally new. So you need to keep the state of your procedure and check it every time a message (or a request, more generally) is sent to the bot, to check if the message sent is consistent with the specific state of the bot, and then updating the state.
Implementing this logic manually can be challenging, but with this framework the main aspects are handled automatically. You only need to follow some basic rules (explained in the wiki)
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.php
file and fill theconfig.json
file 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 of the wiki), 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' {} +