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
}