Skip to content

Commit 9577b33

Browse files
authored
Merge pull request #94 from Eleirbag89/analysis-zO0VRW
Apply fixes from StyleCI
2 parents f6188e6 + 8d032e2 commit 9577b33

File tree

8 files changed

+2413
-2350
lines changed

8 files changed

+2413
-2350
lines changed

Telegram.php

Lines changed: 2158 additions & 2086 deletions
Large diffs are not rendered by default.

TelegramErrorLogger.php

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,74 +3,78 @@
33

44
/**
55
* Telegram Error Logger Class.
6+
*
67
* @author shakibonline <[email protected]>
78
*/
89
class TelegramErrorLogger
910
{
10-
1111
private static $self;
1212

1313
/// Log request and response parameters from/to Telegrsm API
14+
1415
/**
1516
* Prints the list of parameters from/to Telegram's API endpoint
1617
* \param $result the Telegram's response as array
17-
* \param $content the request parameters as array
18+
* \param $content the request parameters as array.
1819
*/
19-
static public function log($result,$content, $use_rt = true)
20+
public static function log($result, $content, $use_rt = true)
2021
{
2122
try {
2223
if ($result['ok'] === false) {
23-
self::$self = new self;
24+
self::$self = new self();
2425
$e = new \Exception();
2526
$error = PHP_EOL;
26-
$error .= "==========[Response]==========";
27+
$error .= '==========[Response]==========';
2728
$error .= "\n";
2829
foreach ($result as $key => $value) {
29-
if ($value == false) $error .= $key . ":\t\t\tFalse\n";
30-
else $error .= $key . ":\t\t" . $value ."\n";
30+
if ($value == false) {
31+
$error .= $key.":\t\t\tFalse\n";
32+
} else {
33+
$error .= $key.":\t\t".$value."\n";
34+
}
3135
}
32-
$array = "=========[Sent Data]==========";
36+
$array = '=========[Sent Data]==========';
3337
$array .= "\n";
3438
if ($use_rt == true) {
3539
foreach ($content as $item) {
36-
$array .= self::$self->rt($item) . PHP_EOL . PHP_EOL;
40+
$array .= self::$self->rt($item).PHP_EOL.PHP_EOL;
3741
}
3842
} else {
3943
foreach ($content as $key => $value) {
40-
$array .= $key . ":\t\t" . $value ."\n";
44+
$array .= $key.":\t\t".$value."\n";
4145
}
4246
}
43-
$backtrace = "============[Trace]===========";
47+
$backtrace = '============[Trace]===========';
4448
$backtrace .= "\n";
4549
$backtrace .= $e->getTraceAsString();
46-
self::$self->_log_to_file($error . $array . $backtrace);
50+
self::$self->_log_to_file($error.$array.$backtrace);
4751
}
4852
} catch (\Exception $e) {
4953
echo $e->getMessage();
5054
}
5155
}
5256

5357
/// Write a string in the log file adding the current server time
58+
5459
/**
5560
* Write a string in the log file TelegramErrorLogger.txt adding the current server time
56-
* \param $error_text the text to append in the log
61+
* \param $error_text the text to append in the log.
5762
*/
5863
private function _log_to_file($error_text)
5964
{
6065
try {
61-
$fileName = __CLASS__ . '.txt';
62-
$myFile = fopen($fileName, "a+");
63-
$date = "============[Date]============";
66+
$fileName = __CLASS__.'.txt';
67+
$myFile = fopen($fileName, 'a+');
68+
$date = '============[Date]============';
6469
$date .= "\n";
65-
$date .= '[ ' . date('Y-m-d H:i:s e') . ' ] ';
66-
fwrite($myFile, $date .$error_text . "\n\n");
70+
$date .= '[ '.date('Y-m-d H:i:s e').' ] ';
71+
fwrite($myFile, $date.$error_text."\n\n");
6772
fclose($myFile);
6873
} catch (\Exception $e) {
6974
echo $e->getMessage();
7075
}
7176
}
7277

73-
7478
private function rt($array, $title = null, $head = true)
7579
{
7680
$ref = 'ref';
@@ -84,18 +88,19 @@ private function rt($array, $title = null, $head = true)
8488
if ($title != null) {
8589
$key = $title.'.'.$key;
8690
}
87-
$text .= self::rt($value , $key, false);
91+
$text .= self::rt($value, $key, false);
8892
} else {
8993
if (is_bool($value)) {
9094
$value = ($value) ? 'true' : 'false';
9195
}
92-
if ($title != '')
93-
$text .= $ref . '.'.$title.'.'.$key.'= '.$value.PHP_EOL;
94-
else
95-
$text .= $ref . '.'.$key.'= '.$value.PHP_EOL;
96-
96+
if ($title != '') {
97+
$text .= $ref.'.'.$title.'.'.$key.'= '.$value.PHP_EOL;
98+
} else {
99+
$text .= $ref.'.'.$key.'= '.$value.PHP_EOL;
100+
}
97101
}
98102
}
103+
99104
return $text;
100105
}
101-
}
106+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<?php
2-
require_once '../../../vendor/autoload.php';
32

