-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CollectChat.php
51 lines (40 loc) · 1.26 KB
/
CollectChat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace App\Telegram\Middleware;
use App\Models\Chat;
use Illuminate\Support\Facades\DB;
use Sentry\State\Scope;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Properties\ChatType;
use function Sentry\configureScope;
class CollectChat
{
public function __invoke(Nutgram $bot, $next): void
{
$user = $bot->user();
if ($user === null) {
return;
}
$chat = DB::transaction(function () use ($user, $bot) {
//save or update chat
$chat = Chat::updateOrCreate([
'chat_id' => $user->id,
], [
'first_name' => $user->first_name,
'last_name' => $user->last_name,
'username' => $user->username,
'language_code' => $user->language_code,
'blocked_at' => null,
]);
if ($chat->started_at === null && $bot->message()?->chat?->type === ChatType::PRIVATE) {
$chat->started_at = now();
$chat->save();
}
return $chat;
});
$bot->set(Chat::class, $chat);
configureScope(function(Scope $scope) use ($chat) {
$scope->setUser($chat->toArray());
});
$next($bot);
}
}