/**
  * Get weather string from weather data
  *
  * @param array $data
  *
  * @return string
  */
 private function getWeatherString(array $data)
 {
     try {
         if (!(isset($data['cod']) && $data['cod'] === 200)) {
             return '';
         }
         //http://openweathermap.org/weather-conditions
         $conditions = ['clear' => ' ☀️', 'clouds' => ' ☁️', 'rain' => ' ☔', 'drizzle' => ' ☔', 'thunderstorm' => ' ⚡️', 'snow' => ' ❄️'];
         $conditions_now = strtolower($data['weather'][0]['main']);
         return sprintf('The temperature in %s (%s) is %s°C' . "\n" . 'Current conditions are: %s%s', $data['name'], $data['sys']['country'], $data['main']['temp'], $data['weather'][0]['description'], isset($conditions[$conditions_now]) ? $conditions[$conditions_now] : '');
     } catch (Exception $e) {
         TelegramLog::error($e->getMessage());
         return '';
     }
 }
Esempio n. 2
0
 /**
  * Add a single custom commands path
  *
  * @param string $path   Custom commands path to add
  * @param bool   $before If the path should be prepended or appended to the list
  *
  * @return Telegram
  */
 public function addCommandsPath($path, $before = true)
 {
     if (!is_dir($path)) {
         TelegramLog::error('Commands path "' . $path . '" does not exist.');
     } elseif (!in_array($path, $this->commands_paths)) {
         if ($before) {
             array_unshift($this->commands_paths, $path);
         } else {
             array_push($this->commands_paths, $path);
         }
     }
     return $this;
 }
 public function testExternalStream()
 {
     $file = $this->logfiles['external'];
     $this->assertFileNotExists($file);
     $external_monolog = new Logger('bot_update_log');
     $external_monolog->pushHandler(new StreamHandler($file, Logger::ERROR));
     $external_monolog->pushHandler(new StreamHandler($file, Logger::DEBUG));
     TelegramLog::initialize($external_monolog);
     TelegramLog::error('my error');
     TelegramLog::debug('my debug');
     $this->assertFileExists($file);
     $file_contents = file_get_contents($file);
     $this->assertContains('bot_update_log.ERROR: my error', $file_contents);
     $this->assertContains('bot_update_log.DEBUG: my debug', $file_contents);
 }
Esempio n. 4
0
 /**
  * Url Shortener function
  *
  * @param  $url
  * @param  $user_id
  *
  * @return string
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public static function shortenUrl($url, $user_id)
 {
     if (empty(self::$token)) {
         return $url;
     }
     if (empty($user_id)) {
         throw new TelegramException('User id is empty!');
     }
     $cached = BotanDB::selectShortUrl($user_id, $url);
     if (!empty($cached[0]['short_url'])) {
         return $cached[0]['short_url'];
     }
     $request = str_replace(['#TOKEN', '#UID', '#URL'], [self::$token, $user_id, urlencode($url)], self::$shortener_url);
     $options = ['http' => ['ignore_errors' => true, 'timeout' => 3]];
     $context = stream_context_create($options);
     $response = @file_get_contents($request, false, $context);
     if (!filter_var($response, FILTER_VALIDATE_URL) === false) {
         BotanDB::insertShortUrl($user_id, $url, $response);
     } else {
         TelegramLog::debug('Botan.io API replied with error: ' . $response);
         return $url;
     }
     return $response;
 }
Esempio n. 5
0
 /**
  * Download file
  *
  * @param Entities\File $file
  *
  * @return boolean
  */
 public static function downloadFile(File $file)
 {
     $path = $file->getFilePath();
     //Create the directory
     $loc_path = self::$telegram->getDownloadPath() . '/' . $path;
     $dirname = dirname($loc_path);
     if (!is_dir($dirname) && !mkdir($dirname, 0755, true)) {
         throw new TelegramException('Directory ' . $dirname . ' can\'t be created');
     }
     $debug_handle = TelegramLog::getDebugLogTempStream();
     try {
         $response = self::$client->get('/file/bot' . self::$telegram->getApiKey() . '/' . $path, ['debug' => $debug_handle, 'sink' => $loc_path]);
     } catch (RequestException $e) {
         throw new TelegramException($e->getMessage());
     } finally {
         //Logging verbose debug output
         TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
     }
     return filesize($loc_path) > 0;
 }
Esempio n. 6
0
 /**
  * Download file
  *
  * @param \Longman\TelegramBot\Entities\File $file
  *
  * @return boolean
  * @throws \Longman\TelegramBot\Exception\TelegramException
  */
 public static function downloadFile(File $file)
 {
     $tg_file_path = $file->getFilePath();
     $file_path = self::$telegram->getDownloadPath() . '/' . $tg_file_path;
     $file_dir = dirname($file_path);
     //For safety reasons, first try to create the directory, then check that it exists.
     //This is in case some other process has created the folder in the meantime.
     if (!@mkdir($file_dir, 0755, true) && !is_dir($file_dir)) {
         throw new TelegramException('Directory ' . $file_dir . ' can\'t be created');
     }
     $debug_handle = TelegramLog::getDebugLogTempStream();
     try {
         self::$client->get('/file/bot' . self::$telegram->getApiKey() . '/' . $tg_file_path, ['debug' => $debug_handle, 'sink' => $file_path]);
         return filesize($file_path) > 0;
     } catch (RequestException $e) {
         return (string) $e->getResponse()->getBody();
     } finally {
         //Logging verbose debug output
         TelegramLog::endDebugLogTempStream("Verbose HTTP File Download Request output:\n%s\n");
     }
 }
 /**
  * Get date
  *
  * @param string $lat
  * @param string $lng
  *
  * @return array
  */
 private function getDate($lat, $lng)
 {
     $path = 'timezone/json';
     $date_utc = new \DateTime(null, new \DateTimeZone('UTC'));
     $timestamp = $date_utc->format('U');
     $query = ['location' => urlencode($lat) . ',' . urlencode($lng), 'timestamp' => urlencode($timestamp)];
     if ($this->google_api_key !== null) {
         $query['key'] = $this->google_api_key;
     }
     try {
         $response = $this->client->get($path, ['query' => $query]);
     } catch (RequestException $e) {
         TelegramLog::error($e->getMessage());
         return [];
     }
     if (!($data = $this->validateResponseData($response->getBody()))) {
         return [];
     }
     $local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
     return [$local_time, $data['timeZoneId']];
 }