Skip to content

Commit 09ca731

Browse files
committed
refactor: manage subscribers list in json
1 parent 62c20a2 commit 09ca731

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

func_subscribers.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
function registerSubscriber($chat_id, array $data){
3+
$file = __DIR__.'/subscribers.json';
4+
$first = isset($data['message']['from']['first_name']) ? $data['message']['from']['first_name'] : '';
5+
$last = isset($data['message']['from']['last_name']) ? $data['message']['from']['last_name'] : '';
6+
$usern = isset($data['message']['from']['username']) ? $data['message']['from']['username'] : '';
7+
$record = [
8+
'chat_id' => (int)$chat_id,
9+
'name' => trim(filter_var($first.' '.$last, FILTER_SANITIZE_FULL_SPECIAL_CHARS)),
10+
'username' => trim(filter_var($usern, FILTER_SANITIZE_FULL_SPECIAL_CHARS))
11+
];
12+
$list = [];
13+
if(file_exists($file)){
14+
$content = file_get_contents($file);
15+
$list = json_decode($content, true);
16+
if(!is_array($list)) $list = [];
17+
}
18+
foreach($list as $item){
19+
if($item['chat_id'] === $record['chat_id']){
20+
return;
21+
}
22+
}
23+
$list[] = $record;
24+
file_put_contents($file, json_encode($list, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX);
25+
@chmod($file, 0600);
26+
}
27+
?>

tgbot.php

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
include 'func_cheque.php';
2323
include 'func_staking.php';
2424
include 'func_exchange.php';
25-
include 'func_exchange2.php';
25+
include 'func_exchange2.php';
26+
include 'func_subscribers.php';
2627

2728
#################################
2829

@@ -69,35 +70,7 @@
6970
############### START ###############
7071
if( preg_match("/\/start/i", $data['message']['text'] )){
7172

72-
//register subscriber
73-
$subscribersFile = 'subscribers.json';
74-
$first = isset($data['message']['from']['first_name']) ? $data['message']['from']['first_name'] : '';
75-
$last = isset($data['message']['from']['last_name']) ? $data['message']['from']['last_name'] : '';
76-
$usern = isset($data['message']['from']['username']) ? $data['message']['from']['username'] : '';
77-
$newrecord = [
78-
'chat_id' => (int)$chat_id,
79-
'name' => trim(filter_var($first." ".$last, FILTER_SANITIZE_FULL_SPECIAL_CHARS)),
80-
'username' => trim(filter_var($usern, FILTER_SANITIZE_FULL_SPECIAL_CHARS))
81-
];
82-
$subscribers = [];
83-
if(file_exists($subscribersFile)){
84-
$fileData = file_get_contents($subscribersFile);
85-
$subscribers = json_decode($fileData, true);
86-
if(!is_array($subscribers)) $subscribers = [];
87-
}
88-
$exists = false;
89-
foreach($subscribers as $record){
90-
if($record['chat_id'] === $newrecord['chat_id']){
91-
$exists = true;
92-
break;
93-
}
94-
}
95-
if(!$exists){
96-
$subscribers[] = $newrecord;
97-
file_put_contents($subscribersFile, json_encode($subscribers, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), LOCK_EX);
98-
@chmod($subscribersFile, 0600);
99-
}
100-
//register subscriber
73+
registerSubscriber($chat_id, $data);
10174

10275
$r = saveReferral($data);
10376

0 commit comments

Comments
 (0)