-
Notifications
You must be signed in to change notification settings - Fork 35
/
jarvischrome.php
161 lines (146 loc) · 5.78 KB
/
jarvischrome.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
<?php
$stturl = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US";
$wolframurl = "http://api.wolframalpha.com/v2/query?appid=<YOUR WOLFRAM APP ID>&format=plaintext&podtitle=Result&input=";
$trueknowledgeurl = "https://api.trueknowledge.com/direct_answer/?api_account_id=<YOUR TRUE KNOWLEDGE API USERNAME>&api_password=<YOUR TRUE KNOWLEDGE API PASSWORD>&question=";
$ttsurl = "http://translate.google.com/translate_tts?tl=en&q=";
$arduinowebserver1 = "192.168.1.177";
if(isset($_GET['speechinput'])){
/*
// Google Speech to Text
$filename = "./test1.flac";
$upload = file_get_contents($filename);
$data = array(
"Content_Type" => "audio/x-flac; rate=16000",
"Content" => $upload,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $stturl);
//curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
//curl_setopt($ch, CURLOPT_INTERFACE, "127.0.0.1");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("Content-Type: audio/x-flac; rate=16000"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
//echo $contents;
$textarray = (json_decode($contents,true));
$text = $textarray['hypotheses']['0']['utterance'];
//echo $text;
*/
$text = $_GET['speechinput'];
if(stripos($text,"turn")!==FALSE){
// Home Control API
// Handle stuff here
if(stripos($text,"light")!==FALSE){
$url = $arduinowebserver1."/?dev=light";
if(stripos($text," on")!==FALSE){
$url .= "&cmd=on";
}
else{
$url .= "&cmd=off";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
curl_close($ch);
ob_end_clean();
}
$search = array("turn","my");
$replace = array("turned","your");
$responsetext = str_ireplace($search,$replace,$text);
$answer = "Yes, I ".$responsetext;
}
elseif(stripos($text,"temperature")!==FALSE && stripos($text,"house")!==FALSE){
// Get Home Temperature
$url = $arduinowebserver1."/?dev=temperature";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
curl_close($ch);
$answer = ob_get_contents();
ob_end_clean();
}
else{
// True Knowledge API
$trueknowledgeurl .= urlencode($text);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trueknowledgeurl);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
//echo $contents;
$contents = str_replace("tk:","tk_",$contents); // simplexml doesn't seem to like colons
$obj = new SimpleXMLElement($contents);
if($answer = $obj->tk_text_result){
//$answer = "master cranky, ".$answer;
}
else{
//$answer = $obj->tk_error_message;
// Wolfram Alpha API
$wolframurl .= urlencode($text);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $wolframurl);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
//echo $contents;
$obj = new SimpleXMLElement($contents);
$answer = $obj->pod->subpod->plaintext;
if(strlen($answer)){
//$answer = "master cranky, ".$answer;
}
else{
$answer = "sorry, I don't understand";
}
}
//echo $answer;
}
// Google Text to Speech
$ttsurl .= urlencode($answer);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $ttsurl);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
//header('Content-Disposition: attachment; filename="response.mp3"');
//header("Content-Transfer-Encoding: binary");
//header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-Type: audio/mpeg');
header('Cache-Control: no-cache');
print $contents;
}
else{
?>
<html>
<head>
<title>.: Jarvis :.</title>
<script>
function submitandclear(){
if(document.getElementById('speechinput').value != ""){
document.jarvisform.submit();
document.getElementById('speechinput').value = "";
}
}
</script>
</head>
<body>
<form method="get" name="jarvisform" id="jarvisform" action="<?=$_SERVER['PHP_SELF']?>" target="voiceframe">
<input name="speechinput" id="speechinput" type="text" onFocus="submitandclear(this);" style="width:20px;" x-webkit-speech /><!--input type="submit" value="ASK" /-->
</form>
<iframe width="0px" height="0px" style="border:0px;" src="about:none" name="voiceframe"></iframe>
</body>
</html>
<?
}
?>