Issues (236)

examples/messenger/bot.php (2 issues)

1
<?php
2
3
use BPT\BPT;
4
use BPT\constants\dbTypes;
5
use BPT\database\mysql;
6
use BPT\types\message;
7
8
if (file_exists('vendor/autoload.php')) {
9
    require 'vendor/autoload.php';
10
}
11
else {
12
    if (!file_exists('BPT.phar')) {
13
        copy('https://dl.bptlib.ir/BPT.phar', 'BPT.phar');
14
    }
15
    require 'BPT.phar';
16
}
17
18
class handler extends BPT {
19
    const ADMIN = 123456789;
20
    const SHOW_STATUS = true;
21
    const UNKNOWN = 'I didnt understand it, Please use commands or reply to a user message';
22
    const HELP            = 'Hello dear admin 
23
This is a simple messenger bot which also support reply for users and admin itself
24
For answering an user message , You must reply on it
25
If you want to reply , You must first use /reply_on command and this will apply on your next message
26
If you want to cancel reply , use /reply_off
27
If you reply on yourself message , it will be replied without checking reply_on status
28
Good luck and be nice';
29
    const REPLY_ON        = 'Ok dear admin , Your next message will be replied';
30
    const REPLY_OFF       = 'Ok dear admin , Reply canceled';
31
    const NOT_FOUND       = 'I didn\'t found this message in my database';
32
    const START_TEXT      = 'Hello dear user
33
Send your message and I will deliver it to my admin';
34
    const SEND_FAILED     = 'Failed!';
35
    const SEND_SUCCESSFUL = 'Done!';
36
37
    public function __construct (array $settings) {
38
        parent::__construct($settings);
39
    }
40
41
    public function message (message $update) {
42
        $text = $update->text ?? '';
43
        $user_id = $update->from->id;
44
45
        if ($text === '/start') {
46
            return $this->sendMessage(self::START_TEXT, answer: true);
0 ignored issues
show
The call to BPT\BPT::sendMessage() has too many arguments starting with self::START_TEXT. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            return $this->/** @scrutinizer ignore-call */ sendMessage(self::START_TEXT, answer: true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
47
        }
48
49
        $message_id = $update->message_id;
50
        #$message_id = $update->id;
51
52
        if (self::ADMIN === $user_id) {
53
            if ($text === '/help') {
54
                return $this->sendMessage(self::HELP, answer: true);
55
            }
56
            if ($text === '/reply_on') {
57
                mysql::update('users',['value'=>'reply_on'],['id'=>$user_id],1);
58
                return $this->sendMessage(self::REPLY_ON, answer: true);
59
            }
60
            if ($text === '/reply_off') {
61
                mysql::update('users',['value'=>'reply_off'],['id'=>$user_id],1);
62
                return $this->sendMessage(self::REPLY_OFF, answer: true);
63
            }
64
            if (!isset($update->reply_to_message)) {
65
                return $this->sendMessage(self::UNKNOWN, answer: true);
66
            }
67
68
            $reply_message_id = $update->reply_to_message->message_id;
69
70
            if ($update->reply_to_message->from->id === $user_id) {
71
                $check_message = mysql::select('messages', ['receiver_message_id','receiver_id'], [
72
                    'sender_message_id' => $reply_message_id,
73
                    'sender_id'         => $user_id
74
                ],1);
75
76
                if ($check_message->num_rows < 1) {
77
                    return $this->sendMessage(self::NOT_FOUND, answer: true);
78
                }
79
                $data = $check_message->fetch_object();
80
                $receiver_id = $data->receiver_id;
81
                $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->receiver_message_id);
0 ignored issues
show
The call to BPT\BPT::copyMessage() has too many arguments starting with $receiver_id. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
                /** @scrutinizer ignore-call */ 
82
                $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->receiver_message_id);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
82
            }
83
            else {
84
                $check_message = mysql::select('messages', ['sender_message_id','sender_id'], [
85
                    'receiver_message_id' => $reply_message_id,
86
                    'receiver_id'         => $user_id
87
                ],1);
88
89
                if ($check_message->num_rows < 1) {
90
                    return $this->sendMessage(self::NOT_FOUND, answer: true);
91
                }
92
                $data = $check_message->fetch_object();
93
                $value = mysql::select('users','value',['id'=>$user_id])->fetch_object()->value;
94
                $receiver_id = $data->sender_id;
95
                if ($value === 'reply_on') {
96
                    mysql::update('users',['value'=>''],['id'=>$user_id]);
97
                    $result = $this->copyMessage($receiver_id, reply_to_message_id: $data->sender_message_id);
98
                }
99
                else {
100
                    $result = $this->copyMessage($receiver_id);
101
                }
102
            }
103
104
            if (!self::$status) {
105
                return $this->sendMessage(self::SEND_FAILED, answer: true);
106
            }
107
            mysql::insert('messages', [
108
                'sender_message_id',
109
                'sender_id',
110
                'receiver_message_id',
111
                'receiver_id'
112
            ], [$message_id, $user_id, $result->message_id, $receiver_id]);
113
            if (self::SHOW_STATUS) {
114
                $this->sendMessage(self::SEND_SUCCESSFUL, answer: true);
115
            }
116
            return true;
117
        }
118
        $username = $update->from->username;
119
        if (empty($username)) {
120
            $name = $update->from->first_name . (!empty($update->from->last_name) ? (' ' . $update->from->last_name) : '');
121
            $keyboard = [
122
                'inline_keyboard' => [
123
                    [
124
                        [
125
                            'text' => 'User ID', 'url' => "tg://user?id=$user_id"
126
                        ],
127
                        [
128
                            'text' => $user_id, 'url' => "tg://user?id=$user_id"
129
                        ]
130
                    ],
131
                    [
132
                        [
133
                            'text' => 'Name', 'callback_data' => 'none'
134
                        ],
135
                        [
136
                            'text' => $name, 'callback_data' => 'none'
137
                        ]
138
                    ]
139
                ]
140
            ];
141
        }
142
        else {
143
            $keyboard = [
144
                'inline_keyboard' => [
145
                    [
146
                        [
147
                            'text' => 'Username', 'url' => "https://t.me/$username"
148
                        ],
149
                        [
150
                            'text' => $username, 'url' => "https://t.me/$username"
151
                        ]
152
                    ]
153
                ]
154
            ];
155
        }
156
        if (isset($update->reply_to_message)) {
157
            $reply_message_id = $update->reply_to_message->message_id;
158
            if ($update->reply_to_message->from->id === $user_id){
159
                $check_message = mysql::select('messages', 'receiver_message_id', [
160
                    'sender_message_id' => $reply_message_id,
161
                    'sender_id'         => $user_id
162
                ],1);
163
                if ($check_message->num_rows > 0) {
164
                    $result = $this->copyMessage(self::ADMIN, reply_to_message_id: $check_message->fetch_object()->receiver_message_id,reply_markup: $keyboard);
165
                }
166
                else {
167
                    $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
168
                }
169
            }
170
            else {
171
                $check_message = mysql::select('messages', 'sender_message_id', [
172
                    'receiver_message_id' => $reply_message_id,
173
                    'receiver_id'         => $user_id
174
                ],1);
175
                if ($check_message->num_rows > 0) {
176
                    $result = $this->copyMessage(self::ADMIN, reply_to_message_id: $check_message->fetch_object()->sender_message_id,reply_markup: $keyboard);
177
                }
178
                else {
179
                    $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
180
                }
181
            }
182
        }
183
        else {
184
            $result = $this->copyMessage(self::ADMIN,reply_markup: $keyboard);
185
        }
186
187
        /**
188
         * This is status of last called telegram method
189
         * You can use telegram::$status or request::$status either
190
         */
191
        if (!self::$status) {
192
            return $this->sendMessage(self::SEND_FAILED, answer: true);
193
        }
194
195
        mysql::insert('messages', [
196
            'sender_message_id',
197
            'sender_id',
198
            'receiver_message_id',
199
            'receiver_id'
200
        ], [$message_id, $user_id, $result->message_id, self::ADMIN]);
201
202
        if (self::SHOW_STATUS) {
203
            $this->sendMessage(self::SEND_SUCCESSFUL, answer: true);
204
        }
205
        return true;
206
    }
207
}
208
209
/**
210
 * BPT settings
211
 *
212
 * @link https://bptlib.ir/multi
213
 */
214
$BPT = new handler([
215
    'token' => 'YOUR_BOT_TOKEN',
216
    'db' => [
217
        'type' => dbTypes::MYSQL,
218
        'user' => 'dbUser',
219
        'pass' => 'dbPassword',
220
        'dbname' => 'dbName'
221
    ],
222
    'allowed_updates' => ['message']
223
]);