Telegram bot bundle on top of telegram-bot/api
library
See example project
$ composer require boshurik/telegram-bot-bundle
If you are using symfony/flex all you need is to set TELEGRAM_BOT_TOKEN
environment variable
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new BoShurik\TelegramBotBundle\BoShurikTelegramBotBundle,
);
// ...
}
BoShurikTelegramBotBundle:
resource: "@BoShurikTelegramBotBundle/Resources/config/routing.yml"
prefix: /_telegram/<some-secret>
boshurik_telegram_bot:
api:
token: "%telegram_bot_api_token%"
proxy: "socks5://127.0.0.1:8888"
$api = $this->container->get(TelegramBot\Api\BotApi::class);
For more info see Usage section in telegram-bot/api
library
bin/console telegram:updates
For more information see official documentation
bin/console telegram:webhook:set <url> [<path-to-certificate>]
bin/console telegram:webhook:unset
For more information see official documentation
To improve performance, you can leverage Messenger to process webhooks later via a Messenger transport.
composer req symfony/messenger
# config/packages/messenger.yaml
framework:
messenger:
transports:
async: "%env(MESSENGER_TRANSPORT_DSN)%"
routing:
'BoShurik\TelegramBotBundle\Messenger\TelegramMessage': async
Commands must implement \BoShurik\TelegramBotBundle\Telegram\Command\CommandInterface
There is \BoShurik\TelegramBotBundle\Telegram\Command\AbstractCommand
you can start with
To register command: add tag boshurik_telegram_bot.command
to service definition
app.telegram.command:
class: AppBundle\Telegram\Command\SomeCommand
tags:
- { name: boshurik_telegram_bot.command }
If you use autoconfigure
tag will be added automatically
There is predefined \BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand
. You need to register it:
app.telegram.command.help:
class: BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand
arguments:
- '@BoShurik\TelegramBotBundle\Telegram\Command\CommandRegistry'
tags:
- { name: boshurik_telegram_bot.command }
It displays commands which additionally implement \BoShurik\TelegramBotBundle\Telegram\Command\PublicCommandInterface
For more complex application (e.g. conversations) you can listen for BoShurik\TelegramBotBundle\Event\UpdateEvent
event
/**
* @param UpdateEvent $event
*/
public function onUpdate(UpdateEvent $event)
{
$update = $event->getUpdate();
$message = $update->getMessage();
}
This bundle supports login through Telegram Api
If you want allow your Bot's users to login without requiring them to register again follow these instructions.