commit | author | age
|
8bd4d9
|
1 |
<?php |
bc45a2
|
2 |
require_once(dirname(__FILE__).'/msapi_config.php'); |
JK |
3 |
|
|
4 |
class msapi extends msapi_config { |
|
5 |
public $url; |
|
6 |
|
|
7 |
function __construct($url) { |
|
8 |
$this->url = $url; |
|
9 |
} |
|
10 |
|
|
11 |
function execute($params) { |
|
12 |
if(!is_array($params)) { |
|
13 |
throw new Exception('Przekazany parametr nie jest tablicą'); |
|
14 |
} |
|
15 |
|
|
16 |
foreach($params as $name => &$param) { |
|
17 |
if(substr($name, 0, 1)!='$' && is_string($param)) { |
317fa6
|
18 |
$param = '\''.strtr($param, array('\'' => '\'\'')).'\''; |
bc45a2
|
19 |
} |
JK |
20 |
} |
|
21 |
unset($param); |
|
22 |
$params['$format'] = 'json'; |
|
23 |
|
|
24 |
$context = stream_context_create(array( |
|
25 |
'http' => array( |
|
26 |
'request_fulluri' => TRUE, |
|
27 |
'header' => 'Authorization: Basic '.base64_encode(':'.$this->accountKey) |
|
28 |
), |
|
29 |
)); |
|
30 |
|
|
31 |
$content = file_get_contents($this->url.'?'.http_build_query($params, '', '&'), FALSE, $context); |
|
32 |
if(!$content) { |
|
33 |
return FALSE; |
|
34 |
} |
|
35 |
|
|
36 |
$content = json_decode($content, TRUE); |
|
37 |
if(!$content) { |
|
38 |
return FALSE; |
|
39 |
} |
|
40 |
|
|
41 |
return $content; |
|
42 |
} |
|
43 |
} |
64d311
|
44 |
|
8bd4d9
|
45 |
class bot_lang_module implements BotModule { |
JK |
46 |
function handle($msg, $params) { |
|
47 |
$args = trim($msg->args); |
|
48 |
|
|
49 |
if(empty($args)) { |
|
50 |
return BotMsg('Podaj tekst do przetłumaczenia!'); |
|
51 |
} |
|
52 |
|
|
53 |
$url = 'http://translate.google.com/translate_a/t?client=t&text='.urlencode($args).'&sl='.$params[0].'&tl='.$params[1].'&hl=pl&ie=utf-8&oe=utf-8'; |
|
54 |
$data = @file_get_contents($url, 0, stream_context_create(array( |
|
55 |
'http' => array( |
|
56 |
'method' => 'GET', |
|
57 |
), |
|
58 |
))); |
|
59 |
|
|
60 |
if(!$data) { |
|
61 |
return new BotMsg('Błąd podczas pobierania danych ze słownika. Przepraszamy.'); |
|
62 |
} |
|
63 |
|
|
64 |
$data = jsarray::parse($data); |
|
65 |
|
|
66 |
if(!$data OR count($data)==0 OR count($data[1])==0) { |
bc45a2
|
67 |
$api = new msapi('https://api.datamarket.azure.com/Bing/MicrosoftTranslator/Translate'); |
64d311
|
68 |
$data = $api->execute(array( |
JK |
69 |
'From' => $params[0], |
|
70 |
'To' => $params[1], |
|
71 |
'Text' => $args, |
|
72 |
'$skip' => 0, |
|
73 |
'$top' => 1 |
|
74 |
)); |
8bd4d9
|
75 |
|
64d311
|
76 |
if(!$data || !isset($data['d']['results'][0]['Text'])) { |
JK |
77 |
return new BotMsg('Błąd podczas pobierania danych z tłumacza. Przepraszamy.'); |
8bd4d9
|
78 |
} |
JK |
79 |
|
64d311
|
80 |
$data = $data['d']['results'][0]['Text']; |
JK |
81 |
|
|
82 |
return new BotMsg('<u>Tłumaczenie (by Microsoft Translator):</u><br />'."\n".htmlspecialchars($data)); |
8bd4d9
|
83 |
} |
JK |
84 |
else |
|
85 |
{ |
|
86 |
$html = '<u>Słownik (by Google):</u>'; |
|
87 |
foreach($data[1] as $przyp) { |
|
88 |
$html .= '<br />'."\n".'<b>'.htmlspecialchars($przyp[0]).'</b>'; |
|
89 |
foreach($przyp[1] as $term) { |
|
90 |
$html .= '<br />'."\n".'- '.htmlspecialchars($term); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
return new BotMsg($html); |
|
95 |
} |
|
96 |
} |
|
97 |
|
|
98 |
function typo($msg, $params) { |
|
99 |
return new BotMsg('Wybrana komenda nie istnieje. Prawdopodobnie chodziło ci o jedną z komend językowych, których nazwy zapisywane są <b>bez</b> spacji pomiędzy spacji pomiędzy kodami języków (angpol, a nie: ang pol).<br /><br />'."\n\n" |
|
100 |
|
|
101 |
. '<u>Spróbuj:</u><br />'."\n" |
0e0018
|
102 |
. $msg->command.htmlspecialchars(ltrim($msg->args))); |
8bd4d9
|
103 |
} |
JK |
104 |
} |
|
105 |
?> |