· 6 years ago · Feb 19, 2020, 06:56 PM
1<?php
2/**
3 * This file is part of the TelegramBot package.
4 *
5 * (c) Avtandil Kikabidze aka LONGMAN <akalongman@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace Longman\TelegramBot\Commands\UserCommands;
12
13use Exception;
14use GuzzleHttp\Client;
15use GuzzleHttp\Exception\RequestException;
16use Longman\TelegramBot\Commands\UserCommand;
17use Longman\TelegramBot\Request;
18use Longman\TelegramBot\TelegramLog;
19
20/**
21 * User "/weather" command
22 *
23 * Get weather info for any place.
24 * This command requires an API key to be set via command config.
25 */
26class FichaCommand extends UserCommand
27{
28 /**
29 * @var string
30 */
31 protected $name = 'ficha';
32
33 /**
34 * @var string
35 */
36 protected $description = 'Mostra Informações do Produto';
37
38 /**
39 * @var string
40 */
41 protected $usage = '/ficha <codigo>';
42
43 /**
44 * @var string
45 */
46 protected $version = '1.2.1';
47
48 /**
49 * Base URI for OpenWeatherMap API
50 *
51 * @var string
52 */
53 private $owm_api_base_uri = 'https://api.marazul.app/produto.php?id=';
54
55 /**
56 * Get weather data using HTTP request
57 *
58 * @param string $produto
59 *
60 * @return string
61 */
62 private function getWeatherData($produto)
63 {
64 $client = new Client(['base_uri' => $this->owm_api_base_uri]);
65 $path = 'weather';
66 $query = [
67 'q' => $produto,
68 'units' => 'metric',
69 'APPID' => trim($this->getConfig('owm_api_key')),
70 ];
71
72 try {
73 $response = $client->get($path, ['query' => $query]);
74 } catch (RequestException $e) {
75 TelegramLog::error($e->getMessage());
76
77 return '';
78 }
79
80 return (string) $response->getBody();
81 }
82
83 /**
84 * Get weather string from weather data
85 *
86 * @param array $data
87 *
88 * @return string
89 */
90 private function getWeatherString(array $data)
91 {
92 try {
93 if (!(isset($data['cod']) && $data['cod'] === 200)) {
94 return '';
95 }
96
97 //http://openweathermap.org/weather-conditions
98 $conditions = [
99 'clear' => ' ☀️',
100 'clouds' => ' ☁️',
101 'rain' => ' ☔',
102 'drizzle' => ' ☔',
103 'thunderstorm' => ' ⚡️',
104 'snow' => ' ❄️',
105 ];
106 $conditions_now = strtolower($data['weather'][0]['main']);
107
108 return sprintf(
109 'The temperature in %s (%s) is %s°C' . PHP_EOL .
110 'Current conditions are: %s%s',
111 $data['cod'], //city
112 $data['nome']['comp'], //country
113 $data['larg']['gram']['rend'], //temperature
114 $data['torcao'][0]['description'], //description of weather
115 isset($conditions[$conditions_now]) ? $conditions[$conditions_now] : ''
116 );
117 } catch (Exception $e) {
118 TelegramLog::error($e->getMessage());
119
120 return '';
121 }
122 }
123
124 /**
125 * Command execute method
126 *
127 * @return \Longman\TelegramBot\Entities\ServerResponse
128 * @throws \Longman\TelegramBot\Exception\TelegramException
129 */
130 public function execute()
131 {
132 $message = $this->getMessage();
133 $chat_id = $message->getChat()->getId();
134 $text = '';
135
136 if (trim($this->getConfig('owm_api_key'))) {
137 $produto = trim($message->getText(true));
138 if ($produto !== '') {
139 if ($weather_data = json_decode($this->getWeatherData($produto), true)) {
140 $text = $this->getWeatherString($weather_data);
141 }
142 if ($text === '') {
143 $text = 'Não encontramos a Ficha do Produto: ' . $produto;
144 }
145 } else {
146 $text = 'Voce precisa informar o produto: /ficha <codigo>';
147 }
148 } else {
149 $text = 'Ajuda para conf. o bot ainda não iniciada :(';
150 }
151
152 $data = [
153 'chat_id' => $chat_id,
154 'text' => $text,
155 ];
156
157 return Request::sendMessage($data);
158 }
159}