-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathwolfram.php
More file actions
160 lines (129 loc) · 4.08 KB
/
wolfram.php
File metadata and controls
160 lines (129 loc) · 4.08 KB
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
<?php
// alexa ask wolfram skill
// astrobleem/chad doebelin
//
//This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// test artifacts
//$myQ = 'when will the sun set tomorrow';
//$myQ = 'how many calories are in a pepsi';
//$myQ = 'who was gandalf the grey';
$myQ = 'what is chromium';
//$myQ = 'how many teaspoons in a tablespoon';
// functions for making alexa speak
function speechOut($words) {
header('Content-type: application/json');
$open = '{"version": "1.0","sessionAttributes":"","response":{"outputSpeech":{"type": "PlainText","text": "';
$close = '"},"shouldEndSession": false}}';
$end = '"},"shouldEndSession": false}}';
echo $open;
echo ($words);
echo $close;
}
function endSession($words) {
header('Content-type: application/json');
$open = '{"version": "1.0","sessionAttributes":"","response":{"outputSpeech":{"type": "PlainText","text": "';
$close = '"},"shouldEndSession": true}}';
echo $open;
echo ($words);
echo $close;
}
// gets the header received from amazon when it makes the request
// so i can write it to file and tail -f the file this lets me watch
// what's happening in the session
$data = json_decode(file_get_contents('php://input'),TRUE);
$text = print_r($data,true);
$ver = $data['version'];
$requestType = $data['request'][type];
// request type just writes the type of request to the file
// the text variable file_put_contents writes the php array to disk
// tail this file
//file_put_contents('fromamazon.txt',$requestType);
file_put_contents('fromamazon.txt',$text);
// if for some reason you wanted to show something to web users.
if(!$ver)
{
//echo("<html>web request detected</html>");
}
if($requestType == 'SessionEndedRequest')
{
endSession('goodbye');
}
//$requestType = 'LaunchRequest';
if($requestType == 'LaunchRequest')
{
// new session!
speechOut('What is your question?');
}
//$requestType = 'IntentRequest';
if($requestType == 'IntentRequest')
{
// the question comes from the header sent by 'mamazon
$myQ = $data['request'][intent][slots][Ans][value];
// build the string
$path = 'http://api.wolframalpha.com/v2/query?input=';
$append_id = '&appid=';
// GET YOUR OWN WOLFRAM API KEY ITS FREE
$append_id .= 'XXXXXX-XXXXXXXX';
// GET YOUR OWN WOLFRAM API KEY ITS FREE
// https://developer.wolframalpha.com/portal/apisignup.html
$question = urlencode($myQ);
$getstring = $path.$question.$append_id;
$data = simplexml_load_string(file_get_contents($getstring));
$i = 0;
$answered = 0;
$answer = '';
foreach($data as $pod)
foreach($pod as $subpod)
foreach($subpod as $key => $val){
if($key == 'plaintext'){
$i = $i + 1;
// Input Interpretation pod:
if($i == 1){
// if ($val)
// $answer .= $val;
// echo("wolfram question interpretation: $val<br>");
//
}
// Result pod:
if($i == 2){
if ($val)
{
$answer .= $val;
$answered = 1;
}
}
if( ($i == 3) && ($answered == 0) ){
if ($val)
{
$answer .= $val;
$answered = 1;
}
}
if( ($i == 4) && ($answered == 0) ){
if ($val)
{
$answer .= $val;
$answered = 1;
}
}
} // foreach
} // foreach
// this gets the extra junk and formatting out of the response
$answer = strip_tags($answer);
$answer = preg_replace("/[^A-Za-z0-9 ]/", '', $answer);
$answer = preg_replace("/[^[:alnum:][:space:]]/ui", '', $answer);
// now it's plaintext, lets have alexa say it.
speechOut($answer);
} // end of intent request
?>