1 | <?php |
||
24 | class DateCommand extends UserCommand |
||
25 | { |
||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name = 'date'; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $description = 'Show date/time by location'; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $usage = '/date <location>'; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $version = '1.4.0'; |
||
45 | |||
46 | /** |
||
47 | * Guzzle Client object |
||
48 | * |
||
49 | * @var \GuzzleHttp\Client |
||
50 | */ |
||
51 | private $client; |
||
52 | |||
53 | /** |
||
54 | * Base URI for Google Maps API |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $google_api_base_uri = 'https://maps.googleapis.com/maps/api/'; |
||
59 | |||
60 | /** |
||
61 | * The Google API Key from the command config |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | private $google_api_key; |
||
66 | |||
67 | /** |
||
68 | * Date format |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $date_format = 'd-m-Y H:i:s'; |
||
73 | |||
74 | /** |
||
75 | * Get coordinates |
||
76 | * |
||
77 | * @param string $location |
||
78 | * |
||
79 | * @return array |
||
80 | */ |
||
81 | private function getCoordinates($location) |
||
82 | { |
||
83 | $path = 'geocode/json'; |
||
84 | $query = ['address' => urlencode($location)]; |
||
85 | |||
86 | if ($this->google_api_key !== null) { |
||
87 | $query['key'] = $this->google_api_key; |
||
88 | } |
||
89 | |||
90 | try { |
||
91 | $response = $this->client->get($path, ['query' => $query]); |
||
92 | } catch (RequestException $e) { |
||
93 | TelegramLog::error($e->getMessage()); |
||
94 | |||
95 | return []; |
||
96 | } |
||
97 | |||
98 | if (!($data = $this->validateResponseData($response->getBody()))) { |
||
99 | return []; |
||
100 | } |
||
101 | |||
102 | $result = $data['results'][0]; |
||
103 | $lat = $result['geometry']['location']['lat']; |
||
104 | $lng = $result['geometry']['location']['lng']; |
||
105 | $acc = $result['geometry']['location_type']; |
||
106 | $types = $result['types']; |
||
107 | |||
108 | return [$lat, $lng, $acc, $types]; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get date |
||
113 | * |
||
114 | * @param string $lat |
||
115 | * @param string $lng |
||
116 | * |
||
117 | * @return array |
||
118 | */ |
||
119 | private function getDate($lat, $lng) |
||
120 | { |
||
121 | $path = 'timezone/json'; |
||
122 | |||
123 | $date_utc = new \DateTime(null, new \DateTimeZone('UTC')); |
||
124 | $timestamp = $date_utc->format('U'); |
||
125 | |||
126 | $query = [ |
||
127 | 'location' => urlencode($lat) . ',' . urlencode($lng), |
||
128 | 'timestamp' => urlencode($timestamp), |
||
129 | ]; |
||
130 | |||
131 | if ($this->google_api_key !== null) { |
||
132 | $query['key'] = $this->google_api_key; |
||
133 | } |
||
134 | |||
135 | try { |
||
136 | $response = $this->client->get($path, ['query' => $query]); |
||
137 | } catch (RequestException $e) { |
||
138 | TelegramLog::error($e->getMessage()); |
||
139 | |||
140 | return []; |
||
141 | } |
||
142 | |||
143 | if (!($data = $this->validateResponseData($response->getBody()))) { |
||
144 | return []; |
||
145 | } |
||
146 | |||
147 | $local_time = $timestamp + $data['rawOffset'] + $data['dstOffset']; |
||
148 | |||
149 | return [$local_time, $data['timeZoneId']]; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Evaluate the response data and see if the request was successful |
||
154 | * |
||
155 | * @param string $data |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | private function validateResponseData($data) |
||
176 | |||
177 | /** |
||
178 | * Get formatted date at the passed location |
||
179 | * |
||
180 | * @param string $location |
||
181 | * |
||
182 | * @return string |
||
183 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
184 | */ |
||
185 | private function getFormattedDate($location) |
||
202 | |||
203 | /** |
||
204 | * Command execute method |
||
205 | * |
||
206 | * @return mixed |
||
207 | * @throws \Longman\TelegramBot\Exception\TelegramException |
||
208 | */ |
||
209 | public function execute() |
||
235 | } |
||
236 |