example-bot icon indicating copy to clipboard operation
example-bot copied to clipboard

can I instantiate Redis in CallbackqueryCommand.php...

Open XiaomiYe opened this issue 2 years ago • 3 comments

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 avatar Sep 16 '22 10:09 XiaomiYe

@XiaomiYe Please add some code and more context, otherwise nobody can help :blush:

noplanman avatar Sep 18 '22 01:09 noplanman

@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();
    }

XiaomiYe avatar Sep 19 '22 01:09 XiaomiYe

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());
}

noplanman avatar Sep 19 '22 03:09 noplanman

Thank you. I have found the problem and solved it

XiaomiYe avatar Sep 26 '22 09:09 XiaomiYe

Feel free to share the problem and your solution, so others (and me!) can learn, thanks :smiley:

noplanman avatar Sep 26 '22 10:09 noplanman

just Replace with $redisobj=new \Redis();

XiaomiYe avatar Sep 26 '22 10:09 XiaomiYe