Download the PHP package arodu/tebo without Composer
On this page you can find all versions of the php package arodu/tebo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package tebo
TeBo: CakePHP plugin for Telegram Bot
TeBo is a plugin that integrates a Telegram bot into CakePHP 5 applications, allowing configuration and management of custom commands with an easy setup.
Installation
-
Install the plugin using Composer:
-
Load the plugin in your project:
- Add your Telegram bot token to the
.envfile, or set it as an environment variable:
Webhook Configuration
TeBo offers commands to manage the webhook setup in Telegram. To run these commands, use:
The available options are:
- Get Webhook URL: Displays the current webhook URL configured on the local system.
- Set Webhook to telegram: Sets the webhook on Telegram, linking the bot to a specific URL to receive updates.
- Delete Webhook from telegram: Deletes the webhook from Telegram, stopping the bot from receiving updates.
- Get Webhook info from telegram: Shows information about the webhook configured on Telegram, including status and connection details.
- Get bot info: Displays information about the bot, including the bot's name, username, and ID.
Additional Configuration (Optional)
You can add the following values to the .env file to enhance webhook functionality:
- WEBHOOK_OBFUSCATION: Obfuscates the webhook URL, adding an additional security layer.
- WEBHOOK_BASE: Sets the base domain for the webhook URL. If not specified,
127.0.0.1is used, which is incompatible with the Telegram API.
Configuring CSRF Protection
CakePHP includes built-in protection against Cross-Site Request Forgery (CSRF) attacks. However, to allow Telegram webhooks to work properly with TeBo, you need to exclude requests coming from the plugin from this protection.
To do this, adjust the middleware configuration in the src/Application.php file. Modify the middleware method to include the following logic:
[!WARNING] Why Is This Necessary? Telegram cannot send custom headers (such as CSRF tokens), which would cause webhook requests to be rejected if CSRF protection is enabled. By configuring this exclusion, we allow Telegram to interact with our application without compromising the overall security.
Bot Testing
Once the webhook and token are configured, the bot should be ready to work. You can test it on Telegram using the following commands:
/start/hello/about
Customization
To customize the bot's options, you can create a configuration file in config/tebo.php with the following structure:
[!NOTE] You can find more information about this file on
config/tebo.phpin the plugin's directory.
Usage
The plugin provides a default Action that can be extended to create custom actions. To create a new action, follow these steps:
- Create a new command class in the
src/Actionsdirectory. - Extend from
\TeBo\Action\Actionor Implement the\TeBo\Action\ActionInterfaceinterface. - Add the command to the
config/tebo.phpfile.
To add the command to the configuration file config/tebo.php, use the following code:
[!NOTE] After adding the command, you can test it by sending
/pricesto the bot.
Bake a New Action
Also, you can bake a new action using the following command:
Send an HTML Formatted Message
To send a message with HTML formatting, use HtmlMessage:
[!NOTE] In this example, HTML tags such as
<b>,<i>,<a>, and<code>are supported for text formatting.
Send a Photo
-
Example 1: Send a Local Photo
If the image is stored locally, use the path to the image file: - Example 2: Send a Photo from a URL with a Caption
You can also send a photo from a URL with a custom caption:
Sending with a chat ID
To send a message to a specific chat ID, use the following code:
Notes
- Ensure that the
.envfile is properly configured before testing the bot. - The default command executed by the plugin is
\TeBo\Action\Command\NotFoundAction::class. You can disable it by setting it tonull.
License
The MIT License (MIT). Please see License File for more information.