-1

I need to make the data printed by this method be stored in a table called bot_events. The method successfully prints the bot’s events, but I need the data to be saved in the table. Here is an example of the data that the bot prints:

{
    "ok": true,
    "result": [
        {
            "update_id": 268191029,
            "message": {
                "message_id": 1,
                "from": {
                    "id": 12345,
                    "is_bot": false,
                    "first_name": "Kaiser0112",
                    "language_code": "es"
                },
                "chat": {
                    "id": 5204194546,
                    "first_name": "Kaiser0112",
                    "type": "private"
                },
                "date": 1692238373,
                "text": "/start",
                "entities": [
                    {
                        "offset": 0,
                        "length": 6,
                        "type": "bot_command"
                    }
                ]
            }
        },
        {
            "update_id": 268191030,
            "message": {
                "message_id": 2,
                "from": {
                    "id": 12345,
                    "is_bot": false,
                    "first_name": "Kaiser0112",
                    "language_code": "es"
                },
                "chat": {
                    "id": 5204194546,
                    "first_name": "Kaiser0112",
                    "type": "private"
                },
                "date": 1692239193,
                "text": "Hello"
            }
        },
       

Here is the method I was trying to use:

public function getUpdates()
    {
        $updates = $this->telegramService->getUpdates();
        foreach ($updates as $update) {
        BotEvent::create([
            'update_id' => $update['update_id'],
            'message_id' => $update['message']['message_id'],
            'chat_id' => $update['message']['chat']['id'],
            'user_id' => $update['message']['from']['id'],
            'event_type' => 'message',
            'event_data' => json_encode($update),
        ]);
    }
        return response()->json($updates);
        

    }

This is the error it throws when I try to enter the data into the table:

{
    "errosr": [],
    "exception": "ErrorException",
    "file": "/var/www/html/api-laravel-10/app/Http/Controllers/TelegramTecnosystemController.php",
    "line": 22,
    "message": "Trying to access array offset on value of type bool",
    "statuscode": 500
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    If `$updates` is the full json above, then the first key/value is `"ok": true,`. You really want to iterate through `$updates['result']` – aynber Aug 17 '23 at 19:12
  • Firstly decode json to array $updates = json_decode($updates, true) then in loop foreach($updates[“result”] as $update) //do rest – Sumit kumar Aug 17 '23 at 19:19

0 Answers0