Developed with love by KnpLabs Hire us for your project!
50

TelegramBotBundle

by BoShurik

Symfony Telegram Bot Bundle

TelegramBotBundle

Build Status

Telegram bot bundle on top of telegram-bot/api library

Examples

See example project

Installation

Composer

$ composer require boshurik/telegram-bot-bundle

If you are using symfony/flex all you need is to set TELEGRAM_BOT_TOKEN environment variable

Register the bundle

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new BoShurik\TelegramBotBundle\BoShurikTelegramBotBundle,
    );
    // ...
}

Add routing for webhook

BoShurikTelegramBotBundle:
    resource: "@BoShurikTelegramBotBundle/Resources/config/routing.yml"
    prefix: /_telegram/<some-secret>

Configuration

boshurik_telegram_bot:
    api:
        token: "%telegram_bot_api_token%"
        proxy: "socks5://127.0.0.1:8888"

Usage

API

    $api = $this->container->get(TelegramBot\Api\BotApi::class);

For more info see Usage section in telegram-bot/api library

Getting updates

bin/console telegram:updates

For more information see official documentation

Webhook

Set
bin/console telegram:webhook:set <url> [<path-to-certificate>]
Unset
bin/console telegram:webhook:unset

For more information see official documentation

Async command processing

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

Adding commands

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
yaml
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:
yaml
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

Events

For more complex application (e.g. conversations) you can listen for BoShurik\TelegramBotBundle\Event\UpdateEvent event
php
/**
* @param UpdateEvent $event
*/
public function onUpdate(UpdateEvent $event)
{
$update = $event->getUpdate();
$message = $update->getMessage();
}

Login with Telegram

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.

The MIT License (MIT)

Copyright (c) 2016 - 2020 Alexander Borisov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
  • Fix test
    By BoShurik, 4 months ago
  • Prepare release
    By BoShurik, 4 months ago
  • Command tests
    By BoShurik, 4 months ago
  • More tests
    By BoShurik, 4 months ago
  • Guard fixes
    By BoShurik, 4 months ago
  • symfony/messenger support
    By BoShurik, 4 months ago
  • telegram:webhook:info command
    By BoShurik, 4 months ago
  • CHANGELOG
    By BoShurik, 4 months ago
  • cs-fixer + psalm
    By BoShurik, 4 months ago
  • Remove _telegram_login route
    By BoShurik, 4 months ago
  • Document how to configure Login with Telegram (#28)
    By web-flow, 5 months ago
  • [Feature] Login with Telegram (#25)
    By web-flow, 6 months ago
  • Fix CommandListener
    By BoShurik, 9 months ago
  • Return exit codes
    By BoShurik, 10 months ago
  • Remove deprecated config for bot name
    By BoShurik, 10 months ago
  • Symfony 5 (#22)
    By web-flow, 10 months ago
  • Flex note
    By BoShurik, 1 year ago
  • Prepare release
    By BoShurik, 1 year ago
  • Move example section to the top
    By BoShurik, 1 year ago
  • php7.4 support (#17)
    By web-flow, 1 year ago
  • Remove redundant url check
    By BoShurik, 1 year ago
  • Deprecate boshurik_telegram_bot.name
    By BoShurik, 1 year ago
  • Travis. Allow failures for php 7.4
    By BoShurik, 1 year ago
  • Travis. Allow failures for php 7.4
    By BoShurik, 1 year ago
  • Add lowest deps and php 7.4snapshot
    By BoShurik, 1 year ago
  • Add missing tests
    By BoShurik, 1 year ago
  • PublicCommandInterface
    By BoShurik, 1 year ago
  • Add release date
    By BoShurik, 1 year ago
  • Fix service classes and ids
    By BoShurik, 1 year ago
  • Fix quotes
    By BoShurik, 1 year ago