dev-master
9999999-dev https://github.com/sloothword/bot-chassisStreamline your Telegram bot development
MIT
The Requires
The Development Requires
by sloothword
laravel telegram telegram bot telegram php laravel telegram
Streamline your Telegram bot development
WARNING: Currently in ALPHA state due to: - quickstart not tested and controller.md not finished - the telegram-bot-sdk currently used is no stable version - test coverage is low (but slowly growing) - integration options for plain php are lacking - no argument validation of Chassis API, (*1)
Adds a fully featured layer upon the basic telegram API (telegram-bot-sdk) to drastically speed up telegram bot development, (*2)
The telegram-bot-sdk offers a stable API layer for bot development. However, for a fully featured bot many aspects are missing. Instead of jumping into defining the behaviour of the bot, you first need some routing system to handle arbitrary updates, a system to keep track of user conversations and persist associated data, and so on. This package aims to implement this middle layer and enables you to go straight for the fun part!, (*3)
Builds upon telegram-bot-sdk and makes all features easily accessible, (*4)
Use controller to easily handle any incoming updates. E.g, (*5)
/text
)Use simple but powerful configuration to wire up and chain Controller, (*6)
Integrate with Laravel (and Redis as Backend) out of the box (see below for upcoming options), (*7)
Out-of-the-box persistent storage of metadata, (*8)
See below for a quickstart tutorial, (*9)
Installation methods, configuration options, framework integration and telegram-bot-sdk integration., (*10)
Routing of updates to Controller, Controller features, persistent MetaData storage, (*11)
A bot can only do as much as is defined in the official telegram bot API., (*12)
bot-chassis is build upon telegram-bot-sdk with all features supported. Check the Migration Guide if you are coming from telegram-bot-sdk., (*13)
For all these you can use the issue tracker., (*14)
You could also find me and other bot developers at Slack PHP Chat (Direct message irazasyed for access to #telegram-bot-sdk), (*15)
Pull requests are most appreciated if they use PSR-2 Coding Standards, have tests and can be pulled from a feature-branch., (*16)
This tutorial uses Laravel and Redis and introduces the basic features Controller
and MetaData
., (*17)
For more alternatives check the integration guide [Configuration]., (*18)
Install PHP7, Laravel and Redis. Configure Laravel to use your database and redis instance., (*19)
Tell composer to use the telegram-bot-sdk
with dev
stability (needed until we can use a stable version):, (*20)
composer require irazasyed/telegram-bot-sdk:@dev
Add bot-chassis
to the composer.json:, (*21)
composer require sloothword/bot-chassis
Add the LaravelServiceProvider in the app.php:, (*22)
'providers' => [ ... Chassis\Laravel\ChassisServiceProvider::class,
Publish the configuration (and migrations), (*23)
artisan vendor:publish
Open config/chassis.php
and insert your bot credentials., (*24)
Send a command to the bot (commands /echo
, /delayed
, /double
, see Chassis/Controller/EchoController.php), then call artisan chassis:handle
to process it., (*25)
This tutorial bot will have three functions:
- The bot adds every integer messages in the chat.
- /reset
sets the counter back to 0
- /set
sets the counter to the next sent integer, (*26)
Create a new Controller under your application namespace and structure., (*27)
<?php namespace App\Controller; use Chassis\Controller\Controller; class CountController extends Controller { public function add(){ // Add sent number to counter } public function reset(){ // Reset the counter } public function set(){ // Set the counter to the next integer message } }
and add it to the controller list in chassis.php (and remove the others):, (*28)
'bots' => [ 'mybot' => [ 'username' => 'BOT-USERNAME', 'token' => env('TELEGRAM_BOT_TOKEN', 'BOT-TOKEN'), 'shared' => [], 'controllers' => [ ['text', App\Controller\CountController::class, 'add'], ['/reset', App\Controller\CountController::class, 'reset'], ['/set', App\Controller\CountController::class, 'set'] ], ], ],
Now everytime you send the /reset
command to the Bot the reset()
method of CountController
is called. Everytime we send text to the bot (which is no command) the add()
method is called., (*29)
The MetaData you get by calling $this->getConversationData()
(shortcut for $this->getMetaData($this->getUpdate()
) in any Controller is persisted by the Chassis framework and linked to the current conversation. Using this here means that each user and chat has its own counter., (*30)
We could also use the MetaData object linked to the user ($this->getMetaData($this->getUser())
) to get each user a single counter across all chats. With $this->getMetaData($this->getChat())
all users of a chat could count together., (*31)
class CountController extends Controller { public function add(){ $value = intval($this->getText()); $conversationData = $this->getConversationData(); $counter = $conversationData->get('count', 0); $conversationData['count'] = $counter + $value; $this->replyWithMessage(['text' => 'New value: ' .$conversationData['count']]); } public function reset(){ $this->getConversationData()->forget('count'); $this->replyWithMessage(['text' => 'Reset counter']); } }
Notes:
- You do not need to be concerned about saving back the changed MetaData as it will be done automatically.
- MetaData extends Illuminate/Support/Collection (see get()
, has()
and forget()
methods).
- $this->getText()
is a shortcut for $this->getUpdate()->getMessage()->getText()
- Controller uses the Telegram\Bot\Answers\Answerable
trait, so you can use all the reply*
methods, (*32)
For the /set
command we need to do three things:
- Instruct the user to message the new count
- Define which controller method should handle the response
- In that method set the counter to the message text, (*33)
public function set(){ $this->replyWithMessage(['text' => 'Insert new value:']); $this->getConversationData()->nextConversationStep(self::class, 'saveCounter'); } public function saveCounter(){ $value = intval($this->getText()); $conversationData = $this->getConversationData(); $conversationData['count'] = $value; $this->replyWithMessage(['text' => 'Set counter to ' .$value]); $conversationData->clearConversation(); }
Pay attention to the last line: as we set the next handling method to saveCounter()
the bot will always call that same method untill we tell him not to ->clearConversation()
., (*34)
Note: This behaviour will change soon., (*35)
Play around with your finished bot., (*36)
Also try artisan chassis:handle --loop
to continously run your bot., (*37)
For more features and detailed information check out the documentation Documentation., (*38)
Streamline your Telegram bot development
MIT
laravel telegram telegram bot telegram php laravel telegram