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].
......
<?xml version="1.0"?> <?xml version="1.0"?>
<ruleset name="Package"> <ruleset name="Package">
<description>The Package coding standard.</description> <description>The Package coding standard.</description>
<!-- Exclude folders not containing production code --> <!-- Exclude folders not containing production code -->
<exclude-pattern type="relative">build/*</exclude-pattern> <exclude-pattern type="relative">build/*</exclude-pattern>
<exclude-pattern type="relative">docs/*</exclude-pattern> <exclude-pattern type="relative">docs/*</exclude-pattern>
<exclude-pattern type="relative">tests/*</exclude-pattern> <exclude-pattern type="relative">tests/*</exclude-pattern>
<exclude-pattern type="relative">cache/*</exclude-pattern> <exclude-pattern type="relative">cache/*</exclude-pattern>
<exclude-pattern type="relative">tmp/*</exclude-pattern> <exclude-pattern type="relative">tmp/*</exclude-pattern>
<exclude-pattern type="relative">logs/*</exclude-pattern> <exclude-pattern type="relative">logs/*</exclude-pattern>
<!-- Exclude 3rd party libraries and Framework code. --> <!-- Exclude 3rd party libraries and Framework code. -->
<exclude-pattern type="relative">vendor/*</exclude-pattern> <exclude-pattern type="relative">vendor/*</exclude-pattern>
<!-- Include all sniffs in an external standard directory --> <!-- Include all sniffs in an external standard directory -->
<!-- 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>
<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>
</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,73 +7,81 @@ ...@@ -6,73 +7,81 @@
* *
* 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;
abstract class Command abstract class Command
{ {
protected $telegram; protected $telegram;
protected $update; protected $update;
protected $message; protected $message;
protected $command; protected $command;
protected $description = 'Command help'; protected $description = 'Command help';
protected $usage = 'Command usage'; protected $usage = 'Command usage';
protected $version = '1.0.0'; protected $version = '1.0.0';
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) {
$this->update = $update; public function setUpdate(Update $update)
$this->message = $this->update->getMessage(); {
return $this; $this->update = $update;
} $this->message = $this->update->getMessage();
return $this;
}
public abstract function execute();
abstract public function execute();
public function getUpdate() { public function getUpdate()
return $this->update; {
} return $this->update;
}
public function getMessage() {
return $this->message; public function getMessage()
} {
return $this->message;
}
public function getTelegram() {
return $this->telegram; public function getTelegram()
} {
return $this->telegram;
public function setCommand($command) { }
$this->command = $command;
return $this; public function setCommand($command)
} {
$this->command = $command;
public function getUsage() { return $this;
return $this->usage; }
}
public function getUsage()
public function getVersion() { {
return $this->version; return $this->usage;
} }
public function getDescription() { public function getVersion()
return $this->description; {
} return $this->version;
}
public function getName() {
return $this->name; public function getDescription()
} {
return $this->description;
public function isEnabled() { }
return $this->enabled;
} public function getName()
{
return $this->name;
}
public function isEnabled()
{
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;
...@@ -17,52 +18,45 @@ use Longman\TelegramBot\Entities\Update; ...@@ -17,52 +18,45 @@ use Longman\TelegramBot\Entities\Update;
class CalcCommand extends Command class CalcCommand extends Command
{ {
protected $name = 'calc'; protected $name = 'calc';
protected $description = 'Calculate math expression'; protected $description = 'Calculate math expression';
protected $usage = '/calc <expression>'; protected $usage = '/calc <expression>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; protected $enabled = true;
public function execute() {
$update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId(); public function execute()
$message_id = $message->getMessageId(); {
$text = $message->getText(true); $update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$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;
$data['text'] = $this->compute($text); $data['text'] = $this->compute($text);
$result = Request::sendMessage($data); $result = Request::sendMessage($data);
return $result; return $result;
} }
protected function compute($expression)
{
protected function compute($expression) { // Load the compiler
$compiler = \Hoa\Compiler\Llk::load(new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp'));
// Load the compiler // Load the visitor, aka the "evaluator"
$compiler = \Hoa\Compiler\Llk::load( $visitor = new \Hoa\Math\Visitor\Arithmetic();
new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')
);
// Load the visitor, aka the "evaluator" // Parse the expression
$visitor = new \Hoa\Math\Visitor\Arithmetic(); $ast = $compiler->parse($expression);
// Parse the expression
$ast = $compiler->parse($expression);
// Evaluate
$result = $visitor->visit($ast);
return $result;
}
// Evaluate
$result = $visitor->visit($ast);
return $result;
}
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,169 +7,150 @@ ...@@ -6,169 +7,150 @@
* *
* 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
{ {
protected $name = 'date'; protected $name = 'date';
protected $description = 'Show date/time by location'; protected $description = 'Show date/time by location';
protected $usage = '/date <location>'; protected $usage = '/date <location>';
protected $version = '1.0.0'; protected $version = '1.0.0';
protected $enabled = true; 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;
}
$data = $this->request($url.$params);
if (empty($data)) {
return false;
}
$data = json_decode($data, true);
if (empty($data)) {
return false;
}
if ($data['status'] !== 'OK') {
return false;
}
$lat = $data['results'][0]['geometry']['location']['lat'];
$lng = $data['results'][0]['geometry']['location']['lng'];
$acc = $data['results'][0]['geometry']['location_type'];
$types = $data['results'][0]['types'];
return array($lat, $lng, $acc, $types);
}
private function getDate($lat, $lng) {
$url = $this->base_url.'/timezone/json?';
$timestamp = time();
$params = 'location='.urlencode($lat).','.urlencode($lng).'&timestamp='.urlencode($timestamp);
if (!empty($this->google_api_key)) {
$params .= '&key='.$this->google_api_key;
}
$data = $this->request($url.$params);
if (empty($data)) {
return false;
}
$data = json_decode($data, true);
if (empty($data)) {
return false;
}
if ($data['status'] !== 'OK') {
return false;
}
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
return array($local_time, $data['timeZoneId']);
}
private function getFormattedDate($location) {
if (empty($location)) {
return 'The time in nowhere is never';
}
list($lat, $lng, $acc, $types) = $this->getCoordinates($location);
if (empty($lat) || empty($lng)) {
return 'It seems that in "'.$location.'" they do not have a concept of time.';
}
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);
$return = 'The local time in '.$timezone_id.' is: '.date($this->date_format, $local_time).'';
return $return;
}
private function request($url) {
$ch = curl_init();
$curlConfig = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code !== 200) {
throw new \Exception('Error receiving data from url');
}
curl_close($ch);
return $response;
}
public function execute() {
$update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
if (empty($text)) {
$text = 'You must specify location in format: /date <city>';
} else {
$date = $this->getformattedDate($text);
if (empty($date)) {
$text = 'Can not find date for location: '.$text;
} else {
$text = $date;
}
}
$data = array(); private $google_api_key = '';
$data['chat_id'] = $chat_id; private $base_url = 'https://maps.googleapis.com/maps/api';
$data['reply_to_message_id'] = $message_id; private $date_format = 'd-m-Y H:i:s';
$data['text'] = $text;
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;
}
$result = Request::sendMessage($data); $data = $this->request($url . $params);
} if (empty($data)) {
return false;
}
$data = json_decode($data, true);
if (empty($data)) {
return false;
}
if ($data['status'] !== 'OK') {
return false;
}
$lat = $data['results'][0]['geometry']['location']['lat'];
$lng = $data['results'][0]['geometry']['location']['lng'];
$acc = $data['results'][0]['geometry']['location_type'];
$types = $data['results'][0]['types'];
return array($lat, $lng, $acc, $types);
}
private function getDate($lat, $lng)
{
$url = $this->base_url . '/timezone/json?';
$timestamp = time();
$params = 'location=' . urlencode($lat) . ',' . urlencode($lng) . '&timestamp=' . urlencode($timestamp);
if (!empty($this->google_api_key)) {
$params.= '&key=' . $this->google_api_key;
}
$data = $this->request($url . $params);
if (empty($data)) {
return false;
}
$data = json_decode($data, true);
if (empty($data)) {
return false;
}
if ($data['status'] !== 'OK') {
return false;
}
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset'];
return array($local_time, $data['timeZoneId']);
}
private function getFormattedDate($location)
{
if (empty($location)) {
return 'The time in nowhere is never';
}
list($lat, $lng, $acc, $types) = $this->getCoordinates($location);
if (empty($lat) || empty($lng)) {
return 'It seems that in "' . $location . '" they do not have a concept of time.';
}
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);
$return = 'The local time in ' . $timezone_id . ' is: ' . date($this->date_format, $local_time) . '';
return $return;
}
private function request($url)
{
$ch = curl_init();
$curlConfig = array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true,);
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code !== 200) {
throw new TelegramException('Error receiving data from url');
}
curl_close($ch);
return $response;
}
public function execute()
{
$update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
if (empty($text)) {
$text = 'You must specify location in format: /date <city>';
} else {
$date = $this->getformattedDate($text);
if (empty($date)) {
$text = 'Can not find date for location: ' . $text;
} else {
$text = $date;
}
}
$data = array();
$data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id;
$data['text'] = $text;
$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;
...@@ -15,31 +16,25 @@ use Longman\TelegramBot\Entities\Update; ...@@ -15,31 +16,25 @@ use Longman\TelegramBot\Entities\Update;
class EchoCommand extends Command class EchoCommand extends Command
{ {
protected $name = 'echo'; protected $name = 'echo';
protected $description = 'Show text'; protected $description = 'Show text';
protected $usage = '/echo <text>'; protected $usage = '/echo <text>';
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(); {
$message = $this->getMessage(); $update = $this->getUpdate();
$message = $this->getMessage();
$chat_id = $message->getChat()->getId();
$chat_id = $message->getChat()->getId(); $text = $message->getText(true);
$text = $message->getText(true);
$data = array();
$data['chat_id'] = $chat_id;
$data = array(); $data['text'] = $text;
$data['chat_id'] = $chat_id;
$data['text'] = $text; $result = Request::sendMessage($data);
return $result;
}
$result = Request::sendMessage($data);
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;
...@@ -15,58 +16,51 @@ use Longman\TelegramBot\Entities\Update; ...@@ -15,58 +16,51 @@ use Longman\TelegramBot\Entities\Update;
class HelpCommand extends Command class HelpCommand extends Command
{ {
protected $name = 'help'; protected $name = 'help';
protected $description = 'Show bot commands help'; protected $description = 'Show bot commands help';
protected $usage = '/help or /help <command>'; protected $usage = '/help or /help <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();
$message_id = $message->getMessageId();
$chat_id = $message->getChat()->getId(); $text = $message->getText(true);
$message_id = $message->getMessageId();
$text = $message->getText(true); $commands = $this->telegram->getCommandsList();
if (empty($text)) {
$commands = $this->telegram->getCommandsList(); $msg = 'GeoBot v. ' . $this->telegram->getVersion() . "\n\n";
if (empty($text)) { $msg.= 'Commands List:' . "\n";
$msg = 'GeoBot v. '.$this->telegram->getVersion()."\n\n"; foreach ($commands as $command) {
$msg .= 'Commands List:'."\n"; if (!$command->isEnabled()) {
foreach($commands as $command) { continue;
if (!$command->isEnabled()) { }
continue; $msg.= '/' . $command->getName() . ' - ' . $command->getDescription() . "\n";
} }
$msg .= '/'.$command->getName().' - '.$command->getDescription()."\n";
} $msg.= "\n" . 'For exact command help type: /help <command>';
} else {
$msg .= "\n".'For exact command help type: /help <command>'; $text = str_replace('/', '', $text);
} else { if (isset($commands[$text])) {
$text = str_replace('/', '', $text); $command = $commands[$text];
if (isset($commands[$text])) { $msg = 'Command: ' . $command->getName() . ' v' . $command->getVersion() . "\n";
$command = $commands[$text]; $msg.= 'Description: ' . $command->getDescription() . "\n";
$msg = 'Command: '.$command->getName().' v'.$command->getVersion()."\n"; $msg.= 'Usage: ' . $command->getUsage();
$msg .= 'Description: '.$command->getDescription()."\n"; } else {
$msg .= 'Usage: '.$command->getUsage(); $msg = 'Command ' . $text . ' not found';
} else { }
$msg = 'Command '.$text.' not found'; }
}
} $data = array();
$data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id;
$data = array(); $data['text'] = $msg;
$data['chat_id'] = $chat_id;
$data['reply_to_message_id'] = $message_id; $result = Request::sendMessage($data);
$data['text'] = $msg; return $result;
}
$result = Request::sendMessage($data);
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;
...@@ -15,114 +16,105 @@ use Longman\TelegramBot\Entities\Update; ...@@ -15,114 +16,105 @@ use Longman\TelegramBot\Entities\Update;
class WeatherCommand extends Command class WeatherCommand extends Command
{ {
protected $name = 'weather'; protected $name = 'weather';
protected $description = 'Show weather by location'; protected $description = 'Show weather by location';
protected $usage = '/weather <location>'; protected $usage = '/weather <location>';
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(CURLOPT_URL => $url,
$curlConfig = array(
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,
//CURLOPT_HEADER => true, //CURLOPT_HEADER => 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 \Exception('Error receiving data from url');
} }
curl_close($ch); curl_close($ch);
return $response;
}
return $response;
} private function getWeatherString($location)
{
if (empty($location)) {
private function getWeatherString($location) { return false;
if (empty($location)) { }
return false;
} // try/catch
$data = $this->getWeather($location);
// try/catch
$data = $this->getWeather($location); $decode = json_decode($data, true);
if (empty($decode) || $decode['cod'] != 200) {
$decode = json_decode($data, true); return false;
if (empty($decode) || $decode['cod'] != 200) { }
return false; $city = $decode['name'];
} $country = $decode['sys']['country'];
$city = $decode['name']; $temp = 'The temperature in ' . $city . ' (' . $country . ') is ' . $decode['main']['temp'] . '°C';
$country = $decode['sys']['country']; $conditions = 'Current conditions are: ' . $decode['weather'][0]['description'];
$temp = 'The temperature in '.$city .' (' .$country.') is '.$decode['main']['temp'].'°C';
$conditions = 'Current conditions are: '.$decode['weather'][0]['description']; switch (strtolower($decode['weather'][0]['main'])) {
case 'clear':
switch(strtolower($decode['weather'][0]['main'])) { $conditions.= ' ☀';
case 'clear': break;
$conditions .= ' ☀';
break; case 'clouds':
$conditions.= ' ☁☁';
case 'clouds': break;
$conditions .= ' ☁☁';
break; case 'rain':
$conditions.= ' ☔';
case 'rain': break;
$conditions .= ' ☔';
break; case 'thunderstorm':
$conditions.= ' ☔☔☔☔';
case 'thunderstorm': break;
$conditions .= ' ☔☔☔☔'; }
break;
} return $temp . "\n" . $conditions;
}
return $temp."\n".$conditions; public function execute()
} {
$update = $this->getUpdate();
public function execute() { $message = $this->getMessage();
$update = $this->getUpdate();
$message = $this->getMessage(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId();
$text = $message->getText(true);
$chat_id = $message->getChat()->getId(); if (empty($text)) {
$message_id = $message->getMessageId(); $text = 'You must specify location in format: /weather <city>';
$text = $message->getText(true); } else {
$weather = $this->getWeatherString($text);
if (empty($weather)) {
if (empty($text)) { $text = 'Can not find weather for location: ' . $text;
$text = 'You must specify location in format: /weather <city>'; } else {
} else { $text = $weather;
$weather = $this->getWeatherString($text); }
if (empty($weather)) { }
$text = 'Can not find weather for location: '.$text;
} else { $data = array();
$text = $weather; $data['chat_id'] = $chat_id;
} $data['reply_to_message_id'] = $message_id;
} $data['text'] = $text;
$result = Request::sendMessage($data);
$data = array(); return $result;
$data['chat_id'] = $chat_id; }
$data['reply_to_message_id'] = $message_id;
$data['text'] = $text;
$result = Request::sendMessage($data);
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;
...@@ -18,34 +19,30 @@ use Longman\TelegramBot\Entities\Update; ...@@ -18,34 +19,30 @@ use Longman\TelegramBot\Entities\Update;
class WhoamiCommand extends Command class WhoamiCommand extends Command
{ {
protected $name = 'whoami'; protected $name = 'whoami';
protected $description = 'Show your id, name and username'; protected $description = 'Show your id, name and username';
protected $usage = '/whoami'; protected $usage = '/whoami';
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(); {
$message = $this->getMessage(); $update = $this->getUpdate();
$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; $data['text'] = 'Your Id: ' . $message->getFrom()->getId();
$data['text'] = 'Your Id: ' . $message->getFrom()->getId(); $data['text'] .= "\n" . 'Name: ' . $message->getFrom()->getFirstName()
$data['text'] .= "\n".'Name: ' . $message->getFrom()->getFirstName().' '.$message->getFrom()->getLastName(); . ' ' . $message->getFrom()->getLastName();
$data['text'] .= "\n".'Username: ' . $message->getFrom()->getUsername(); $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;
...@@ -14,52 +15,53 @@ use Longman\TelegramBot\Exception\TelegramException; ...@@ -14,52 +15,53 @@ use Longman\TelegramBot\Exception\TelegramException;
class Chat extends Entity class Chat extends Entity
{ {
protected $id; protected $id;
protected $title; protected $title;
protected $first_name; protected $first_name;
protected $last_name; protected $last_name;
protected $username; protected $username;
public function __construct(array $data) {
$this->id = isset($data['id']) ? $data['id'] : null;
if (empty($this->id)) {
throw new TelegramException('id is empty!');
}
$this->title = isset($data['title']) ? $data['title'] : null;
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null;
}
public function getId() { public function __construct(array $data)
{
return $this->id; $this->id = isset($data['id']) ? $data['id'] : null;
} if (empty($this->id)) {
throw new TelegramException('id is empty!');
}
$this->title = isset($data['title']) ? $data['title'] : null;
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null;
}
public function getTitle() { public function getId()
{
return $this->title; return $this->id;
} }
public function getFirstName() { public function getTitle()
{
return $this->first_name; return $this->title;
} }
public function getLastName() { public function getFirstName()
{
return $this->last_name; return $this->first_name;
} }
public function getLastName()
{
public function getUsername() { return $this->last_name;
}
return $this->username; public function getUsername()
} {
return $this->username;
}
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,36 +7,28 @@ ...@@ -6,36 +7,28 @@
* *
* 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()
{
$reflection = new \ReflectionObject($this);
$properties = $reflection->getProperties();
public function toJSON() { $fields = array();
$reflection = new \ReflectionObject($this);
$properties = $reflection->getProperties();
$fields = array();
foreach($properties as $property) {
$name = $property->getName();
$property->setAccessible(true);
$value = $property->getValue($this);
$fields[$name] = $value;
}
$json = json_encode($fields);
return $json;
}
foreach ($properties as $property) {
$name = $property->getName();
$property->setAccessible(true);
$value = $property->getValue($this);
$fields[$name] = $value;
}
$json = json_encode($fields);
return $json;
}
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,190 +7,185 @@ ...@@ -6,190 +7,185 @@
* *
* 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;
protected $from;
protected $date;
protected $chat;
protected $forward_from;
protected $forward_date;
protected $reply_to_message;
protected $text;
protected $audio;
protected $document;
protected $photo;
protected $sticker;
protected $video;
protected $contact;
protected $location;
protected $new_chat_participant; protected $from;
protected $left_chat_participant; protected $date;
protected $new_chat_title; protected $chat;
protected $new_chat_photo; protected $forward_from;
protected $delete_chat_photo; protected $forward_date;
protected $group_chat_created; protected $reply_to_message;
protected $text;
protected $_command; protected $audio;
protected $bot_name; protected $document;
public function __construct(array $data, $bot_name) { protected $photo;
$this->bot_name = $bot_name; protected $sticker;
$this->message_id = isset($data['message_id']) ? $data['message_id'] : null; protected $video;
if (empty($this->message_id)) {
throw new TelegramException('message_id is empty!');
}
$this->from = isset($data['from']) ? $data['from'] : null; protected $contact;
if (empty($this->from)) {
throw new TelegramException('from is empty!');
}
$this->from = new User($this->from);
protected $location;
$this->date = isset($data['date']) ? $data['date'] : null; protected $new_chat_participant;
if (empty($this->date)) {
throw new TelegramException('date is empty!');
}
$this->chat = isset($data['chat']) ? $data['chat'] : null; protected $left_chat_participant;
if (empty($this->chat)) {
throw new TelegramException('chat is empty!');
}
$this->chat = new Chat($this->chat);
protected $new_chat_title;
$this->text = isset($data['text']) ? $data['text'] : null; protected $new_chat_photo;
protected $delete_chat_photo;
$this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null; protected $group_chat_created;
if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from);
}
$this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null; protected $command;
$this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null; protected $bot_name;
if (!empty($this->reply_to_message)) {
$this->reply_to_message = new Message($this->reply_to_message);
}
} public function __construct(array $data, $bot_name)
{
$this->bot_name = $bot_name;
//return the entire command like /echo or /echo@bot1 if specified $this->message_id = isset($data['message_id']) ? $data['message_id'] : null;
public function getFullCommand(){ if (empty($this->message_id)) {
throw new TelegramException('message_id is empty!');
}
return strtok($this->text, ' '); $this->from = isset($data['from']) ? $data['from'] : null;
} if (empty($this->from)) {
throw new TelegramException('from is empty!');
}
$this->from = new User($this->from);
$this->date = isset($data['date']) ? $data['date'] : null;
if (empty($this->date)) {
throw new TelegramException('date is empty!');
}
public function getCommand() { $this->chat = isset($data['chat']) ? $data['chat'] : null;
if (!empty($this->_command)) { if (empty($this->chat)) {
return $this->_command; throw new TelegramException('chat is empty!');
} }
$this->chat = new Chat($this->chat);
$cmd = $this->getFullCommand(); $this->text = isset($data['text']) ? $data['text'] : null;
if (substr($cmd, 0, 1) === '/') { $this->forward_from = isset($data['forward_from']) ? $data['forward_from'] : null;
$cmd = substr($cmd, 1); if (!empty($this->forward_from)) {
$this->forward_from = new User($this->forward_from);
}
//check if command is follow by botname $this->forward_date = isset($data['forward_date']) ? $data['forward_date'] : null;
$split_cmd = explode('@', $cmd);
if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me
if (strtolower($split_cmd[1]) == strtolower($this->bot_name)){
return $this->_command = $split_cmd[0];
}
} else {
//command is not followed by name
return $this->_command = $cmd;
}
}
return false; $this->reply_to_message = isset($data['reply_to_message']) ? $data['reply_to_message'] : null;
} if (!empty($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
public function getFullCommand()
{
return strtok($this->text, ' ');
}
public function getCommand()
{
if (!empty($this->command)) {
return $this->command;
}
public function getMessageId() { $cmd = $this->getFullCommand();
return $this->message_id; if (substr($cmd, 0, 1) === '/') {
} $cmd = substr($cmd, 1);
public function getDate() { //check if command is follow by botname
$split_cmd = explode('@', $cmd);
if (isset($split_cmd[1])) {
//command is followed by name check if is addressed to me
if (strtolower($split_cmd[1]) == strtolower($this->bot_name)) {
return $this->command = $split_cmd[0];
}
} else {
//command is not followed by name
return $this->command = $cmd;
}
}
return $this->date; return false;
} }
public function getFrom() { public function getMessageId()
{
return $this->from; return $this->message_id;
} }
public function getDate()
{
public function getChat() { return $this->date;
}
return $this->chat; public function getFrom()
} {
public function getForwardFrom() { return $this->from;
}
return $this->forward_from; public function getChat()
} {
public function getForwardDate() { return $this->chat;
}
return $this->forward_date; public function getForwardFrom()
} {
public function getReplyToMessage() { return $this->forward_from;
}
return $this->reply_to_message; public function getForwardDate()
} {
public function getText($without_cmd = false) { return $this->forward_date;
$text = $this->text; }
if ($without_cmd) {
$command = $this->getFullCommand();
$text = substr($text, strlen($command.' '), strlen($text));
}
return $text; public function getReplyToMessage()
} {
return $this->reply_to_message;
}
public function getText($without_cmd = false)
{
$text = $this->text;
if ($without_cmd) {
$command = $this->getFullCommand();
$text = substr($text, strlen($command . ' '), strlen($text));
}
return $text;
}
} }
<?php <?php
/* /*
* This file is part of the TelegramBot package. * This file is part of the TelegramBot package.
* *
...@@ -6,49 +7,43 @@ ...@@ -6,49 +7,43 @@
* *
* 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
{ {
protected $update_id; protected $update_id;
protected $message; protected $message;
protected $bot_name; protected $bot_name;
public function __construct(array $data, $bot_name) {
$update_id = isset($data['update_id']) ? $data['update_id'] : null;
$message = isset($data['message']) ? $data['message'] : null;
if (empty($update_id)) {
throw new TelegramException('update_id is empty!');
}
$this->bot_name = $bot_name;
$this->update_id = $update_id;
$this->message = new Message($message, $bot_name);
} public function __construct(array $data, $bot_name)
{
public function getUpdateId() { $update_id = isset($data['update_id']) ? $data['update_id'] : null;
return $this->update_id; $message = isset($data['message']) ? $data['message'] : null;
}
if (empty($update_id)) {
throw new TelegramException('update_id is empty!');
}
public function getMessage() { $this->bot_name = $bot_name;
$this->update_id = $update_id;
$this->message = new Message($message, $bot_name);
}
return $this->message; public function getUpdateId()
} {
return $this->update_id;
}
public function getMessage()
{
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;
...@@ -14,57 +15,49 @@ use Longman\TelegramBot\Exception\TelegramException; ...@@ -14,57 +15,49 @@ use Longman\TelegramBot\Exception\TelegramException;
class User extends Entity class User extends Entity
{ {
protected $id; protected $id;
protected $first_name; protected $first_name;
protected $last_name; protected $last_name;
protected $username; protected $username;
public function __construct(array $data) {
$this->id = isset($data['id']) ? $data['id'] : null;
if (empty($this->id)) {
throw new TelegramException('id is empty!');
}
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
if (empty($this->first_name)) {
throw new TelegramException('first_name is empty!');
}
$this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null;
}
public function getId() {
return $this->id;
}
public function getFirstName() {
return $this->first_name;
}
public function getLastName() { public function __construct(array $data)
{
return $this->last_name; $this->id = isset($data['id']) ? $data['id'] : null;
} if (empty($this->id)) {
throw new TelegramException('id is empty!');
}
$this->first_name = isset($data['first_name']) ? $data['first_name'] : null;
if (empty($this->first_name)) {
throw new TelegramException('first_name is empty!');
}
public function getUsername() { $this->last_name = isset($data['last_name']) ? $data['last_name'] : null;
$this->username = isset($data['username']) ? $data['username'] : null;
}
return $this->username; public function getId()
} {
return $this->id;
}
public function getFirstName()
{
return $this->first_name;
}
public function getLastName()
{
return $this->last_name;
}
public function getUsername()
{
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,113 +7,104 @@ ...@@ -6,113 +7,104 @@
* *
* 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;
class Request class Request
{ {
private static $telegram; private static $telegram;
private static $input; private static $input;
private static $methods = array( private static $methods = array(
'getMe', 'getMe',
'sendMessage', 'sendMessage',
'forwardMessage', 'forwardMessage',
'sendPhoto', 'sendPhoto',
'sendAudio', 'sendAudio',
'sendDocument', 'sendDocument',
'sendSticker', 'sendSticker',
'sendVideo', 'sendVideo',
'sendLocation', 'sendLocation',
'sendChatAction', 'sendChatAction',
'getUserProfilePhotos', 'getUserProfilePhotos',
'getUpdates', 'getUpdates',
'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 { self::$input = file_get_contents('php://input');
self::$input = file_get_contents('php://input'); }
} self::log();
self::log(); 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; }
} $path = self::$telegram->getLogPath();
$path = self::$telegram->getLogPath(); if (!$path) {
if (!$path) { 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)
{
if (defined('PHPUNIT_TESTSUITE')) {
public static function send($action, array $data = null) { return $data;
if (defined('PHPUNIT_TESTSUITE')) { }
return $data;
} $ch = curl_init();
$curlConfig = array(
$ch = curl_init(); CURLOPT_URL => 'https://api.telegram.org/bot' . self::$telegram->getApiKey() . '/' . $action,
$curlConfig = array( CURLOPT_POST => true,
CURLOPT_URL => 'https://api.telegram.org/bot'.self::$telegram->getApiKey().'/'.$action, CURLOPT_RETURNTRANSFER => true
CURLOPT_POST => true, );
CURLOPT_RETURNTRANSFER => true
); if (!empty($data)) {
if (!empty($data['text']) && substr($data['text'], 0, 1) === '@') {
if (!empty($data)) { $data['text'] = ' ' . $data['text'];
if (!empty($data['text']) && substr($data['text'], 0, 1) === '@') { }
$data['text'] = ' '.$data['text']; $curlConfig[CURLOPT_POSTFIELDS] = $data;
} }
$curlConfig[CURLOPT_POSTFIELDS] = $data;
} curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch);
curl_close($ch);
curl_setopt_array($ch, $curlConfig);
$result = curl_exec($ch); return !empty($result) ? json_decode($result, true) : false;
curl_close($ch); }
return !empty($result) ? json_decode($result, true) : false; public static function sendMessage(array $data)
} {
if (empty($data)) {
throw new TelegramException('Data is empty!');
public static function sendMessage(array $data) { }
if (empty($data)) { $result = self::send('sendMessage', $data);
throw new TelegramException('Data is empty!'); return $result;
} }
$result = self::send('sendMessage', $data); public static function setWebhook($url)
return $result; {
} $result = self::send('setWebhook', array('url' => $url));
return $result;
}
public static function setWebhook($url) {
$result = self::send('setWebhook', array('url'=>$url));
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