rgzone/telegram-bot-bundle

Symfony bundle to inject the most popular Telegram Bot API implementations

0.0.1 2020-11-15 20:54 UTC

This package is auto-updated.

Last update: 2021-11-16 00:14:58 UTC


README

Symfony Bundle to autowire most used Telegram Bot API implementations.

Table of Contents

Install

$ composer require rgzone/telegram-bot-bundle
<?php // config/bundles.php

return [
    // ...
    RGZ\TelegramBotBundle\RGZTelegramBundle::class => ['all' => true],
];

Configure

# config/packages/rgz_telegram.yaml

rgz_telegram:
    bot:
        name: bot
        token: '%env(TG_TOKEN)%'
# config/packages/rgz_telegram.yaml

rgz_telegram:
    default_bot: default
    bots:
        main:
            token: '%env(TG_TOKEN_MAIN)%'
        default:
            token: '%env(TG_TOKEN_DEFAULT)%'
        logger:
            token: '%env(TG_TOKEN_LOGGER)%'

Suggested packages

Usage

Install suggestions

$ composer require telegram-bot/api:^2.3
$ composer require irazasyed/telegram-bot-sdk:^3.3
$ composer require longman/telegram-bot:0.64.*
$ bin/console debug:autowiring --all | grep Telegram

 Longman\TelegramBot\Telegram (rgzone.telegram_bot.any_name.bot_lng)
 Longman\TelegramBot\Telegram $anyName (rgzone.telegram_bot.any_name.bot_lng)
 Longman\TelegramBot\Telegram $botMain (rgzone.telegram_bot.bot_main.bot_lng)
 Longman\TelegramBot\Telegram $logger (rgzone.telegram_bot.logger.bot_lng)
 TelegramBot\Api\BotApi (rgzone.telegram_bot.any_name.bot_api)
 TelegramBot\Api\BotApi $anyName (rgzone.telegram_bot.any_name.bot_api)
 TelegramBot\Api\BotApi $botMain (rgzone.telegram_bot.bot_main.bot_api)
 TelegramBot\Api\BotApi $logger (rgzone.telegram_bot.logger.bot_api)
 Telegram\Bot\Api (rgzone.telegram_bot.any_name.bot_sdk)
 Telegram\Bot\Api $anyName (rgzone.telegram_bot.any_name.bot_sdk)
 Telegram\Bot\Api $botMain (rgzone.telegram_bot.bot_main.bot_sdk)
 Telegram\Bot\Api $logger (rgzone.telegram_bot.logger.bot_sdk)
# config/services.yaml

services:
    _defaults:
        bind:
            $testChatId: '%env(TG_CHAT_ID_TEST)%'
<?php // src/Command/TestCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

use TelegramBot\Api\BotApi;
use Telegram\Bot\Api;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\Request;

class TestCommand extends Command
{
    protected static $defaultName = 'app:test';

    protected BotApi $defaultBot;
    protected Api $firstBot;
    protected Telegram $secondBot;
    protected int $testChatId;

    public function __construct(BotApi $randomName, Api $botMain, Telegram $logger, int $testChatId)
    {
        parent::__construct();
        $this->defaultBot = $randomName;
        $this->firstBot = $botMain;
        $this->secondBot = $logger;
        $this->testChatId = $testChatId;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $this->defaultBot->sendMessage(
            $this->testChatId,
            'Delivered by "<b>telegram-bot/api</b>"',
            'HTML'
        );

        $this->firstBot->sendMessage([
            'chat_id' => $this->testChatId,
            'text' => 'Delivered by "<b>irazasyed/telegram-bot-sdk</b>"',
            'parse_mode' => 'HTML',
        ]);

        Request::initialize($this->secondBot);
        Request::sendMessage([
            'chat_id' => $this->testChatId,
            'text' => 'Delivered by "<b>longman/telegram-bot</b>"',
            'parse_mode' => 'HTML',
        ]);

        return Command::SUCCESS;
    }
}