Skip to content

Problem with buttons #782

Closed
Closed
@sergey89d

Description

@sergey89d

Required Information

  • Operating system: ubuntu 17.10
  • PHP version: php7.1-fpm
  • PHP Telegram Bot version: 0.52
  • Using MySQL database: yes
  • MySQL version: 5.7
  • Update Method: Webhook
  • Self-signed certificate: yes

What are the options for solving the problem?

My currency can be added or removed and I do not know number of currencies

Error

[2018-02-16 20:22:22] bot_log.ERROR: Telegram\Bot\Exception\TelegramException: You must add some text to the button! in /var/www/site/src/Entities/KeyboardButton.php:59

This is code not working

 $data[ 'reply_markup' ] =  (new Keyboard(Currency::getArrayCurrency('output'))) // not working
                        ->setResizeKeyboard(true)
                        ->setOneTimeKeyboard(true)
                        ->setSelective(true);

OR

$output = Currency::getArrayCurrency('output');
$data[ 'reply_markup' ] =  (new Keyboard($output)) // not working
                        ->setResizeKeyboard(true)
                        ->setOneTimeKeyboard(true)
                        ->setSelective(true);

Using class

Class Currency {
    public static function getArrayCurrency(string $key): array
    {
        $currency = self::includeCurrency();

        $indexArray= [];
        $keyboardButtons = [];

        $i = 0;
        $j = 0;

        foreach ($currency[ $key ] as $k => $value) {

            $indexArray[] = $value;

        }

        $countEllements = count($indexArray);

        foreach ($indexArray as $index => $v) {

            if ($countEllements <= $j) {
                continue;
            }

            $keyboardButtons[ $i ][ 0 ] = ['text' => $indexArray[ $j++ ]];

            if ($countEllements <= $j) {
                continue;
            }
            $keyboardButtons[ $i ][ 1 ] = ['text' => $indexArray[ $j++ ]];

            $i++;
        }
        return $keyboardButtons;
    }

    protected static function includeCurrency(): array
    {
        if (file_exists(self::$curencyFile)) {
            $currency = include self::$curencyFile;
        } else {
            throw new FileNotFoundException("Currency file not found!");
        }

        return $currency;
    }
}

Currency Array

<?php
return [
    'output' => [
        'wmz' => "WMZ",
        'wmr' => "WMR",
        'qiwi' => "QIWI RUB",
        'yandex' => "YANDEX",
        'uzcard' => "UZCARD UZS",
    ],
    'entry' => [
        'wmz' => "WMZ",
        'wmr' => "WMR",
        'qiwi' => "QIWI RUB",
        'yandex' => "YANDEX",
        'uzcard' => "UZCARD UZS",
        'paynet' => "PAYNET"
    ]
];

Telegram Log

[2018-02-16 21:36:55] bot_log.DEBUG: OUTPUT: array (
0 =>
array (
0 =>
array (
'text' => 'WMZ',
),
1 =>
array (
'text' => 'WMR',
),
),
1 =>
array (
0 =>
array (
'text' => 'QIWI RUB',
),
1 =>
array (
'text' => 'YANDEX',
),
),
2 =>
array (
0 =>
array (
'text' => 'UZCARD UZS',
),
),
)

If you register a static array it works.
For example

 $data[ 'reply_markup' ] =  (new Keyboard(['A', 'B', 'C'])) //working
                        ->setResizeKeyboard(true)
                        ->setOneTimeKeyboard(true)
                        ->setSelective(true);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions