Download the PHP package haikiri/messenger-routing without Composer
On this page you can find all versions of the php package haikiri/messenger-routing. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download haikiri/messenger-routing
More information about haikiri/messenger-routing
Files in haikiri/messenger-routing
Package messenger-routing
Short Description MessengerRouting – is a Command Router for messengers, like Telegram and Others. Php8 command handler attributes.
License MIT
Homepage https://github.com/MKC-MKC/MessengerRouter
Informations about the package messenger-routing
Messenger Router
Attribute Commands handler for your messengers, works like Symfony/Routing, or like this: luzrain/telegram-bot-bundle for Symfony.
But I believe this library can be used not only with Telegram, but also with your other messengers!
This code present as an idea, based on attributes introduced in PHP 8 and above.
This library is native, and has no dependencies or bindings with some external libraries.
Hope there will be cool ideas, fixes, and implementations based on this project idea!
If you need to make any changes, write to the contact details in composer.json.
Installation
Requirements:
- Composer 2+
- PHP >= 8.0
ext-mbstring
Theory, how it works:
- Create a messenger controller
- Create a contract that implements the interface
\Haikiri\MessengerRouting\MessengerContractInterface - Fill this contract with methods from the interface.
- Check which type of message you will handle, if needed:
["message", "edited_message", ...etc]- After checking, create a handler class that will store commands.
Your new handler must inherit from CommandsRouter. Like in this example:
class MyMessengerCommands extends \Haikiri\MessengerRouting\CommandsRouter
- After checking, create a handler class that will store commands.
Your new handler must inherit from CommandsRouter. Like in this example:
- Make your commands based on the example below.
- Create a contract that implements the interface
See examples in
tests/Mocked.
Errors
Please note, if no command has been found, you can catch this discrepancy and implement your behavior. This will not cause an exception if the command is not found. The code will continue to work.
Обрати внимание, если ни одна команда не была найдена, ты можешь отловить это расхождение и реализовать своё поведение. Это не вызовет исключение, если команда не найдена. Код продолжит работу.
Access rights
Your controller supports access rights restrictions. These rights are implemented by you in your model.
This means that now you have separation between administrator, chat owner, and environment administrators.
Hierarchy of rights:
- ENV admins – like the
rootuser in Linux. For example, they can disable the bot in any chat with this bot. - Chat creator can do almost the same but only within their chat.
- Administrator can execute all commands intended only for their group.
This means that an admin cannot execute commands specific for the chat creator or environment administrator.
At the same time, the environment administrator can do everything that others cannot.
It expected that the list of admin’s IDs will be listed in your project's .ENV file as a string, separated by commas.
However, you can implement your own method for getting the list of admins in your contract.
Usage
I recommend looking at the implementation example in the
tests/Mocked.
Structure:
- /src
- /Controller – Your web pages (Symfony) are stored here.
- /Messenger – Everything related to messengers will be here.
First, let's make your entrypoint. A Webhook will be configured on this page.
/src/Controller/ApiController.php
If you are use Symfony:
Symfony Controller Usage
If you are use Symfony, try for use Symfony Tags... Something like this:
Now let's create a Telegram Messenger class that will process your Telegram requests. Connect your Telegram SDK in the constructor. Please, use your real library! This is just an example!
/src/Messenger/TelegramMessenger.php
If you are use Symfony:
If you are not use Symfony:
Write the extends TelegramContract and create an abstract class, where we'll write the data for your command handler.
Connecting your interface from the new library \Haikiri\MessengerRouting\MessengerContractInterface:
/src/Messenger/TelegramContract.php
After creating all the main classes, u will make a trial command handler in private messages with the bot. See example for PrivateRoom.php:
/src/Messenger/Telegram/PrivateRoom.php
Note that your bot accepts commands without special characters such as "/" by default. The short "/command" is equivalent to "command", and "!command" as a "command". etc...
Small example.
When you pass: ["ban", "бан"]
Can be triggered on: ban, /ban, !ban, ban!, Ban!, Ban?, Ban!!!!!!, !BaN! etc...
Allow this in the settings inside BotFather.
All versions of messenger-routing with dependencies
ext-mbstring Version *