telegram-api icon indicating copy to clipboard operation
telegram-api copied to clipboard

What is this error for keyboard ?

Open sallsabil opened this issue 6 years ago • 8 comments

unreal4u/telegram-api v3.4.1 A complete Telegram ...

PHP Version: 7.2

Output:

[Thu Nov 29 07:37:28.746106 2018] [php7:error] [pid 20800] [client 138.201.36.31:46950] PHP Fatal error:  Uncaught TypeError: Argument 1 passed to unreal4u\\TelegramAPI\\Abstracts\\TelegramMethods::formatReplyMarkup() must be an instance of unreal4u\\TelegramAPI\\Abstracts\\TelegramTypes, string given, called in /root/vendor/unreal4u/telegram-api/src/Abstracts/TelegramMethods.php on line 55 and defined in /root/vendor/unreal4u/telegram-api/src/Abstracts/TelegramMethods.php:124\nStack trace:\n#0 /root/vendor/unreal4u/telegram-api/src/Abstracts/TelegramMethods.php(55): unreal4u\\TelegramAPI\\Abstracts\\TelegramMethods->formatReplyMarkup('{"keyboard":[[{...')\n#1 /root/vendor/unreal4u/telegram-api/src/InternalFunctionality/PostOptionsConstructor.php(82): unreal4u\\TelegramAPI\\Abstracts\\TelegramMethods->performSpecialConditions()\n#2 /root/vendor/unreal4u/telegram-api/src/InternalFunctionality/PostOptionsConstructor.php(50): unreal4u\\TelegramAPI\\InternalFunctionality\\PostOptionsConstructor->checkIsMultipart(Object(unreal4u\\TelegramAPI\\Telegram\\Methods\\SendMessage))\n#3 /root/vendor/unreal4u/telegram-api/src/TgLog.php(88): unreal4u\\ in /root/vendor/unreal4u/telegram-api/src/Abstracts/TelegramMethods.php on line 124

When I use this code:

	$sendMessage->chat_id = $chatId;
	$sendMessage->text = "<code>◀️دکمه های اصلی▶️</code>";
	$sendMessage->parse_mode = 'HTML';
	$sendMessage->reply_markup = new \unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup();
	$sendMessage->reply_markup->resize_keyboard = true;
	
	$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
	$keyboardButton->text = '🎤یک';
	$sendMessage->reply_markup->keyboard[0][] = $keyboardButton;
	$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
	$keyboardButton->text = '⛽️دو';
	$sendMessage->reply_markup->keyboard[0][] = $keyboardButton;
	$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
	$keyboardButton->text = '➕سه';
	$sendMessage->reply_markup->keyboard[1][] = $keyboardButton;
	$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
	$keyboardButton->text = '📩چهار';
	$sendMessage->reply_markup->keyboard[1][] = $keyboardButton;
	$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
	$keyboardButton->text = '📘پنج';
	$sendMessage->reply_markup->keyboard[2][] = $keyboardButton;
	$tgLog->performApiRequest($sendMessage);
	$loop->run();

I face the Fatal error above. What is wrong ?

sallsabil avatar Nov 29 '18 07:11 sallsabil

I think this may be an error in the code. I'll look into it tonight ;)

Thanks for reporting this!

unreal4u avatar Nov 29 '18 08:11 unreal4u

Hey I'm waiting

sallsabil avatar Nov 30 '18 07:11 sallsabil

Is the error because of PHP 7.2 ?

sallsabil avatar Dec 03 '18 07:12 sallsabil

Hi, I tested your code and it worked in one go.

image

This isn't a problem of PHP either, as this message was sent using: PHP 7.2.10 (cli) (built: Sep 15 2018 02:33:49)

I don't know how else to try to reproduce it, can you provide some more context? Maybe publishing the entire script?

unreal4u avatar Dec 03 '18 21:12 unreal4u

PD: This was the script I used:

<?php

declare(strict_types = 1);

include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\SendMessage;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));

$sendMessage = new SendMessage();
$sendMessage->chat_id = A_USER_CHAT_ID;
//$sendMessage->chat_id = $chatId;
$sendMessage->text = "<code>◀️دکمه های اصلی▶️</code>";
$sendMessage->parse_mode = 'HTML';
$sendMessage->reply_markup = new \unreal4u\TelegramAPI\Telegram\Types\ReplyKeyboardMarkup();
$sendMessage->reply_markup->resize_keyboard = true;

$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
$keyboardButton->text = '🎤یک';
$sendMessage->reply_markup->keyboard[0][] = $keyboardButton;
$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
$keyboardButton->text = '⛽️دو';
$sendMessage->reply_markup->keyboard[0][] = $keyboardButton;
$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
$keyboardButton->text = '➕سه';
$sendMessage->reply_markup->keyboard[1][] = $keyboardButton;
$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
$keyboardButton->text = '📩چهار';
$sendMessage->reply_markup->keyboard[1][] = $keyboardButton;
$keyboardButton = new \unreal4u\TelegramAPI\Telegram\Types\KeyboardButton();
$keyboardButton->text = '📘پنج';
$sendMessage->reply_markup->keyboard[2][] = $keyboardButton;

$tgLog->performApiRequest($sendMessage);
$promise = $tgLog->performApiRequest($sendMessage);

$promise->then(
    function ($response) {
        echo '<pre>';
        var_dump($response);
        echo '</pre>';
    },
    function (\Exception $exception) {
        // Onoes, an exception occurred...
        echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage();
    }
);

$loop->run();

It is based on this one: https://github.com/unreal4u/telegram-api/blob/master/examples/send-message-with-inlinekeyboard.php

But with your modifications in it, the only thing I did replace was the chatId due to obvious reasons :)

unreal4u avatar Dec 03 '18 21:12 unreal4u

Add this after the above code:

if($chatId == A_USER_CHAT_ID) {
	$sendMessage->chat_id = A_USER_CHAT_ID;
	$sendMessage->text = '%D9%86%D9%87%0A%D8%AF%D8%A7%D8%B1%DB%8C%D9%85+%D8%B3%D8%B1%D9%88%D8%B1%D8%B4';
	$sendMessage->parse_mode = 'HTML';
	$tgLog->performApiRequest($sendMessage);
	$loop->run();
}

I removed this my problem solved!

I discovered If you send message after a message that has keyboard you face that fatal error.

sallsabil avatar Dec 04 '18 09:12 sallsabil

I think that if you initialize a new object you won't have the issue:

if($chatId == A_USER_CHAT_ID) {
    $sendMessage = new SendMessage();
    $sendMessage->chat_id = A_USER_CHAT_ID;
    $sendMessage->text = '%D9%86%D9%87%0A%D8%AF%D8%A7%D8%B1%DB%8C%D9%85+%D8%B3%D8%B1%D9%88%D8%B1%D8%B4';
    $sendMessage->parse_mode = 'HTML';
    $tgLog->performApiRequest($sendMessage);
    $loop->run();
}

unreal4u avatar Dec 04 '18 12:12 unreal4u

No difference

sallsabil avatar Dec 04 '18 12:12 sallsabil