Skip to content

Commit

Permalink
Added command related settings. Fixed bug in date command
Browse files Browse the repository at this point in the history
  • Loading branch information
akalongman committed Jul 9, 2015
1 parent 165d88f commit 25c689b
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -49,6 +53,17 @@ public function getMessage()
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;
Expand Down
24 changes: 13 additions & 11 deletions src/Commands/DateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ 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.1.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;
if (!empty($google_api_key = $this->getConfig('google_api_key'))) {
$params.= '&key=' . $google_api_key;
}

$data = $this->request($url . $params);
Expand Down Expand Up @@ -61,11 +62,14 @@ private function getDate($lat, $lng)
{
$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;
if (!empty($google_api_key = $this->getConfig('google_api_key'))) {
$params.= '&key=' . $google_api_key;
}

$data = $this->request($url . $params);
Expand Down Expand Up @@ -100,11 +104,9 @@ private function getFormattedDate($location)

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;
}
Expand Down
42 changes: 35 additions & 7 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ class Telegram
*/
protected $pdo;


/**
* Commands config
*
* @var array
*/
protected $commands_config;





/**
* Constructor
*
Expand Down Expand Up @@ -364,13 +376,6 @@ protected function insertRequest(Update $update)

$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());
}
Expand All @@ -392,6 +397,29 @@ public function addCommandsPath($folder)
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
*
Expand Down

0 comments on commit 25c689b

Please sign in to comment.