/telegram-silex-provider

A Silex Provider for the Telegram Bot API SDK

Primary LanguagePHPMIT LicenseMIT

Telegram PHP SDK Silex Service Provider

Build status

A Silex service provider to integrate the Telegram PHP SDK

composer require ahoulgrave/silex-tg-service-provider dev-master
<?php
use Telegram\Bot\Silex\Provider\TelegramServiceProvider;

$app = new Silex\Application();

$app->register(new TelegramServiceProvider(), [
    'telegram.bot_api' => '<Your bot api token>',
    'telegram.commands' => [
        \My\Telegram\Command\AwesomeCommand::class,
    ]
]);

If your are using a webhook to fetch the updates, you can register the Controller Provider to handle the request for you.

$app->mount('/telegram-web-hook', new TelegramControllerProvider());

Your webhook should be https://youdomain.com/telegram-web-hook/ (Note the trailing slash)

Now, when telegram sends you the updates, the controller will look for the right command and handle it.

You can extend the Telegram\Bot\Silex\ApplicationAwareCommand class, so your command can access the container.

For example:

<?php
namespace My\Telegram\Command;

use Telegram\Bot\Silex\ApplicationAwareCommand;

class AwesomeCommand extends ApplicationAwareCommand
{
    /**
     * @inheritdoc
     */
    protected $name = 'hello';

    /**
     * @inheritdoc
     */
    public function handle($arguments)
    {
        $update = $this->getUpdate();
        $app = $this->getApplication();
        $app['monolog']->info($update->getMessage()->getText());
        $this->replyWithMessage(['text' => 'Hi there!']);
    }
}

Remember to register all of your commands (see Usage)

Read this for more details on the commands system

(The MIT License)

Copyright (C) 2016 by Agustin Houlgrave