Skip to content

Commit d4d7b67

Browse files
committed
new log system
1 parent f0f5e52 commit d4d7b67

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

src/Command.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace Longman\TelegramBot;
1212

1313
use Longman\TelegramBot\Entities\Update;
14+
use Longman\TelegramBot\Entities\User;
1415

1516
abstract class Command
1617
{
@@ -125,10 +126,10 @@ public function isPublic()
125126

126127
public function tryMention(User $user)
127128
{
128-
if (!is_null($participant->getUsername())) {
129-
return '@'.$participant->getUsername();
129+
if (!is_null($user->getUsername())) {
130+
return '@'.$user->getUsername();
130131
} else {
131-
return $participant->getFirstName();
132+
return $user->getFirstName();
132133
}
133134
}
134135
}

src/Request.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ public static function getInput()
6060
} else {
6161
self::setInputRaw(file_get_contents('php://input'));
6262
}
63-
self::log();
63+
self::log(self::$input);
6464
return self::$input;
6565
}
6666

6767

68-
private static function log()
68+
private static function log($string)
6969
{
7070
if (!self::$telegram->getLogRequests()) {
7171
return false;
@@ -75,8 +75,8 @@ private static function log()
7575
return false;
7676
}
7777

78-
$status = file_put_contents($path, self::$input . "\n", FILE_APPEND);
79-
78+
//$status = file_put_contents($path, self::$input . "\n", FILE_APPEND);
79+
$status = file_put_contents($path, $string . "\n", FILE_APPEND);
8080
return $status;
8181
}
8282

@@ -128,13 +128,36 @@ public static function executeCurl($action, array $data)
128128
$curlConfig[CURLOPT_POSTFIELDS] = $data;
129129
}
130130

131+
if ( self::$telegram->getLogVerbosity() >= 3) {
132+
$curlConfig[CURLOPT_VERBOSE] = true;
133+
$verbose = fopen('php://temp', 'w+');
134+
curl_setopt($ch, CURLOPT_STDERR, $verbose);
135+
//Not so useful
136+
//$info = curl_getinfo($ch);
137+
//echo "Info\n";
138+
//print_r($info);
139+
}
140+
131141
curl_setopt_array($ch, $curlConfig);
132142
$result = curl_exec($ch);
133143

144+
//Logging curl requests
145+
if ( self::$telegram->getLogVerbosity() >= 3) {
146+
rewind($verbose);
147+
$verboseLog = stream_get_contents($verbose);
148+
self::log("Verbose curl output:\n". htmlspecialchars($verboseLog). "\n");
149+
}
150+
151+
//Logging getUpdates Updates
152+
//Logging curl updates
153+
if ($action == 'getUpdates' & self::$telegram->getLogVerbosity() >= 1 | self::$telegram->getLogVerbosity() >= 3) {
154+
self::setInputRaw($result);
155+
self::log($result);
156+
}
157+
134158
if ($result === false) {
135159
throw new TelegramException(curl_error($ch), curl_errno($ch));
136160
}
137-
138161
if (empty($result)) {
139162
throw new TelegramException('Empty server response');
140163
}
@@ -162,7 +185,6 @@ public static function send($action, array $data = null)
162185

163186
public static function getMe()
164187
{
165-
166188
$result = self::send('getMe');
167189
return $result;
168190
}
@@ -197,7 +219,6 @@ public static function sendLocation(array $data)
197219

198220
public static function sendChatAction(array $data)
199221
{
200-
201222
if (empty($data)) {
202223
throw new TelegramException('Data is empty!');
203224
}
@@ -210,21 +231,8 @@ public static function sendChatAction(array $data)
210231

211232
public static function getUpdates($data)
212233
{
213-
214-
if ($update = self::$telegram->getCustomUpdate()) {
215-
self::setInputRaw($update);
216-
} else {
217-
$result = self::executeCurl('getUpdates', $data);
218-
219-
self::setInputRaw($result);
220-
221-
}
222-
223-
self::log();
224-
225-
$bot_name = self::$telegram->getBotName();
226-
return new ServerResponse(json_decode($result, true), $bot_name);
227-
234+
$result = self::send('getUpdates', $data);
235+
return $result;
228236
}
229237

230238
public static function setWebhook($url)

src/Telegram.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Telegram
3030
*
3131
* @var string
3232
*/
33-
protected $version = '0.18.0';
33+
protected $version = '0.18.1';
3434

3535
/**
3636
* Telegram API key
@@ -81,6 +81,13 @@ class Telegram
8181
*/
8282
protected $log_path;
8383

84+
/**
85+
* Log verbosity
86+
*
87+
* @var string
88+
*/
89+
protected $log_verbosity;
90+
8491
/**
8592
* MySQL Integration
8693
*
@@ -216,6 +223,8 @@ public function getCommandsList()
216223
public function setLogRequests($log_requests)
217224
{
218225
$this->log_requests = $log_requests;
226+
//set default log verbosity
227+
$this->log_verbosity = 1;
219228
return $this;
220229
}
221230

@@ -255,6 +264,33 @@ public function getLogPath()
255264
}
256265

257266

267+
/**
268+
* Set log Verbosity
269+
*
270+
* @param int $log_verbosity
271+
*
272+
* 1 only incoming updates from webhook and getUpdates
273+
* 3 incoming updates from webhook and getUpdates and curl request info and response
274+
*
275+
* @return \Longman\TelegramBot\Telegram
276+
*/
277+
public function setLogVerbosity($log_verbosity)
278+
{
279+
$this->log_verbosity = $log_verbosity;;
280+
return $this;
281+
}
282+
283+
/**
284+
* Get log verbosity
285+
*
286+
*
287+
* @return int
288+
*/
289+
public function getLogVerbosity()
290+
{
291+
return $this->log_verbosity;
292+
}
293+
258294
/**
259295
* Set custom update string for debug purposes
260296
*

0 commit comments

Comments
 (0)