-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.php
243 lines (235 loc) · 10.3 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<?php
use Telegram\Bot\Api;
use Telegram\Bot\Objects\Update;
use TelegramPagerfanta\Adapter\PackagistAdapter;
use Pagerfanta\Pagerfanta;
use Base32\Base32;
use Telegram\Bot\Objects\InlineQuery\InlineQueryResultArticle;
use Telegram\Bot\TelegramRequest;
use Telegram\Bot\TelegramResponse;
require_once 'vendor/autoload.php';
if(file_exists('.env')) {
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();
}
if(getenv('MOCK_JSON')) {
class mockApi extends Api{
public function getWebhookUpdate($shouldEmitEvent = true) {
$content = trim(getenv('MOCK_JSON'), "'");
return new Update(json_decode($content, true));
}
}
$telegram = new mockApi();
} else {
error_log(file_get_contents('php://input'));
$telegram = new Api();
}
$update = $telegram->getWebhookUpdates();
// Inline Query
if($update->has('inline_query')) {
$inlineQuery = $update->getInlineQuery();
if($query = $inlineQuery->getQuery()) {
$page = $inlineQuery->getOffset()?:1;
$response = file_get_contents('https://packagist.org/search.json?q='.$query.'&page='.$page);
$response = json_decode($response, true);
if($response['total'] == 0) {
$params = [
'results' =>
[
InlineQueryResultArticle::make([
'id' => 'no-query',
'title' => 'No results',
'message_text' => 'No results',
'description' =>
'Sorry! I found nothing with your search term. Try again.'
])
]
];
} else {
if(array_key_exists('next', $response)) {
preg_match('/&page=(?<page>\d+)/', $response['next'], $next_offset);
$params['next_offset'] = $next_offset['page'];
} else {
$params['next_offset'] = '';
}
$client = new GuzzleHttp\Client();
foreach($response['results'] as $result) {
$encoded = rtrim(Base32::encode(gzdeflate($result['name'], 9)), '=');
$items = [
'id' => substr($encoded, 0, 63),
'title' => $result['name'],
'message_text' => PackagistAdapter::showPackage($result),
'description' => ($result['description'] ? : ''),
'parse_mode' => 'HTML',
'disable_web_page_preview' => true
];
try {
if(preg_match('/github.com\/(?<login>.*)\//', $result['repository'], $githubUser)) {
$githubUser = $client->get(
'https://api.github.com/users/'.$githubUser['login'],
[
'headers' => [
'Authorization' => 'token '.getenv('OAUTH_TOKEN_GITHUB')
]
]
);
if($githubUser->getStatusCode() == 200) {
$githubUser = json_decode($githubUser->getBody(), true);
if($githubUser) {
$items['thumb_url'] = $githubUser['avatar_url'];
}
}
}
} catch(Exception $e) { }
$params['results'][] = InlineQueryResultArticle::make($items);
}
}
} else {
$params = [
'switch_pm_text' => 'Type the query...',
'switch_pm_parameter' => 'inlinehelp',
'results' => []
];
}
$telegram->answerInlineQuery(
[
'inline_query_id' => $inlineQuery->getId()
] + $params
);
} else
// Inline Keyboard
if($update->has('message')) {
$message = $update->getMessage();
if($message->has('text')) {
switch($text = $message->getText()) {
case (preg_match('/\/v_(?<encoded>.+)/', $text, $matched) ? true : false):
$name = str_replace(array('_', 'X'), array('-', '/'), $matched['encoded']);
/*$name = Base32::decode($name);
if($name) {
$name = gzinflate($name);
}*/
if(!$name) {
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'Sorry! I dont find this package for show. ' .
'Try another package.'
]);
} else {
$response = file_get_contents("https://packagist.org/packages/$name.json");
$response = json_decode($response, true);
if(isset($response['status']) && $response['status'] == 'error') {
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'Sorry! ' . $response['message'] . ' '.
'Try another package.'
]);
} else {
$date = new DateTime($response['package']['time']);
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' => PackagistAdapter::showPackage($response['package']),
'parse_mode' => 'HTML',
'disable_web_page_preview' => true
]);
}
}
break;
case '/about':
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
"Bot created by @vitormattos\n".
"Source in: github.com/vitormattos/bot-packagist",
'disable_web_page_preview' => true
]);
break;
case (preg_match('/^\/start inline help/', $text) ? true : false):
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'For use inline query mode, type @PackagistBot in message box and type the text to search',
'disable_web_page_preview' => true
]);
break;
case (preg_match('/^\//', $text) ? true : false):
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'Send any text to search in packagist.org',
'disable_web_page_preview' => true
]);
break;
case strlen($text) > 64:
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'Too long text. Search using a short string.',
'disable_web_page_preview' => true
]);
break;
default:
$response = file_get_contents('https://packagist.org/search.json?q='.$text);
$response = json_decode($response, true);
if($response['total'] == 0) {
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' =>
'Sorry! I found nothing with your search term. ' .
'Try again.'
]);
} else {
$adapter = new PackagistAdapter($response);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(3);
$pagerfanta->setCurrentPage(1);
$view = new \Pagerfanta\View\TelegramInlineView();
$view->setMaxButtons(5);
$buttons = $view->render($pagerfanta, function($page) use ($text) {
return '/p=' . $page . '&q=' . $text;
});
$telegram->sendMessage([
'chat_id' => $message->getChat()->getId(),
'text' => $pagerfanta->getAdapter()->getPageContent($pagerfanta, $text),
'parse_mode' => 'HTML',
'reply_markup' => $buttons
]);
}
break;
}
}
} elseif($update->has('callback_query')) {
$callbackQuery = $update->getCallbackQuery();
switch ($query = $callbackQuery->getData()) {
case (preg_match('/\/p=(?<page>\d+)&q=(?<query>.+)/', $query, $matched) ? true : false):
$packagist_page = (int) ( ($matched['page'] * 3 - 1) / 15 ) + 1;
$response = file_get_contents('https://packagist.org/search.json?q=' . $matched['query'] . '&p=' . $packagist_page);
$response = json_decode($response, true);
$adapter = new PackagistAdapter($response);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(3);
$pagerfanta->setCurrentPage($matched['page']);
$view = new \Pagerfanta\View\TelegramInlineView();
$view->setMaxButtons(5);
$text = $pagerfanta->getAdapter()->getPageContent($pagerfanta, $matched['query']);
$a = str_replace(':', '', strip_tags($text));
$b = str_replace(':', '', $callbackQuery->getMessage()->getText());
if($a != $b) {
$telegram->editMessageText([
'chat_id' => $callbackQuery->getMessage()->getChat()->getId(),
'message_id' => $callbackQuery->getMessage()->getMessageId(),
'text' => $text,
'parse_mode' => 'HTML',
'reply_markup' => $view->render($pagerfanta, function($page) use($matched){
return '/p=' . $page . '&q=' . $matched['query'];
})
]);
} else {
$telegram->answerCallbackQuery([
'callback_query_id' => $callbackQuery->getId()
]);
}
break;
}
}