ahoulgrave / silex-tg-service-provider
Silex Service Provider for the Telegram SDK
Requires
- irazasyed/telegram-bot-sdk: ^2.1
- silex/silex: ~2.0
Requires (Dev)
- phpunit/phpunit: 5.5.*
- symfony/browser-kit: ^3.1
This package is not auto-updated.
Last update: 2024-11-09 19:56:28 UTC
README
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