-
Notifications
You must be signed in to change notification settings - Fork 0
/
repostBot_withGetUdates.php
107 lines (102 loc) · 5.36 KB
/
repostBot_withGetUdates.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
//php /home/pepperte/public_html/feedify/bot/repostBot.php
// Load configuration file outside of doc root
//$root = "$_SERVER['DOCUMENT_ROOT']";
$config = parse_ini_file("/home/pepperte/configTelegram.ini");
$bot_id = $config['botid'];
//Connecting to sql db.
$connection = mysqli_connect("localhost",$config['username'],$config['password'],$config['dbname']);if($connection === false){
//TODO: Add error
}
$getLastUpdate = mysqli_query($connection,"SELECT * FROM `telegramBotFeed` WHERE `id` = 1") or die(mysqli_error($connection));
if($row = mysqli_fetch_array($getLastUpdate)){
$last_update = $row["lastupdate"];
echo "<br>Before: $last_update";}
else {
echo "Something went wrong";
}
$url = 'https://api.telegram.org/bot' . $bot_id . '/getUpdates?timeout=30&offset=' . $last_update;
$result = file_get_contents($url);
$result = json_decode($result, true);
$extLinkArray = array();
function has_dupes($array){
$dupe_array = array();
foreach($array as $val){
if(++$dupe_array[$val] > 1){
return true;
}
}
return false;
}
//Check if array (if there are any message from BOT basically)
if (is_array($result)) {
//Check each message
foreach ($result['result'] as $key => $value) {
$receivedMessage = $value["message"]["text"];
preg_match_all('!https?://\S+!', $receivedMessage, $matches);
$extractedLink = $matches[0][0];
//Check if message is newer than last_update
if ($last_update<$value["update_id"]){
$chat_id = $value["message"]["chat"]["id"];
//TODO: Get URL object from JSON, how to get optional URL
$urlCheck = $value["message"]["entities"][0]["type"];
//$getURL = $value["message"]["entities"][0]["url"];
//Stop Bot
if($receivedMessage == "StopBot"){
file_get_contents('https://api.telegram.org/bot' . $bot_id . '/sendMessage?text=BotStopping&chat_id='.$chat_id);
$break = true;
break;
} else if($receivedMessage == "/repoststats"){
$repoststats = mysqli_query($connection,"SELECT * FROM `stats` WHERE `chatid` = $chat_id ORDER BY reposts DESC") or die(mysqli_error($connection));
$i=1;
while($row3 = mysqli_fetch_array($repoststats)){
$message = $message . "%0A" . "$i. " . $row3["firstname"] .": ". $row3["reposts"];
$i++;
}
echo "<br>Showing stats";
/*foreach($repoststats as $key2 => $value2){
$message = $message . "\n" . $value2["firstname"] .":". $value2["reposts"];
}*/
file_get_contents('https://api.telegram.org/bot' . $bot_id . '/sendMessage?text='.$message.'&chat_id='.$chat_id);
} else if($urlCheck == "url"){
$urlCheck = mysqli_query($connection,"SELECT * FROM `urls` WHERE `url` = '$extractedLink' AND `chatid` = $chat_id") or die(mysqli_error($connection));
$firstname = $value["message"]["from"]["first_name"];
$messageid = $value["message"]["message_id"];
if($row2 = mysqli_fetch_array($urlCheck)){
$message = "wowow repost lul";
$originalMsgId = $row2["messageid"];
file_get_contents('https://api.telegram.org/bot' . $bot_id . '/sendMessage?text='.$message.'&chat_id='.$chat_id.'&reply_to_message_id='.$originalMsgId);
$repostStatCheck = mysqli_query($connection,"SELECT * FROM `stats` WHERE `firstname` = '$firstname' AND `chatid` = $chat_id") or die(mysqli_error($connection));
if($row3 = mysqli_fetch_array($repostStatCheck)){
mysqli_query($connection,"UPDATE `stats` SET `reposts`= reposts+1 WHERE `firstname` = '$firstname' AND `chatid` = $chat_id") or die(mysqli_error($connection));
} else {
mysqli_query($connection,"INSERT INTO `stats`(`chatid`, `firstname`, `reposts`) VALUES ('$chat_id','$firstname',1)") or die(mysqli_error($connection));
}
echo "<br>repost";
} else {
//Add URL to list
echo "<br>not a repost, adding to db";
mysqli_query($connection,"INSERT INTO `urls` (`url`, `firstname`,`messageid`,`chatid`) VALUES ('$extractedLink', '$firstname','$messageid','$chat_id')");
}
/*if(in_array($extractedLink,$extLinkArray)){
echo "<br>OMG DUPE";
foreach ($result['result'] as $key2 => $value2){
if(strpos($value2["message"]["text"],$extractedLink) !== false){
$originalPoster = $value2["message"]["from"]["first_name"];
$originalMsgId = $value2["message"]["message_id"];
break;
}
}
$message = "Wuwuwu wow repost lul, this was posted by " . $originalPoster . " already!!!";
}
array_push($extLinkArray, $extractedLink);*/
}
}
}
}
$last_update = $value["update_id"];
mysqli_query($connection,"UPDATE `telegramBotFeed` SET `lastupdate`= '$last_update' WHERE `id` = 1");
echo "<br>After: $last_update";
//close the db connection
mysqli_close($connection);
?>