-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebhook.php
More file actions
57 lines (45 loc) · 1.48 KB
/
Copy pathwebhook.php
File metadata and controls
57 lines (45 loc) · 1.48 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
<?php
declare(strict_types=1);
require __DIR__ . "/vendor/autoload.php";
$dotenv = Dotenv\Dotenv::create(__DIR__);
$dotenv->load();
/**
* Envia uma requisição para a API do OpenWeatherMap e recupera
* a informação do tempo para uma cidade
*
* @param string $city
* @return string
*/
function getWeatherInformation(string $city): string
{
$apiKey = \getenv("OPEN_WEATHER_MAP_API_KEY");
$weatherUrl = "https://api.openweathermap.org/data/2.5/weather?q=$city&units=metric&appid=$apiKey&lang=pt";
$weather = \file_get_contents($weatherUrl);
$weatherDetails = \json_decode($weather, true);
$temperature = \round($weatherDetails["main"]["temp"] ?? 0, 1);
$weatherDescription = $weatherDetails["weather"][0]["description"] ?? '';
return sendFulfillmentResponse($temperature, $weatherDescription);
}
/**
* Envia a informação do tempo para o Dialogflow
*
* @param float $temperature
* @param string $weatherDescription
*
* @return string
*/
function sendFulfillmentResponse(float $temperature, string $weatherDescription): string
{
$response = "Faz $temperature graus com $weatherDescription";
$fulfillment = [
"fulfillmentText" => $response
];
return \json_encode($fulfillment);
}
// listen to the POST request from Dialogflow
$request = \file_get_contents("php://input");
$requestJson = \json_decode($request, true);
$city = $requestJson['queryResult']['parameters']['geo-city'] ?? '';
if (isset($city) && !empty($city)) {
echo getWeatherInformation($city);
}