jungle-bay/telegram-bot-controller-provider

This package is abandoned and no longer maintained. No replacement package was suggested.

Telegram Bot Controller Provider for Silex

1.1 2018-03-18 07:46 UTC

This package is not auto-updated.

Last update: 2024-01-08 21:28:24 UTC


README

Telegram Bot Controller Provider Logo

Telegram Bot Controller Provider for Silex

Travis CI Scrutinizer CI Codecov

Install

The recommended way to install is through Composer:

composer require jungle-bay/telegram-bot-controller-provider

The simplest example of use

<?php

require_once __DIR__ . DIRECTORY_SEPARATOR . implode(DIRECTORY_SEPARATOR, array('vendor', 'autoload.php'));

$app = new \Silex\Application();

$app->mount('/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11', new \Silex\Provider\TelegramBotControllerProvider(array(
    'token'    => '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11',                 // Your token bot.
    'storage'  => $adapter,                                                    // This adapter for Scrapbook library to store user sessions. See the complete adapters: https://github.com/matthiasmullie/scrapbook#adapters
    'mappings' => array(
        'default'       => \Acme\Bot\Commands\DefaultCmd::class,               // This command will work by default if no command is found or user session. (optional)
        'inline_query'  => \Acme\Bot\Commands\FeedbackInlineQueryCmd::class,   // This command will work with inline queries. (optional)
        'commands'      => array(                                              // This is the list of registered commands for the bot. (optional)
            'help' => \Acme\Bot\Commands\HelpCmd::class,
            'user' => \Acme\Bot\Commands\UserCmd::class
        )
    )
)));

$app->run();
Example implement command
<?php

namespace Acme\Bot\Commands;


use TelegramBotAPI\Types\Update;
use TelegramBotPlatform\TelegramBotPlatform;
use TelegramBotPlatform\Api\TelegramBotCommandInterface;

class DefaultCmd implements TelegramBotCommandInterface {

    /**
     * {@inheritdoc}
     */
    public function execute(TelegramBotPlatform $tbp, Update $update, $payload = null) {
        
        if (null === $update->getMessage()) return false;

        $tbp->getTelegramBotAPI()->sendMessage(array(
            'chat_id' => $update->getMessage()->getChat()->getId(),
            'text'    => 'Default Cmd ;)'
        ));
        
        return true;
    }
}

Warning

Do not forget to install webhook! Example url webhook:

https://www.example.com/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/

Remember that webhook will only work on a HTTPS connection and method getUpdates not work when include webhook.

Note

For the convenience of development, you can use Telegram Bot CLI.

License

This bundle is under the MIT license. See the complete license in the file: here.