-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgraphBot.php
More file actions
136 lines (95 loc) · 3.04 KB
/
graphBot.php
File metadata and controls
136 lines (95 loc) · 3.04 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once ('jpgraph-4.0.1/src/jpgraph.php');
require_once ('jpgraph-4.0.1/src/jpgraph_line.php');
define('BOT_TOKEN', '<your_bot_token>');
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
// read incoming info and grab the chatID
$content = file_get_contents("php://input");
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
switch($message){
case "/<your_command>@<your_bot>": sendGraph($chatID);
break;
case "/<your_command>": sendGraph($chatID);
break;
default: register_traffic($chatID);
}
function connect_db() {
$host = 'localhost';
$user = '<db_user>';
$pwd = '<db_password>';
$dbname = '<db_name>';
$error = "Impossible connect to database $dbname! ";
$connect = mysqli_connect($host, $user, $pwd, $dbname) or die($error.mysqli_connect_error());
return $connect;
}
function register_traffic($ID){
$connect = connect_db();
$hour = date('G', time());
$hour = intval($hour);
$query = "CALL add_stats($ID,$hour)";
$send = mysqli_query($connect, $query) or die("Query add_stats fallita. ".mysqli_error($connect));
}
function sendGraph($ID){
$url = API_URL."sendPhoto?chat_id=".$ID;
$post_fields = array('chat_id' => $ID,'photo' => new CURLFile(realpath("/<path_to_bot_folder>/image.png")));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
makeGraph($ID);
$output = curl_exec($ch);
}
function makeGraph($ID){
$connect = connect_db();
$query = "SELECT * FROM `statistics` WHERE `chat_id` = $ID ORDER BY `hour`";
$send = mysqli_query($connect, $query) or die("Query add_stats failed. ".mysqli_error($connect));
$ydata = array();
$hour = date('G', time());
$hour = intval($hour);
//fetch and order data
$array = array();
while($row = mysqli_fetch_assoc($send)){
array_push($array, $row['counter']);
}
for($i=$hour+1;$i<24;$i++){
$ydata[$i] = $array[$i];
}
for($i=0;$i<$hour+1;$i++){
$ydata[$i] = $array[$i];
}
$orderedYdata = array();
for($i=$hour+1;$i<24;$i++){
array_push($orderedYdata, $ydata[$i]);
}
for($i=0;$i<$hour+1;$i++){
array_push($orderedYdata, $ydata[$i]);
}
// Setup the graph
$graph = new Graph(1000,500);
$graph->SetScale("textlin");
$theme_class = new UniversalTheme;
$graph->SetTheme($theme_class);
$graph->img->SetAntiAliasing(false);
//$graph->title->Set('Last 24h statistics');
$graph->SetBox(false);
$graph->img->SetAntiAliasing();
$graph->yaxis->HideZeroLabel();
$graph->yaxis->HideLine(false);
$graph->yaxis->HideTicks(false,false);
$graph->xgrid->Show();
$graph->xgrid->SetLineStyle("solid");
$graph->xaxis->SetTickLabels(array_keys($ydata));
$graph->xgrid->SetColor('#E3E3E3');
// Create the first line
$p1 = new LinePlot($orderedYdata);
$graph->Add($p1);
$p1->SetColor("#6495ED");
$graph->legend->SetFrameWeight(1);
// Output line
$graph->Stroke('image.png');
}
?>