example-bot
example-bot copied to clipboard
can I instantiate Redis in CallbackqueryCommand.php...
I instantiate Redis in CallbackqueryCommand and want to write the data of CallbackqueryCommand into the list. But I don't know what went wrong
@XiaomiYe Please add some code and more context, otherwise nobody can help :blush:
@noplanman
CallbackqueryCommand.php
/**
* Main command execution
* @return ServerResponse
* @throws \Exception
*/
public function execute(): ServerResponse
{
$callback_query = $this->getCallbackQuery();
$callback_data = $callback_query->getData();
$data['inline_message_id'] = $callback_query->getId();
//$callback_query->getInlineMessageId();
$data['keywords'] = $callback_data;
TelegramLog::debug(json_encode($data));
$redisobj = new Redis();
TelegramLog::debug("instantiate Redis");
try {
$connect = $redisobj->pconnect('127.0.0.1', 6379);
TelegramLog::debug(json_encode($connect));
} catch (Exception $e) {
TelegramLog::error(json_encode($e->getMessage()));
}
$redisobj->lpush('bot-list', $data);
return Request::emptyResponse();
}
In your redis-cli you can use the monitor
command if the value comes through properly:
$ redis-cli
127.0.0.1:6379> monitor
OK
1663558774.728972 [0 127.0.0.1:37318] "LPUSH" "bot-list" "Array"
Your code should work, but you could try this:
-
->connect
instead of->pconnect
-
->lPush
instead of->lpush
- log the redis response
$redisobj = new Redis();
TelegramLog::debug('instantiate Redis');
try {
$connect = $redisobj->connect('127.0.0.1', 6379);
TelegramLog::debug($connect);
$result = $redisobj->lPush('bot-list', $data);
TelegramLog::debug($result);
} catch (Exception $e) {
TelegramLog::error($e->getMessage());
}
Thank you. I have found the problem and solved it
Feel free to share the problem and your solution, so others (and me!) can learn, thanks :smiley:
just Replace with $redisobj=new \Redis();