|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Transmissor\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Transmissor\Models\User; |
|
6
|
|
|
use BotMan\BotMan\BotMan; |
|
7
|
|
|
use BotMan\BotMan\Interfaces\Middleware\Received; |
|
8
|
|
|
use BotMan\BotMan\Messages\Incoming\IncomingMessage; |
|
9
|
|
|
use Illuminate\Support\Facades\Hash; |
|
10
|
|
|
use Transmissor\Services\UserService; |
|
11
|
|
|
|
|
12
|
|
|
class LoadUserMiddleware implements Received |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Handle an incoming message. |
|
18
|
|
|
* |
|
19
|
|
|
* @param IncomingMessage $message |
|
20
|
|
|
* @param callable $next |
|
21
|
|
|
* @param BotMan $bot |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
public function received(IncomingMessage $message, $next, BotMan $bot) |
|
26
|
|
|
{ |
|
27
|
|
|
// $botUser = $bot->getDriver()->getUser($message); |
|
28
|
|
|
|
|
29
|
|
|
// $user = User::firstOrCreate(['telegram_id' => $botUser->getId()], [ |
|
30
|
|
|
// 'name' => $botUser->getFirstName() ?? $botUser->getId(), |
|
31
|
|
|
// 'surname' => $botUser->getLastName(), |
|
32
|
|
|
// 'username' => $botUser->getUsername(), |
|
33
|
|
|
// 'email' => $botUser->getId() . '@Transmissorbot.com', |
|
34
|
|
|
// 'password' => Hash::make($botUser->getUsername() . '-Transmissorbot'), |
|
35
|
|
|
// ]); |
|
36
|
|
|
|
|
37
|
|
|
// auth()->login($user); |
|
38
|
|
|
|
|
39
|
|
|
// return $next($message); |
|
40
|
|
|
$user = (new UserService())->findOrCreate($bot->getDriver()->getUser($message)); |
|
41
|
|
|
|
|
42
|
|
|
auth()->login($user); |
|
43
|
|
|
|
|
44
|
|
|
$this->setUserLanguage($message); |
|
45
|
|
|
|
|
46
|
|
|
$this->checkIsGroupConversation($message, $bot); |
|
47
|
|
|
|
|
48
|
|
|
$this->checkIsNotTalkingToBot($message, $bot); |
|
49
|
|
|
|
|
50
|
|
|
$this->loadUserInGroup($message, $bot); |
|
51
|
|
|
|
|
52
|
|
|
return $next($message); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function setUserLanguage(IncomingMessage $message) |
|
56
|
|
|
{ |
|
57
|
|
|
if (isset($message->getPayload()['from']['language_code'])) { |
|
58
|
|
|
app()->setLocale($message->getPayload()['from']['language_code']); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message |
|
64
|
|
|
* @param \BotMan\BotMan\BotMan $bot |
|
65
|
|
|
* |
|
66
|
|
|
* @throws \Transmissor\Exceptions\PrivateConversationNotAllowedException |
|
67
|
|
|
* @throws \BotMan\BotMan\Exceptions\Base\BotManException |
|
68
|
|
|
*/ |
|
69
|
|
|
private function checkIsGroupConversation(IncomingMessage $message, BotMan $bot) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($message->getPayload()['chat']['type'] === 'group') { |
|
72
|
|
|
return; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$bot->say(trans('errors.bot_is_for_groups'), $message->getRecipient()); |
|
76
|
|
|
|
|
77
|
|
|
throw new PrivateConversationNotAllowedException(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message |
|
82
|
|
|
* @param \BotMan\BotMan\BotMan $bot |
|
83
|
|
|
* |
|
84
|
|
|
* @throws \Transmissor\Exceptions\InteractingWithBotException |
|
85
|
|
|
* @throws \BotMan\BotMan\Exceptions\Base\BotManException |
|
86
|
|
|
*/ |
|
87
|
|
|
private function checkIsNotTalkingToBot(IncomingMessage $message, BotMan $bot) |
|
88
|
|
|
{ |
|
89
|
|
|
if (strpos($message->getText(), '@'.config('botman.telegram.bot.username')) !== false) { |
|
90
|
|
|
|
|
91
|
|
|
$bot->say(trans('debts.you_cannot_debt_to_bot'), $message->getRecipient()); |
|
92
|
|
|
|
|
93
|
|
|
throw new InteractingWithBotException(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param \BotMan\BotMan\Messages\Incoming\IncomingMessage $message |
|
99
|
|
|
* @param \BotMan\BotMan\BotMan $bot |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Transmissor\Exceptions\MissingGroupException |
|
102
|
|
|
* @throws \BotMan\BotMan\Exceptions\Base\BotManException |
|
103
|
|
|
*/ |
|
104
|
|
|
private function loadUserInGroup(IncomingMessage $message, BotMan $bot) |
|
105
|
|
|
{ |
|
106
|
|
|
$user = User::findOrCreateTelegram($bot->getDriver()->getUser($message)); |
|
107
|
|
|
|
|
108
|
|
|
auth()->login($user); |
|
109
|
|
|
|
|
110
|
|
|
$group = Group::where('telegram_id', collect($message->getPayload())->get('chat')['id'])->first(); |
|
111
|
|
|
|
|
112
|
|
|
if ($group) { |
|
113
|
|
|
$user->addToGroup($group); |
|
114
|
|
|
|
|
115
|
|
|
$user->group = $group; |
|
116
|
|
|
|
|
117
|
|
|
app()->setLocale($group->language); |
|
118
|
|
|
|
|
119
|
|
|
} elseif (! $this->isRegisteringGroup($message, $bot)) { |
|
120
|
|
|
$bot->say(trans('groups.first_register'), $message->getRecipient()); |
|
121
|
|
|
|
|
122
|
|
|
throw new MissingGroupException(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function isRegisteringGroup(IncomingMessage $message, BotMan $bot) |
|
127
|
|
|
{ |
|
128
|
|
|
$payload = collect($message->getPayload()); |
|
129
|
|
|
|
|
130
|
|
|
$conversation = $bot->getStoredConversation($message); |
|
131
|
|
|
|
|
132
|
|
|
return $payload->contains('new_chat_members') |
|
133
|
|
|
|| $payload->contains('group_chat_created') |
|
134
|
|
|
|| $message->getText() === '/register' |
|
135
|
|
|
|| ($conversation && $conversation['conversation'] instanceof RegisterGroupConversation); |
|
|
|
|
|
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.