Commit 80ce3980 authored by LONGMAN's avatar LONGMAN

Code rewrited on PSR-2 standard. Added CONTRIBUTING.md for contributors

parent e12787a9
Contributing
-------------
Before you contribute code to php-telegram-bot, please make sure it conforms to the PHPCS coding standard and that the php-telegram-bot unit tests still pass. The easiest way to contribute is to work on a checkout of the repository, or your own fork, rather than an installed PEAR version. If you do this, you can run the following commands to check if everything is ready to submit:
cd php-telegram-bot
composer update
vendor/bin/phpcs --report=full --extensions=php -p --standard=build/phpcs .
Which should give you no output, indicating that there are no coding standard errors. And then:
phpunit
Which should give you no failures or errors. You can ignore any skipped tests as these are for external tools.
\ No newline at end of file
...@@ -100,14 +100,14 @@ Create set.php and put: ...@@ -100,14 +100,14 @@ Create set.php and put:
```php ```php
<?php <?php
$loader = require __DIR__.'/vendor/autoload.php'; $loader = require \_\_DIR\_\_.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API\_KEY = 'your\_bot\_api\_key';
$BOT_NAME = 'namebot'; $BOT_NAME = 'namebot';
try { try {
// create Telegram API object // create Telegram API object
$telegram = new Longman\TelegramBot\Telegram($API_KEY, $BOT_NAME); $telegram = new Longman\TelegramBot\Telegram($API\_KEY, $BOT\_NAME);
// set webhook // set webhook
echo $telegram->setWebHook('https://yourdomain/yourpath_to_hook.php'); echo $telegram->setWebHook('https://yourdomain/yourpath_to_hook.php');
...@@ -123,9 +123,9 @@ After create hook.php and put: ...@@ -123,9 +123,9 @@ After create hook.php and put:
```php ```php
<?php <?php
$loader = require __DIR__.'/vendor/autoload.php'; $loader = require \_\_DIR\_\_.'/vendor/autoload.php';
$API_KEY = 'your_bot_api_key'; $API\_KEY = 'your\_bot\_api\_key';
$BOT_NAME = 'namebot'; $BOT_NAME = 'namebot';
try { try {
...@@ -154,11 +154,19 @@ $telegram->enableMySQL($credentials); ...@@ -154,11 +154,19 @@ $telegram->enableMySQL($credentials);
This code is available on [Github][0]. Pull requests are welcome. This code is available on [Github][0]. Pull requests are welcome.
Troubleshooting Troubleshooting
--------------- -------------
If you like living on the edge, please report any bugs you find on the [PHP Telegram Bot issues](https://github.com/akalongman/php-telegram-bot/issues) page. If you like living on the edge, please report any bugs you find on the [PHP Telegram Bot issues](https://github.com/akalongman/php-telegram-bot/issues) page.
Contributing
-------------
See [CONTRIBUTING.md](CONTRIBUTING.md) for information.
## Credits ## Credits
Created by [Avtandil Kikabidze][1]. Created by [Avtandil Kikabidze][1].
......
...@@ -17,51 +17,9 @@ ...@@ -17,51 +17,9 @@
<!-- Include some additional sniffs from the Generic standard --> <!-- Include some additional sniffs from the Generic standard -->
<rule ref="PEAR.Classes.ClassDeclaration"/> <rule ref="PSR2">
<rule ref="Squiz.Classes.SelfMemberReference"/> <exclude name="PEAR.Functions.FunctionCallSignature"/>
<exclude name="PEAR.Functions.FunctionCallSignature.SpaceAfterCloseBracket"/>
<rule ref="PEAR.Commenting.InlineComment"/>
<rule ref="Generic.Files.LineEndings"/>
<rule ref="PEAR.Formatting.MultiLineAssignment"/>
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
<rule ref="PEAR.NamingConventions.ValidClassName"/>
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
<rule ref="Generic.PHP.DisallowShortOpenTag"/>
<rule ref="Generic.PHP.DeprecatedFunctions"/>
<rule ref="Generic.PHP.ForbiddenFunctions"/>
<rule ref="Squiz.Operators.IncrementDecrementUsage">
<exclude name="Squiz.Operators.IncrementDecrementUsage.processAssignment"/>
</rule>
<rule ref="Squiz.Scope.StaticThisUsage"/>
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/>
<!-- Lines can be 85 chars long, but never show errors -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="150"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
<!-- Use Unix newlines -->
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>
</properties>
</rule>
<!-- This message is not required as spaces are allowed for alignment -->
<rule ref="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma">
<severity>0</severity>
</rule> </rule>
</ruleset> </ruleset>
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "02634aa637830c6e9324925abceb4624", "hash": "e4b463e1eea6333a1eaf38457a54b29c",
"packages": [ "packages": [
{ {
"name": "hoa/compiler", "name": "hoa/compiler",
...@@ -1868,7 +1868,7 @@ ...@@ -1868,7 +1868,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=5.3.0", "php": ">=5.4.0",
"ext-pdo": "*" "ext-pdo": "*"
}, },
"platform-dev": [] "platform-dev": []
......
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
...@@ -24,55 +25,63 @@ abstract class Command ...@@ -24,55 +25,63 @@ abstract class Command
protected $enabled = true; protected $enabled = true;
protected $name = ''; protected $name = '';
public function __construct(Telegram $telegram) { public function __construct(Telegram $telegram)
{
$this->telegram = $telegram; $this->telegram = $telegram;
} }
public function setUpdate(Update $update) { public function setUpdate(Update $update)
{
$this->update = $update; $this->update = $update;
$this->message = $this->update->getMessage(); $this->message = $this->update->getMessage();
return $this; return $this;
} }
abstract public function execute();
public abstract function execute(); public function getUpdate()
{
public function getUpdate() {
return $this->update; return $this->update;
} }
public function getMessage() { public function getMessage()
{
return $this->message; return $this->message;
} }
public function getTelegram()
public function getTelegram() { {
return $this->telegram; return $this->telegram;
} }
public function setCommand($command) { public function setCommand($command)
{
$this->command = $command; $this->command = $command;
return $this; return $this;
} }
public function getUsage() { public function getUsage()
{
return $this->usage; return $this->usage;
} }
public function getVersion() { public function getVersion()
{
return $this->version; return $this->version;
} }
public function getDescription() { public function getDescription()
{
return $this->description; return $this->description;
} }
public function getName() { public function getName()
{
return $this->name; return $this->name;
} }
public function isEnabled() { public function isEnabled()
{
return $this->enabled; return $this->enabled;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -8,7 +9,7 @@ ...@@ -8,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
* *
* Written by Marco Boretto <marco.bore@gmail.com> * Written by Marco Boretto <marco.bore@gmail.com>
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
...@@ -23,17 +24,15 @@ class CalcCommand extends Command ...@@ -23,17 +24,15 @@ class CalcCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
public function execute()
public function execute() { {
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
...@@ -43,13 +42,11 @@ class CalcCommand extends Command ...@@ -43,13 +42,11 @@ class CalcCommand extends Command
return $result; return $result;
} }
protected function compute($expression)
protected function compute($expression) { {
// Load the compiler // Load the compiler
$compiler = \Hoa\Compiler\Llk::load( $compiler = \Hoa\Compiler\Llk::load(new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp'));
new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
);
// Load the visitor, aka the "evaluator" // Load the visitor, aka the "evaluator"
$visitor = new \Hoa\Math\Visitor\Arithmetic(); $visitor = new \Hoa\Math\Visitor\Arithmetic();
...@@ -62,7 +59,4 @@ class CalcCommand extends Command ...@@ -62,7 +59,4 @@ class CalcCommand extends Command
return $result; return $result;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,12 +7,13 @@ ...@@ -6,12 +7,13 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
use Longman\TelegramBot\Command; use Longman\TelegramBot\Command;
use Longman\TelegramBot\Entities\Update; use Longman\TelegramBot\Entities\Update;
use Longman\TelegramBot\Exception\TelegramException;
class DateCommand extends Command class DateCommand extends Command
{ {
...@@ -21,24 +23,19 @@ class DateCommand extends Command ...@@ -21,24 +23,19 @@ class DateCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
private $google_api_key = ''; private $google_api_key = '';
private $base_url = 'https://maps.googleapis.com/maps/api'; private $base_url = 'https://maps.googleapis.com/maps/api';
private $date_format = 'd-m-Y H:i:s'; private $date_format = 'd-m-Y H:i:s';
private function getCoordinates($location)
{
$url = $this->base_url . '/geocode/json?';
$params = 'address=' . urlencode($location);
private function getCoordinates($location) {
$url = $this->base_url.'/geocode/json?';
$params = 'address='.urlencode($location);
if (!empty($this->google_api_key)) { if (!empty($this->google_api_key)) {
$params .= '&key='.$this->google_api_key; $params.= '&key=' . $this->google_api_key;
} }
$data = $this->request($url.$params); $data = $this->request($url . $params);
if (empty($data)) { if (empty($data)) {
return false; return false;
} }
...@@ -60,21 +57,18 @@ class DateCommand extends Command ...@@ -60,21 +57,18 @@ class DateCommand extends Command
return array($lat, $lng, $acc, $types); return array($lat, $lng, $acc, $types);
} }
private function getDate($lat, $lng)
{
private function getDate($lat, $lng) { $url = $this->base_url . '/timezone/json?';
$url = $this->base_url.'/timezone/json?';
$timestamp = time(); $timestamp = time();
$params = 'location=' . urlencode($lat) . ',' . urlencode($lng) . '&timestamp=' . urlencode($timestamp);
$params = 'location='.urlencode($lat).','.urlencode($lng).'&timestamp='.urlencode($timestamp);
if (!empty($this->google_api_key)) { if (!empty($this->google_api_key)) {
$params .= '&key='.$this->google_api_key; $params.= '&key=' . $this->google_api_key;
} }
$data = $this->request($url.$params); $data = $this->request($url . $params);
if (empty($data)) { if (empty($data)) {
return false; return false;
} }
...@@ -88,87 +82,75 @@ class DateCommand extends Command ...@@ -88,87 +82,75 @@ class DateCommand extends Command
return false; return false;
} }
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset']; $local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
return array($local_time, $data['timeZoneId']); return array($local_time, $data['timeZoneId']);
} }
private function getFormattedDate($location) { private function getFormattedDate($location)
{
if (empty($location)) { if (empty($location)) {
return 'The time in nowhere is never'; return 'The time in nowhere is never';
} }
list($lat, $lng, $acc, $types) = $this->getCoordinates($location); list($lat, $lng, $acc, $types) = $this->getCoordinates($location);
if (empty($lat) || empty($lng)) { if (empty($lat) || empty($lng)) {
return 'It seems that in "'.$location.'" they do not have a concept of time.'; return 'It seems that in "' . $location . '" they do not have a concept of time.';
} }
list($local_time, $timezone_id) = $this->getDate($lat, $lng); 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)); $date_utc = new \DateTime(date('Y-m-d H:i:s', $local_time), new \DateTimeZone($timezone_id));
//$timestamp = $date_utc->format($this->date_format); //$timestamp = $date_utc->format($this->date_format);
$return = 'The local time in '.$timezone_id.' is: '.date($this->date_format, $local_time).''; $return = 'The local time in ' . $timezone_id . ' is: ' . date($this->date_format, $local_time) . '';
return $return; return $return;
} }
private function request($url)
private function request($url) { {
$ch = curl_init(); $ch = curl_init();
$curlConfig = array( $curlConfig = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true,);
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($ch, $curlConfig); curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch); $response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code !== 200) { if ($http_code !== 200) {
throw new \Exception('Error receiving data from url'); throw new TelegramException('Error receiving data from url');
} }
curl_close($ch); curl_close($ch);
return $response; return $response;
} }
public function execute()
public function execute() { {
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text = 'You must specify location in format: /date <city>'; $text = 'You must specify location in format: /date <city>';
} else { } else {
$date = $this->getformattedDate($text); $date = $this->getformattedDate($text);
if (empty($date)) { if (empty($date)) {
$text = 'Can not find date for location: '.$text; $text = 'Can not find date for location: ' . $text;
} else { } else {
$text = $date; $text = $date;
} }
} }
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = $text; $data['text'] = $text;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
...@@ -21,25 +22,19 @@ class EchoCommand extends Command ...@@ -21,25 +22,19 @@ class EchoCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
public function execute() { public function execute()
{
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$text = $message->getText(true); $text = $message->getText(true);
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['text'] = $text; $data['text'] = $text;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result; return $result;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
...@@ -21,52 +22,45 @@ class HelpCommand extends Command ...@@ -21,52 +22,45 @@ class HelpCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
public function execute()
public function execute() { {
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
$commands = $this->telegram->getCommandsList(); $commands = $this->telegram->getCommandsList();
if (empty($text)) { if (empty($text)) {
$msg = 'GeoBot v. '.$this->telegram->getVersion()."\n\n"; $msg = 'GeoBot v. ' . $this->telegram->getVersion() . "\n\n";
$msg .= 'Commands List:'."\n"; $msg.= 'Commands List:' . "\n";
foreach($commands as $command) { foreach ($commands as $command) {
if (!$command->isEnabled()) { if (!$command->isEnabled()) {
continue; continue;
} }
$msg .= '/'.$command->getName().' - '.$command->getDescription()."\n"; $msg.= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n";
} }
$msg .= "\n".'For exact command help type: /help <command>'; $msg.= "\n" . 'For exact command help type: /help <command>';
} else { } else {
$text = str_replace('/', '', $text); $text = str_replace('/', '', $text);
if (isset($commands[$text])) { if (isset($commands[$text])) {
$command = $commands[$text]; $command = $commands[$text];
$msg = 'Command: '.$command->getName().' v'.$command->getVersion()."\n"; $msg = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
$msg .= 'Description: '.$command->getDescription()."\n"; $msg.= 'Description: ' . $command->getDescription() . "\n";
$msg .= 'Usage: '.$command->getUsage(); $msg.= 'Usage: ' . $command->getUsage();
} else { } else {
$msg = 'Command '.$text.' not found'; $msg = 'Command ' . $text . ' not found';
} }
} }
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = $msg; $data['text'] = $msg;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result; return $result;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
...@@ -21,16 +22,16 @@ class WeatherCommand extends Command ...@@ -21,16 +22,16 @@ class WeatherCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
private function getWeather($location)
private function getWeather($location) { {
$url = 'http://api.openweathermap.org/data/2.5/weather?q='.$location.'&units=metric'; $url = 'http://api.openweathermap.org/data/2.5/weather?q=' . $location . '&units=metric';
$ch = curl_init(); $ch = curl_init();
$curlConfig = array( $curlConfig = array(CURLOPT_URL => $url,
CURLOPT_URL => $url,
//CURLOPT_POST => true, //CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
//CURLOPT_HTTPHEADER => array('Content-Type: text/plain'), //CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
//CURLOPT_POSTFIELDS => $data //CURLOPT_POSTFIELDS => $data
//CURLOPT_VERBOSE => true, //CURLOPT_VERBOSE => true,
...@@ -46,13 +47,11 @@ class WeatherCommand extends Command ...@@ -46,13 +47,11 @@ class WeatherCommand extends Command
} }
curl_close($ch); curl_close($ch);
return $response; return $response;
} }
private function getWeatherString($location)
private function getWeatherString($location) { {
if (empty($location)) { if (empty($location)) {
return false; return false;
} }
...@@ -66,63 +65,56 @@ class WeatherCommand extends Command ...@@ -66,63 +65,56 @@ class WeatherCommand extends Command
} }
$city = $decode['name']; $city = $decode['name'];
$country = $decode['sys']['country']; $country = $decode['sys']['country'];
$temp = 'The temperature in '.$city .' (' .$country.') is '.$decode['main']['temp'].'°C'; $temp = 'The temperature in ' . $city . ' (' . $country . ') is ' . $decode['main']['temp'] . '°C';
$conditions = 'Current conditions are: '.$decode['weather'][0]['description']; $conditions = 'Current conditions are: ' . $decode['weather'][0]['description'];
switch(strtolower($decode['weather'][0]['main'])) { switch (strtolower($decode['weather'][0]['main'])) {
case 'clear': case 'clear':
$conditions .= ' ☀'; $conditions.= ' ☀';
break; break;
case 'clouds': case 'clouds':
$conditions .= ' ☁☁'; $conditions.= ' ☁☁';
break; break;
case 'rain': case 'rain':
$conditions .= ' ☔'; $conditions.= ' ☔';
break; break;
case 'thunderstorm': case 'thunderstorm':
$conditions .= ' ☔☔☔☔'; $conditions.= ' ☔☔☔☔';
break; break;
} }
return $temp . "\n" . $conditions;
return $temp."\n".$conditions;
} }
public function execute() { public function execute()
{
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
if (empty($text)) { if (empty($text)) {
$text = 'You must specify location in format: /weather <city>'; $text = 'You must specify location in format: /weather <city>';
} else { } else {
$weather = $this->getWeatherString($text); $weather = $this->getWeatherString($text);
if (empty($weather)) { if (empty($weather)) {
$text = 'Can not find weather for location: '.$text; $text = 'Can not find weather for location: ' . $text;
} else { } else {
$text = $weather; $text = $weather;
} }
} }
$data = array(); $data = array();
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = $text; $data['text'] = $text;
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result; return $result;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -8,7 +9,7 @@ ...@@ -8,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
* *
* Written by Marco Boretto <marco.bore@gmail.com> * Written by Marco Boretto <marco.bore@gmail.com>
*/ */
namespace Longman\TelegramBot\Commands; namespace Longman\TelegramBot\Commands;
...@@ -24,12 +25,11 @@ class WhoamiCommand extends Command ...@@ -24,12 +25,11 @@ class WhoamiCommand extends Command
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
public function execute() { public function execute()
{
$update = $this->getUpdate(); $update = $this->getUpdate();
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $text = $message->getText(true);
...@@ -38,14 +38,11 @@ class WhoamiCommand extends Command ...@@ -38,14 +38,11 @@ class WhoamiCommand extends Command
$data['chat_id'] = $chat_id; $data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $data['reply_to_message_id'] = $message_id;
$data['text'] = 'Your Id: ' . $message->getFrom()->getId(); $data['text'] = 'Your Id: ' . $message->getFrom()->getId();
$data['text'] .= "\n".'Name: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName(); $data['text'] .= "\n" . 'Name: ' . $message->getFrom()->getFirstName()
$data['text'] .= "\n".'Username: ' . $message->getFrom()->getUsername(); . ' ' . $message->getFrom()->getLastName();
$data['text'] .= "\n" . 'Username: ' . $message->getFrom()->getUsername();
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result; return $result;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
...@@ -20,7 +21,8 @@ class Chat extends Entity ...@@ -20,7 +21,8 @@ class Chat extends Entity
protected $last_name; protected $last_name;
protected $username; protected $username;
public function __construct(array $data) { public function __construct(array $data)
{
$this->id = isset($data['id']) ? $data['id'] : null; $this->id = isset($data['id']) ? $data['id'] : null;
if (empty($this->id)) { if (empty($this->id)) {
...@@ -31,35 +33,35 @@ class Chat extends Entity ...@@ -31,35 +33,35 @@ class Chat extends Entity
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null; $this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null; $this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null; $this->username = isset($data['username']) ? $data['username'] : null;
} }
public function getId()
public function getId() { {
return $this->id; return $this->id;
} }
public function getTitle()
public function getTitle() { {
return $this->title; return $this->title;
} }
public function getFirstName() { public function getFirstName()
{
return $this->first_name; return $this->first_name;
} }
public function getLastName() { public function getLastName()
{
return $this->last_name; return $this->last_name;
} }
public function getUsername()
public function getUsername() { {
return $this->username; return $this->username;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,21 +7,20 @@ ...@@ -6,21 +7,20 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
class Entity class Entity
{ {
public function toJSON()
public function toJSON() { {
$reflection = new \ReflectionObject($this); $reflection = new \ReflectionObject($this);
$properties = $reflection->getProperties(); $properties = $reflection->getProperties();
$fields = array(); $fields = array();
foreach($properties as $property) { foreach ($properties as $property) {
$name = $property->getName(); $name = $property->getName();
$property->setAccessible(true); $property->setAccessible(true);
$value = $property->getValue($this); $value = $property->getValue($this);
...@@ -31,11 +31,4 @@ class Entity ...@@ -31,11 +31,4 @@ class Entity
return $json; return $json;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,13 +7,11 @@ ...@@ -6,13 +7,11 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
class Message extends Entity class Message extends Entity
{ {
protected $message_id; protected $message_id;
...@@ -57,12 +56,12 @@ class Message extends Entity ...@@ -57,12 +56,12 @@ class Message extends Entity
protected $group_chat_created; protected $group_chat_created;
protected $command;
protected $_command;
protected $bot_name; protected $bot_name;
public function __construct(array $data, $bot_name) { public function __construct(array $data, $bot_name)
{
$this->bot_name = $bot_name; $this->bot_name = $bot_name;
...@@ -77,7 +76,6 @@ class Message extends Entity ...@@ -77,7 +76,6 @@ class Message extends Entity
} }
$this->from = new User($this->from); $this->from = new User($this->from);
$this->date = isset($data['date']) ? $data['date'] : null; $this->date = isset($data['date']) ? $data['date'] : null;
if (empty($this->date)) { if (empty($this->date)) {
throw new TelegramException('date is empty!'); throw new TelegramException('date is empty!');
...@@ -89,10 +87,8 @@ class Message extends Entity ...@@ -89,10 +87,8 @@ class Message extends Entity
} }
$this->chat = new Chat($this->chat); $this->chat = new Chat($this->chat);
$this->text = isset($data['text']) ? $data['text'] : null; $this->text = isset($data['text']) ? $data['text'] : null;
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
if (!empty($this->forward_from)) { if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from); $this->forward_from = new User($this->forward_from);
...@@ -104,20 +100,19 @@ class Message extends Entity ...@@ -104,20 +100,19 @@ class Message extends Entity
if (!empty($this->reply_to_message)) { if (!empty($this->reply_to_message)) {
$this->reply_to_message = new Message($this->reply_to_message); $this->reply_to_message = new Message($this->reply_to_message);
} }
} }
//return the entire command like /echo or /echo@bot1 if specified //return the entire command like /echo or /echo@bot1 if specified
public function getFullCommand(){ public function getFullCommand()
{
return strtok($this->text, ' '); return strtok($this->text, ' ');
} }
public function getCommand()
public function getCommand() { {
if (!empty($this->_command)) { if (!empty($this->command)) {
return $this->_command; return $this->command;
} }
$cmd = $this->getFullCommand(); $cmd = $this->getFullCommand();
...@@ -129,67 +124,68 @@ class Message extends Entity ...@@ -129,67 +124,68 @@ class Message extends Entity
$split_cmd = explode('@', $cmd); $split_cmd = explode('@', $cmd);
if (isset($split_cmd[1])) { if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me //command is followed by name check if is addressed to me
if (strtolower($split_cmd[1]) == strtolower($this->bot_name)){ if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) {
return $this->_command = $split_cmd[0]; return $this->command = $split_cmd[0];
} }
} else { } else {
//command is not followed by name //command is not followed by name
return $this->_command = $cmd; return $this->command = $cmd;
} }
} }
return false; return false;
} }
public function getMessageId()
{
public function getMessageId() {
return $this->message_id; return $this->message_id;
} }
public function getDate() { public function getDate()
{
return $this->date; return $this->date;
} }
public function getFrom() { public function getFrom()
{
return $this->from; return $this->from;
} }
public function getChat()
public function getChat() { {
return $this->chat; return $this->chat;
} }
public function getForwardFrom() { public function getForwardFrom()
{
return $this->forward_from; return $this->forward_from;
} }
public function getForwardDate() { public function getForwardDate()
{
return $this->forward_date; return $this->forward_date;
} }
public function getReplyToMessage() { public function getReplyToMessage()
{
return $this->reply_to_message; return $this->reply_to_message;
} }
public function getText($without_cmd = false) { public function getText($without_cmd = false)
{
$text = $this->text; $text = $this->text;
if ($without_cmd) { if ($without_cmd) {
$command = $this->getFullCommand(); $command = $this->getFullCommand();
$text = substr($text, strlen($command.' '), strlen($text)); $text = substr($text, strlen($command . ' '), strlen($text));
} }
return $text; return $text;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,12 +7,11 @@ ...@@ -6,12 +7,11 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
class Update extends Entity class Update extends Entity
{ {
...@@ -19,10 +19,8 @@ class Update extends Entity ...@@ -19,10 +19,8 @@ class Update extends Entity
protected $message; protected $message;
protected $bot_name; protected $bot_name;
public function __construct(array $data, $bot_name)
{
public function __construct(array $data, $bot_name) {
$update_id = isset($data['update_id']) ? $data['update_id'] : null; $update_id = isset($data['update_id']) ? $data['update_id'] : null;
...@@ -35,20 +33,17 @@ class Update extends Entity ...@@ -35,20 +33,17 @@ class Update extends Entity
$this->bot_name = $bot_name; $this->bot_name = $bot_name;
$this->update_id = $update_id; $this->update_id = $update_id;
$this->message = new Message($message, $bot_name); $this->message = new Message($message, $bot_name);
} }
public function getUpdateId() { public function getUpdateId()
{
return $this->update_id; return $this->update_id;
} }
public function getMessage()
public function getMessage() { {
return $this->message; return $this->message;
} }
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Entities; namespace Longman\TelegramBot\Entities;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
...@@ -19,10 +20,8 @@ class User extends Entity ...@@ -19,10 +20,8 @@ class User extends Entity
protected $last_name; protected $last_name;
protected $username; protected $username;
public function __construct(array $data)
{
public function __construct(array $data) {
$this->id = isset($data['id']) ? $data['id'] : null; $this->id = isset($data['id']) ? $data['id'] : null;
if (empty($this->id)) { if (empty($this->id)) {
...@@ -36,35 +35,29 @@ class User extends Entity ...@@ -36,35 +35,29 @@ class User extends Entity
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null; $this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null; $this->username = isset($data['username']) ? $data['username'] : null;
} }
public function getId()
public function getId() { {
return $this->id; return $this->id;
} }
public function getFirstName() { public function getFirstName()
{
return $this->first_name; return $this->first_name;
} }
public function getLastName() { public function getLastName()
{
return $this->last_name; return $this->last_name;
} }
public function getUsername()
public function getUsername() { {
return $this->username; return $this->username;
} }
} }
...@@ -6,13 +6,9 @@ ...@@ -6,13 +6,9 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot\Exception; namespace Longman\TelegramBot\Exception;
class TelegramException extends \Exception class TelegramException extends \Exception
{ {
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,7 +7,7 @@ ...@@ -6,7 +7,7 @@
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Longman\TelegramBot; namespace Longman\TelegramBot;
use Longman\TelegramBot\Exception\TelegramException; use Longman\TelegramBot\Exception\TelegramException;
...@@ -32,14 +33,13 @@ class Request ...@@ -32,14 +33,13 @@ class Request
'setWebhook', 'setWebhook',
); );
public static function initialize(Telegram $telegram)
{
public static function initialize(Telegram $telegram) {
self::$telegram = $telegram; self::$telegram = $telegram;
} }
public static function getInput()
public static function getInput() { {
if ($update = self::$telegram->getCustomUpdate()) { if ($update = self::$telegram->getCustomUpdate()) {
self::$input = $update; self::$input = $update;
} else { } else {
...@@ -49,8 +49,8 @@ class Request ...@@ -49,8 +49,8 @@ class Request
return self::$input; return self::$input;
} }
private static function log()
private static function log() { {
if (!self::$telegram->getLogRequests()) { if (!self::$telegram->getLogRequests()) {
return false; return false;
} }
...@@ -59,33 +59,31 @@ class Request ...@@ -59,33 +59,31 @@ class Request
return false; return false;
} }
$status = file_put_contents($path, self::$input."\n", FILE_APPEND); $status = file_put_contents($path, self::$input . "\n", FILE_APPEND);
return $status; return $status;
} }
public static function send($action, array $data = null)
{
public static function send($action, array $data = null) {
if (defined('PHPUNIT_TESTSUITE')) { if (defined('PHPUNIT_TESTSUITE')) {
return $data; return $data;
} }
$ch = curl_init(); $ch = curl_init();
$curlConfig = array( $curlConfig = array(
CURLOPT_URL => 'https://api.telegram.org/bot'.self::$telegram->getApiKey().'/'.$action, CURLOPT_URL => 'https://api.telegram.org/bot' . self::$telegram->getApiKey() . '/' . $action,
CURLOPT_POST => true, CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true CURLOPT_RETURNTRANSFER => true
); );
if (!empty($data)) { if (!empty($data)) {
if (!empty($data['text']) && substr($data['text'], 0, 1) === '@') { if (!empty($data['text']) && substr($data['text'], 0, 1) === '@') {
$data['text'] = ' '.$data['text']; $data['text'] = ' ' . $data['text'];
} }
$curlConfig[CURLOPT_POSTFIELDS] = $data; $curlConfig[CURLOPT_POSTFIELDS] = $data;
} }
curl_setopt_array($ch, $curlConfig); curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch); $result = curl_exec($ch);
curl_close($ch); curl_close($ch);
...@@ -93,9 +91,8 @@ class Request ...@@ -93,9 +91,8 @@ class Request
return !empty($result) ? json_decode($result, true) : false; return !empty($result) ? json_decode($result, true) : false;
} }
public static function sendMessage(array $data)
{
public static function sendMessage(array $data) {
if (empty($data)) { if (empty($data)) {
throw new TelegramException('Data is empty!'); throw new TelegramException('Data is empty!');
...@@ -105,14 +102,9 @@ class Request ...@@ -105,14 +102,9 @@ class Request
return $result; return $result;
} }
public static function setWebhook($url)
public static function setWebhook($url) { {
$result = self::send('setWebhook', array('url'=>$url)); $result = self::send('setWebhook', array('url' => $url));
return $result; return $result;
} }
} }
This diff is collapsed.
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