1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\Gcalendar_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
define('APPLICATION_NAME', 'JarvisPHP Client'); |
11
|
|
|
define('CREDENTIALS_PATH', 'Plugins/Gcalendar_plugin/api-key.json'); |
12
|
|
|
define('CLIENT_SECRET_PATH', 'Plugins/Gcalendar_plugin/secret-client-key.json'); |
13
|
|
|
define('SCOPES', implode(' ', array( |
14
|
|
|
\Google_Service_Calendar::CALENDAR_READONLY) |
15
|
|
|
)); |
16
|
|
|
define('_MAX_EVENTS', 4); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Google Calendar plugin |
20
|
|
|
* @author Stefano Bianchini |
21
|
|
|
* @website http://www.stefanobianchini.net |
22
|
|
|
*/ |
23
|
|
|
class Gcalendar_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
24
|
|
|
/** |
25
|
|
|
* Priority of plugin |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
var $priority = 1; |
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* the behaviour of plugin |
32
|
|
|
* @param string $command |
33
|
|
|
*/ |
34
|
|
|
function answer($command) { |
|
|
|
|
35
|
|
|
$answer = ''; |
36
|
|
|
|
37
|
|
|
JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
38
|
|
|
|
39
|
|
|
// Get the API client and construct the service object. |
40
|
|
|
$client = Gcalendar_plugin::getClient(); |
41
|
|
|
|
42
|
|
|
if($client==null) return null; |
43
|
|
|
|
44
|
|
|
$service = new \Google_Service_Calendar($client); |
45
|
|
|
|
46
|
|
|
// Print the next _MAX_EVENTS events on the user's calendar. |
47
|
|
|
$calendarId = 'primary'; |
48
|
|
|
$optParams = array( |
49
|
|
|
'maxResults' => _MAX_EVENTS, |
50
|
|
|
'orderBy' => 'startTime', |
51
|
|
|
'singleEvents' => TRUE, |
52
|
|
|
'timeMin' => date('c'), |
53
|
|
|
); |
54
|
|
|
$results = $service->events->listEvents($calendarId, $optParams); |
55
|
|
|
|
56
|
|
|
if (count($results->getItems()) == 0) { |
|
|
|
|
57
|
|
|
$answer = JarvisLanguage::translate('no_appointments',get_called_class()); |
58
|
|
|
} else { |
59
|
|
|
|
60
|
|
|
foreach ($results->getItems() as $event) { |
|
|
|
|
61
|
|
|
$start = $event->start->dateTime; |
62
|
|
|
if (empty($start)) { |
63
|
|
|
$start = $event->start->date; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$date = new \DateTime($start); |
67
|
|
|
$answer.= sprintf(JarvisLanguage::translate('list_events',get_called_class()), $date->format('j'), JarvisLanguage::translate('month_'.$date->format('n'),get_called_class()), $date->format('H'), $date->format('i'), $event->getSummary())."\n"; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
JarvisTTS::speak($answer); |
72
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
73
|
|
|
$response->send(); |
74
|
|
|
} |
75
|
|
|
/** |
76
|
|
|
* Get plugin's priority |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
function getPriority() { |
|
|
|
|
80
|
|
|
return $this->priority; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Is it the right plugin for the command? |
85
|
|
|
* @param string $command |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
|
|
function isLikely($command) { |
|
|
|
|
89
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Does the plugin need a session? |
94
|
|
|
* @return boolean |
95
|
|
|
*/ |
96
|
|
|
function hasSession() { |
|
|
|
|
97
|
|
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Returns an authorized API client. |
102
|
|
|
* @return Google_Client the authorized client object |
103
|
|
|
*/ |
104
|
|
|
function getClient() { |
|
|
|
|
105
|
|
|
$client = new \Google_Client(); |
106
|
|
|
$client->setApplicationName(APPLICATION_NAME); |
107
|
|
|
$client->setScopes(SCOPES); |
108
|
|
|
$client->setAuthConfigFile(CLIENT_SECRET_PATH); |
109
|
|
|
$client->setAccessType('offline'); |
110
|
|
|
|
111
|
|
|
// Load previously authorized credentials from a file. |
112
|
|
|
$credentialsPath = CREDENTIALS_PATH; |
113
|
|
|
if (file_exists($credentialsPath)) { |
114
|
|
|
$accessToken = file_get_contents($credentialsPath); |
115
|
|
|
} else { |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
$client->setAccessToken($accessToken); |
119
|
|
|
|
120
|
|
|
// Refresh the token if it's expired. |
121
|
|
|
if ($client->isAccessTokenExpired()) { |
122
|
|
|
$client->refreshToken($client->getRefreshToken()); |
123
|
|
|
file_put_contents($credentialsPath, $client->getAccessToken()); |
124
|
|
|
} |
125
|
|
|
return $client; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
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.