ahoulgrave/silex-tg-service-provider

There is no license information available for the latest version (dev-master) of this package.

Silex Service Provider for the Telegram SDK

dev-master 2016-11-18 23:45 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:02:38 UTC


README

Build status

Overview

A Silex service provider to integrate the Telegram PHP SDK

Requirements

Installation

composer require ahoulgrave/silex-tg-service-provider dev-master

Usage

<?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,
    ]
]);

Webhook

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.

Commands

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

More information

License

(The MIT License)

Copyright (C) 2016 by Agustin Houlgrave