telegram-api
telegram-api copied to clipboard
What is this error for keyboard ?
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 ?
I think this may be an error in the code. I'll look into it tonight ;)
Thanks for reporting this!
Hey I'm waiting
Is the error because of PHP 7.2 ?
Hi, I tested your code and it worked in one go.
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?
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 :)
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.
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();
}
No difference