3+
require_once '../../../vendor/autoload.php';
44

5-
$bot_id = "bot_token";
5+
$bot_id = 'bot_token';
66
$telegram = new Telegram($bot_id);
77

8-
98
$telegram->getUpdates();
109
$telegram->serveUpdate(0);
1110

1211
$res = $telegram->sendMessage([
1312
'chat_id' => 'adsf', // Chat not found
14-
'text' => 'Hello world'
13+
'text' => 'Hello world',
1514
]);
16-
var_dump($res);
15+
var_dump($res);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
2+
23
require_once '../../../vendor/autoload.php';
34

4-
$bot_id = "bot_token";
5+
$bot_id = 'bot_token';
56
$telegram = new Telegram($bot_id);
67

7-
var_dump($telegram->getUpdates());
8+
var_dump($telegram->getUpdates());

bot examples/updates/getUpdates.php

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,44 @@
22
/**
33
* Telegram Bot Example whitout WebHook.
44
* It uses getUpdates Telegram's API.
5+
*
56
* @author Gabriele Grillo <[email protected]>
67
*/
8+
include 'Telegram.php';
79

8-
include("Telegram.php");
9-
10-
$bot_id = "bot_token";
10+
$bot_id = 'bot_token';
1111
$telegram = new Telegram($bot_id);
1212

