-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
193 lines (191 loc) · 6.73 KB
/
index.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
$token = "token";
$api = "bot$token";
$strings = array(
'apikey',
'apikey',
);
$key = array_rand($strings);
/**
* HttpRequest - A simple PHP class using cURL for Ajax-like HTTP requests
* by Hay Kranen <hay at bykr dot org>
* Released under the MIT / X11 license
* version 1.0
*
* Use:
* $r = new HttpRequest($method, $url, $data, $args)
* $method: "get" / "post"
* $url: duh :)
* $data: an array of values you want to send as query parameters or POST values
* $args: array of possible extra arguments for the request
*
* Then, check for errors with $r->getError() and response with $r->getResponse()
* eg with the public tweets from twitter
* $r = new HttpRequest("get", "http://twitter.com/statuses/public_timeline.json");
* if ($r->getError()) {
* echo "sorry, an error occured";
* } else {
* // parse json and show tweets
* $tweets = json_decode($r->getResponse());
* var_dump($tweets);
* }
*
* == License (MIT/X11) ==
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
class HttpRequest {
private $method, $url, $data = false;
private $error, $hasError = false, $response, $status;
private $requestInfo, $curlError, $headers = array();
// Default arguments
private $args = array(
"followRedirect" => true,
);
function __construct($method, $url, $data = false, $args = false) {
$method = strtolower($method);
if ($method == "post" || $method == "get") {
$this->method = $method;
} else {
$this->setError("Invalid method: $method");
return;
}
$this->url = $url;
$this->data = $data;
if (is_array($args)) {
// Add arguments to the already available default arguments
foreach($args as $key => $value) {
$this->args[$key] = $value;
}
}
$this->doRequest();
}
function hasError() {
return $this->hasError;
}
private function setError($msg) {
$this->error = $msg;
$this->hasError = true;
}
function getError() {
return $this->error;
}
function getStatus() {
return $this->status;
}
function getResponse() {
return $this->response;
}
function getRequestInfo() {
return $this->requestInfo;
}
function toString() {
var_dump($this);
}
private function doRequest() {
$this->doCurl();
if ($this->status != "200") {
$this->setError("Response error: " . $this->status . " (" . $this->curlError . ")");
}
}
private function doCurl() {
$c = curl_init();
// Maybe we want to rewrite the url for data arguments in GET requests
if ($this->method == "get" && $this->data) {
$this->url .= "?" . http_build_query($this->data);
}
// Default values
curl_setopt($c, CURLOPT_URL, $this->url);
curl_setopt($c, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, $this->args['followRedirect']);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Expect:'));
//register a callback function which will process the headers
//this assumes your code is into a class method, and uses $this->readHeader as the callback //function
curl_setopt($c, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
// Authentication
if (isset($this->args['username']) && isset($this->args['password'])) {
curl_setopt($c, CURLOPT_USERPWD, $this->args['username'] . ':' . $this->args['password']);
}
// POST
if($this->method == "post") {
curl_setopt($c, CURLOPT_POST, true);
// Always escape HTTP data dammit!
curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($this->data));
}
// Many servers require this to output decent HTML
if (empty($this->args['useragent'])) {
curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0");
} else {
curl_setopt($c, CURLOPT_USERAGENT, $this->args['useragent']);
}
$this->response = curl_exec($c);
$this->status = curl_getinfo($c, CURLINFO_HTTP_CODE);
$this->curlError = curl_errno($c) . ": ". curl_error($c);
$this->requestInfo = curl_getinfo($c);
$this->headers = array_merge($this->requestInfo, $this->headers);
curl_close($c);
}
private function readHeader($ch, $header) {
$key = trim(substr($header, 0, strpos($header, ":")));
$val = trim(substr($header, strpos($header, ":") + 1));
if (!empty($key) && !empty($val)) {
$this->headers[$key] = $val;
}
return strlen($header);
}
function getHeaders($key = false) {
if ($key) {
return $this->headers[$key];
} else {
return $this->headers;
}
}
}
$content = file_get_contents('php://input');
$update = json_decode($content, true);
$chatID = $update["message"]["chat"]["id"];
$userID = $update["message"]["from"]["id"];
$msg = $update["message"]["text"];
$username = $update["message"]["from"]["username"];
$nome = $update["message"]["from"]["first_name"];
// LASCIARE I CREDITI
if($msg == "/start") {
$text = "Ciao $nome!\n Fai /tts [PAROLA] \n Powered by @zProAle";
$args = array(
'chat_id' => $chatID,
'text' => "$text",
);
new HttpRequest("get", "https://api.telegram.org/$api/sendmessage", $args);
}
if(strpos($msg, "/tts")===0)
{
$e = explode(" ", $msg, 2);
$text = $e[1];
$text = str_replace(' ', "%20", $text);
$didascalia = str_replace("%20", ' ', $text);
$args = array(
'chat_id' => $chatID,
'title' => "$text",
'audio' => "http://api.voicerss.org/?key=$strings[$key]&hl=it-it&src=$text",
);
new HttpRequest("get", "https://api.telegram.org/$api/sendAudio", $args);
}