-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.php
128 lines (107 loc) · 4.86 KB
/
webhook.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
<?php
define('BOT_TOKEN', ''); // replace
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
define('BOT_USERNAME', ''); // replace
require('curl.php');
function catchRequest() {
$json = file_get_contents("php://input");
if(!$request = json_decode($json)){
return false;
}
return $request;
}
if(!$REQUEST = catchRequest()){
exit;
}
// end of reception
if(strlen($REQUEST->message->text) < 5){
exit;
}
function sendMessage($chatId, $text) {
$query = http_build_query([
'chat_id'=> $chatId,
'text'=> $text
]);
$response = file_get_contents(API_URL.'sendMessage?'.$query);
return $response;
}
function youtubeMusicSearch($query){
$link = 'https://music.youtube.com/youtubei/v1/search?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'; // <= Public API Key
$head = array(
'Accept: */*',
'Accept-Language: en-US',
'Content-Type: application/json',
'X-YouTube-Client-Name: 67',
'X-YouTube-Client-Version: 0.1',
'X-YouTube-Utc-Offset: -360',
'X-YouTube-Time-Zone: America/Mexico_City',
'Origin: https://music.youtube.com',
'Referer: https://music.youtube.com/'
);
$post = '{"context":{"client":{"clientName":"WEB_REMIX","clientVersion":"0.1","hl":"es-419","gl":"MX","experimentIds":[],"experimentsToken":"","utcOffsetMinutes":-360,"locationInfo":{"locationPermissionAuthorizationStatus":"LOCATION_PERMISSION_AUTHORIZATION_STATUS_UNSUPPORTED"},"musicAppInfo":{"musicActivityMasterSwitch":"MUSIC_ACTIVITY_MASTER_SWITCH_INDETERMINATE","musicLocationMasterSwitch":"MUSIC_LOCATION_MASTER_SWITCH_INDETERMINATE","pwaInstallabilityStatus":"PWA_INSTALLABILITY_STATUS_CAN_BE_INSTALLED"}},"capabilities":{},"request":{"internalExperimentFlags":[{"key":"force_music_enable_outertube_tastebuilder_browse","value":"true"},{"key":"force_music_enable_outertube_search_suggestions","value":"true"},{"key":"force_music_enable_outertube_playlist_detail_browse","value":"true"}],"sessionIndex":0},"activePlayers":{},"user":{"enableSafetyMode":false}},"query":"'.$query.'","suggestStats":{"validationStatus":"VALID","parameterValidationStatus":"VALID_PARAMETERS","clientName":"youtube-music","originalQuery":"'.$query.'","availableSuggestions":[{"index":0,"type":0},{"index":1,"type":0},{"index":2,"type":0},{"index":3,"type":0},{"index":4,"type":0},{"index":5,"type":0},{"index":6,"type":0}],"zeroPrefixEnabled":true}}';
if(!$req = curl($link, $head, $post, false)){
return false;
}
if($req['info']['http_code'] !== 200){
return $req['info']['http_code'];
}
$response = json_decode($req['body'], true);
if(isset($response['contents']['sectionListRenderer']['contents'][0]['musicShelfRenderer']['contents'][0]['musicResponsiveListItemRenderer']['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId'])){
return $response['contents']['sectionListRenderer']['contents'][0]['musicShelfRenderer']['contents'][0]['musicResponsiveListItemRenderer']['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId'];
}
return false;
}
function normalSearch($query){
$q = http_build_query(
['search_query' => $query]
);
$opts = [
"http" => [
"method" => "GET",
"header" => "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0\r\n" .
"accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\n"
]
];
$context = stream_context_create($opts);
$response = file_get_contents('https://youtube.com/results?'.$q,false,$context);
if (preg_match('/\/watch\?v=[\w\d-]+/',$response,$coincidencias)) {
return $coincidencias[0];
} else {
return "No hubo resultados para tu busqueda :(";
}
}
// send message
$message_id = $REQUEST->message->message_id;
$chat_id = $REQUEST->message->chat->id;
$text = $REQUEST->message->text;
function yt_search($query){
if (strpos($query,'-')){
$search = youtubeMusicSearch($query);
if ($search){
return 'https://www.youtube.com/watch?v='.$search;
} else {
return 'No hubo resultados.';
}
} else {
$search = normalSearch($query);
if ($search) {
return 'https://www.youtube.com'.$search;
} else {
return 'Hubo un error';
}
}
}
switch ($REQUEST->message->chat->type) {
case 'private':
if (strpos($text, "/start") === 0) {
sendMessage($chat_id,'Hola, envia tu busqueda.');
} else {
sendMessage($chat_id,yt_search($text));
}
case 'supergroup':
if(strpos($text, "@".BOT_USERNAME) === 0){
$new = str_replace("@".BOT_USERNAME." ",'', $text);
sendMessage($chat_id,yt_search($new));
}
}
?>