1313
// Get all the new updates and set the new correct update_id
1414
$req = $telegram->getUpdates();
15-
for ($i = 0; $i < $telegram-> UpdateCount(); $i++) {
16-
// You NEED to call serveUpdate before accessing the values of message in Telegram Class
17-
$telegram->serveUpdate($i);
18-
$text = $telegram->Text();
19-
$chat_id = $telegram->ChatID();
15+
for ($i = 0; $i < $telegram->UpdateCount(); $i++) {
16+
// You NEED to call serveUpdate before accessing the values of message in Telegram Class
17+
$telegram->serveUpdate($i);
18+
$text = $telegram->Text();
19+
$chat_id = $telegram->ChatID();
2020

21-
if ($text == "/start") {
22-
$reply = "Working";
23-
$content = array('chat_id' => $chat_id, 'text' => $reply);
24-
$telegram->sendMessage($content);
25-
}
26-
if ($text == "/test") {
27-
if ($telegram->messageFromGroup()) {
28-
$reply = "Chat Group";
29-
} else {
30-
$reply = "Private Chat";
31-
}
21+
if ($text == '/start') {
22+
$reply = 'Working';
23+
$content = ['chat_id' => $chat_id, 'text' => $reply];
24+
$telegram->sendMessage($content);
25+
}
26+
if ($text == '/test') {
27+
if ($telegram->messageFromGroup()) {
28+
$reply = 'Chat Group';
29+
} else {
30+
$reply = 'Private Chat';
31+
}
3232
// Create option for the custom keyboard. Array of array string
33-
$option = array( array("A", "B"), array("C", "D") );
33+
$option = [['A', 'B'], ['C', 'D']];
3434
// Get the keyboard
35-
$keyb = $telegram->buildKeyBoard($option);
36-
$content = array('chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => $reply);
37-
$telegram->sendMessage($content);
38-
}
39-
if ($text == "/git") {
40-
$reply = "Check me on GitHub: https://github.com/Eleirbag89/TelegramBotPHP";
41-
// Build the reply array
42-
$content = array('chat_id' => $chat_id, 'text' => $reply);
43-
$telegram->sendMessage($content);
44-
}
35+
$keyb = $telegram->buildKeyBoard($option);
36+
$content = ['chat_id' => $chat_id, 'reply_markup' => $keyb, 'text' => $reply];
37+
$telegram->sendMessage($content);
38+
}
39+
if ($text == '/git') {
40+
$reply = 'Check me on GitHub: https://github.com/Eleirbag89/TelegramBotPHP';
41+
// Build the reply array
42+
$content = ['chat_id' => $chat_id, 'text' => $reply];
43+
$telegram->sendMessage($content);
44+
}
4545
}
46-
47-
?>

bot examples/webhook/gamebot.php

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
<?php
2-
include("Telegram.php");
3-
4-
$bot_id = "bot_token";
5-
$telegram = new Telegram($bot_id);
6-
$text = $telegram->Text();
7-
$chat_id = $telegram->ChatID();
8-
$data = $telegram->getData();
9-
$callback_query = $telegram->Callback_Query();
10-
11-
if (isset($_GET['user_id']) && isset($_GET['inline']) && isset($_GET['score']) ) {
12-
$content = array('user_id' => $_GET['user_id'], 'inline_message_id' => $_GET['inline'], 'score' => $_GET['score'], 'force' => "false");
13-
$reply = $telegram->setGameScore($content);
14-
echo $reply;
15-
return;
16-
}
17-
18-
if ($data["inline_query"] !== null && $data["inline_query"] != "") {
19-
$query = $data["inline_query"]["query"];
20-
21-
if (strpos("gamename", $query) !== false) {
22-
$results = json_encode(array( array('type' => "game", 'id'=> "1", 'game_short_name' => "game_short") ) );
23-
$content = array('inline_query_id' => $data["inline_query"]["id"], 'results' => $results );
24-
$reply = $telegram->answerInlineQuery($content);
25-
}
26-
27-
}
28-
29-
if ($callback_query !== null && $callback_query != "") {
30-
$game_name = $data["callback_query"]["game_short_name"];
31-
$user_id = $data["callback_query"]["from"]["id"];
32-
$inline_id = $data["callback_query"]["inline_message_id"];
33-
34-
$content = array('callback_query_id' => $telegram->Callback_ID(), 'url' => "http://domain.com/gamefolder/?user_id=".$user_id."&inline=".$inline_id);
35-
$telegram->answerCallbackQuery($content);
36-
37-
}
38-
39-
if ($text == "/start") {
40-
$content = array('chat_id' => $chat_id, 'text' => "Welcome to Test GameBot !");
41-
$telegram->sendMessage($content);
42-
}
43-
44-
?>
1+
<?php
2+
3+
include 'Telegram.php';
4+
5+
$bot_id = 'bot_token';
6+
$telegram = new Telegram($bot_id);
7+
$text = $telegram->Text();
8+
$chat_id = $telegram->ChatID();
9+
$data = $telegram->getData();
10+
$callback_query = $telegram->Callback_Query();
11+
12+
if (isset($_GET['user_id']) && isset($_GET['inline']) && isset($_GET['score'])) {
13+
$content = ['user_id' => $_GET['user_id'], 'inline_message_id' => $_GET['inline'], 'score' => $_GET['score'], 'force' => 'false'];
14+
$reply = $telegram->setGameScore($content);
15+
echo $reply;
16+
17+
return;
18+
}
19+
20+
if ($data['inline_query'] !== null && $data['inline_query'] != '') {
21+
$query = $data['inline_query']['query'];
22+
23+
if (strpos('gamename', $query) !== false) {
24+
$results = json_encode([['type' => 'game', 'id'=> '1', 'game_short_name' => 'game_short']]);
25+
$content = ['inline_query_id' => $data['inline_query']['id'], 'results' => $results];
26+
$reply = $telegram->answerInlineQuery($content);
27+
}
28+
}
29+
30+
if ($callback_query !== null && $callback_query != '') {
31+
$game_name = $data['callback_query']['game_short_name'];
32+
$user_id = $data['callback_query']['from']['id'];
33+
$inline_id = $data['callback_query']['inline_message_id'];
34+
35+
$content = ['callback_query_id' => $telegram->Callback_ID(), 'url' => 'http://domain.com/gamefolder/?user_id='.$user_id.'&inline='.$inline_id];
36+
$telegram->answerCallbackQuery($content);
37+
}
38+
39+
if ($text == '/start') {
40+
$content = ['chat_id' => $chat_id, 'text' => 'Welcome to Test GameBot !'];
41+
$telegram->sendMessage($content);
42+
}

0 commit comments

Comments
 (0)