1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\ActualOutsideTemperature_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* ActualOutsideTemperature_plugin |
12
|
|
|
* |
13
|
|
|
* @author Stefano Bianchini |
14
|
|
|
*/ |
15
|
|
|
class ActualOutsideTemperature_plugin implements \JarvisPHP\Core\JarvisPluginInterface { |
16
|
|
|
/** |
17
|
|
|
* Priority of plugin |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
var $priority = 4; |
|
|
|
|
21
|
|
|
|
22
|
|
|
var $place = "Rimini, Italy"; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* the behaviour of plugin |
26
|
|
|
* @param string $command |
27
|
|
|
*/ |
28
|
|
|
function answer($command) { |
|
|
|
|
29
|
|
|
|
30
|
|
|
$BASE_URL = "http://query.yahooapis.com/v1/public/yql"; |
31
|
|
|
$yql_query = 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="'.$this->place.'") and u="c"'; |
32
|
|
|
$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json"; |
33
|
|
|
|
34
|
|
|
// Make call with cURL |
35
|
|
|
$session = curl_init($yql_query_url); |
36
|
|
|
curl_setopt($session, CURLOPT_RETURNTRANSFER,true); |
37
|
|
|
$json = curl_exec($session); |
38
|
|
|
// Convert JSON to PHP object |
39
|
|
|
$phpObj = json_decode($json); |
40
|
|
|
$answer = sprintf(JarvisLanguage::translate('actual_temperature_outside_is',get_called_class()),$phpObj->query->results->channel->item->condition->temp, $phpObj->query->results->channel->atmosphere->humidity); |
41
|
|
|
JarvisTTS::speak($answer); |
42
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
43
|
|
|
$response->send(); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get plugin's priority |
49
|
|
|
* @return int |
50
|
|
|
*/ |
51
|
|
|
function getPriority() { |
|
|
|
|
52
|
|
|
return $this->priority; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Is it the right plugin for the command? |
57
|
|
|
* @param string $command |
58
|
|
|
* @return boolean |
59
|
|
|
*/ |
60
|
|
|
function isLikely($command) { |
|
|
|
|
61
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Does the plugin need a session? |
66
|
|
|
* @return boolean |
67
|
|
|
*/ |
68
|
|
|
function hasSession() { |
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.