Commit dc174281 authored by MBoretto's avatar MBoretto

Merge remote-tracking branch 'upstream/master'

parents ac19ac11 2b293902
......@@ -131,6 +131,9 @@ try {
// create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY,$BOT_NAME);
// here you can set some command specified parameters, for example, google geocode/timezone api key for date command:
$telegram->setCommandConfig('date', array('google_api_key'=>'your_google_api_key_here'));
// handle telegram webhook request
$telegram->handle();
} catch (Longman\TelegramBot\Exception\TelegramException $e) {
......
......@@ -25,9 +25,13 @@ abstract class Command
protected $enabled = true;
protected $name = '';
protected $config;
public function __construct(Telegram $telegram)
{
$this->telegram = $telegram;
$this->config = $telegram->getCommandConfig($this->name);
}
public function setUpdate(Update $update)
......@@ -49,6 +53,17 @@ abstract class Command
return $this->message;
}
public function getConfig($name = null)
{
if (isset($this->config[$name])) {
return $this->config[$name];
} else {
return null;
}
return $this->config;
}
public function getTelegram()
{
return $this->telegram;
......
......@@ -20,19 +20,22 @@ class DateCommand extends Command
protected $name = 'date';
protected $description = 'Show date/time by location';
protected $usage = '/date <location>';
protected $version = '1.0.0';
protected $version = '1.2.0';
protected $enabled = true;
private $google_api_key = '';
private $base_url = 'https://maps.googleapis.com/maps/api';
private $date_format = 'd-m-Y H:i:s';
private function getCoordinates($location)
{
$url = $this->base_url . '/geocode/json?';
$params = 'address=' . urlencode($location);
if (!empty($this->google_api_key)) {
$params.= '&key=' . $this->google_api_key;
$google_api_key = $this->getConfig('google_api_key');
if (!empty($google_api_key)) {
$params .= '&key=' . $google_api_key;
}
$data = $this->request($url . $params);
......@@ -61,11 +64,15 @@ class DateCommand extends Command
{
$url = $this->base_url . '/timezone/json?';
$timestamp = time();
$date_utc = new \DateTime(null, new \DateTimeZone("UTC"));
$timestamp = $date_utc->format('U');
$params = 'location=' . urlencode($lat) . ',' . urlencode($lng) . '&timestamp=' . urlencode($timestamp);
if (!empty($this->google_api_key)) {
$params.= '&key=' . $this->google_api_key;
$google_api_key = $this->getConfig('google_api_key');
if (!empty($google_api_key)) {
$params.= '&key=' . $google_api_key;
}
$data = $this->request($url . $params);
......@@ -100,11 +107,9 @@ class DateCommand extends Command
list($local_time, $timezone_id) = $this->getDate($lat, $lng);
$date_utc = new \DateTime(date('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id));
//$timestamp = $date_utc->format($this->date_format);
$date_utc = new \DateTime(gmdate('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id));
$return = 'The local time in ' . $timezone_id . ' is: ' . date($this->date_format, $local_time) . '';
$return = 'The local time in ' . $timezone_id . ' is: ' . $date_utc->format($this->date_format) . '';
return $return;
}
......
......@@ -30,7 +30,7 @@ class Telegram
*
* @var string
*/
protected $version = '0.0.5';
protected $version = '0.0.7';
/**
* Telegram API key
......@@ -102,6 +102,18 @@ class Telegram
*/
protected $pdo;
/**
* Commands config
*
* @var array
*/
protected $commands_config;
/**
* Constructor
*
......@@ -364,13 +376,6 @@ class Telegram
$status = $sth->execute();
/*$status = $executeQuery->execute(
array(
$update_id, $message_id, $from, $date, $chat, $forward_from,
$forward_date, $reply_to_message, $text,
)
);*/
} catch (PDOException $e) {
throw new TelegramException($e->getMessage());
}
......@@ -392,6 +397,29 @@ class Telegram
return $this;
}
/**
* Set command config
*
* @return object
*/
public function setCommandConfig($command, array $array)
{
$this->commands_config[$command] = $array;
return $this;
}
/**
* Get command config
*
* @return object
*/
public function getCommandConfig($command)
{
return isset($this->commands_config[$command]) ? $this->commands_config[$command] : array();
}
/**
* Get API KEY
*
......
......@@ -25,7 +25,7 @@ $root = realpath(dirname(dirname(__FILE__)));
* Check that --dev composer installation was done
*/
if (!file_exists($root . '/vendor/autoload.php')) {
throw new Exception(
throw new \Exception(
'Please run "php composer.phar install --dev" in root directory '
. 'to setup unit test dependencies before running the tests'
);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment