Skip to content

Commit

Permalink
Merge pull request #9 from westacks/develop
Browse files Browse the repository at this point in the history
1.7.1
  • Loading branch information
punyflash committed Feb 1, 2021
2 parents 144adde + 2b0d051 commit d0b7e05
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 17 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ All notable changes to `telebot` will be documented in this file

- `WeStacks\TeleBot\Laravel\TelegramMessage` renamed to `WeStacks\TeleBot\Laravel\TelegramNotification`.
- Telegram notification now can be sent only using `WeStacks\TeleBot\Laravel\TelegramNotification` object. Old array system is dropped
- When sending notification using `WeStacks\TeleBot\Laravel\TelegramNotification`, methods could be chained to send multiple messages in a row
- When sending notification using `WeStacks\TeleBot\Laravel\TelegramNotification`, methods could be chained to send multiple messages in a row
## 1.7.1 - 2021-02-01

- Added `getConfig()` method to the `WeStacks\TeleBot\TeleBot` instance. It will return the passsed to the constructor config. [#8](https://github.com/westacks/telebot/issues/8)
- Added ability to [change](https://westacks.github.io/telebot/#/configuration?id=standalone) `WeStacks\TeleBot\TeleBot`'s config parameters "on the go" using get/set syntax.
6 changes: 5 additions & 1 deletion docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ All notable changes to `telebot` will be documented here

- `WeStacks\TeleBot\Laravel\TelegramMessage` renamed to `WeStacks\TeleBot\Laravel\TelegramNotification`.
- Telegram notification now can be sent only using `WeStacks\TeleBot\Laravel\TelegramNotification` object. Old array system is dropped
- When sending notification using `WeStacks\TeleBot\Laravel\TelegramNotification`, methods could be chained to send multiple messages in a row
- When sending notification using `WeStacks\TeleBot\Laravel\TelegramNotification`, methods could be chained to send multiple messages in a row
## 1.7.1 - 2021-02-01

- Added `getConfig()` method to the `WeStacks\TeleBot\TeleBot` instance. It will return the passsed to the constructor config. [#8](https://github.com/westacks/telebot/issues/8)
- Added ability to [change](http://localhost:3000/#/configuration?id=standalone) `WeStacks\TeleBot\TeleBot`'s config parameters "on the go" using get/set syntax.
11 changes: 10 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,16 @@ $bot = new TeleBot([
'handlers' => []
]);

$bot->getMe();
/** @var User */
$user = $bot->getMe();

// You may change all config parameters "on the go" using get/set syntax
$bot->async = true; // Now bot uses A+ promises

$bot->getMe()->then(function (User $user) {
var_dump($user);
})->wait();

```

#### ** Advanced **
Expand Down
4 changes: 4 additions & 0 deletions docs/methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Additional library methods:
* Throw exceptions on next method, ignoring config parameter (bot method will throw `TeleBotRequestException` on request error)
* Returns: `self`

* **`getConfig()`**
* Get config that was used to create this bot instance
* Returns: `mixed`

* **`handleUpdate(Update $update = null)`**
* Handle Telegram [Update](https://core.telegram.org/bots/api#update) using registered update handlers. If given update is `null`, the library will try to create update from incoming `POST` request (in case you are using webhook). See more details in [Handling updates](updates.md) section
* Returns: `Update|False` - given update, or `false` in case `Update` was not valid.
Expand Down
31 changes: 31 additions & 0 deletions src/BotManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
* @method false|Update handleUpdate(Update $update = null) Handle given update
* @method BotCommand[] getLocalCommands() Get local bot instance commands registered by commands handlers
* @method void callHandler($handler, Update $update, bool $force = false) Run update handler.
*
*
* @method mixed getConfig() Get config that was used to create this bot instance
* @property string $token Your telegram bot token.
* @property string $api_url API URL which will be used by library's HTTP client.
* @property bool $exceptions By default, bot throws TeleBotRequestException on telegram request errors. You may set this parameter false. In this case bot methods will return false instead of throwing exception.
* @property bool $async If you set this parameter true, bot methods will return Guzzle's A+ `PromiseInterface` object, which you can handle mannualy.
*/
class BotManager
{
Expand Down Expand Up @@ -138,6 +145,30 @@ public function __call(string $name, array $arguments)
return $this->bot()->{$name}(...($arguments ?? []));
}

public function __get(string $name)
{
return $this->bot()->$name;
}

public function __set(string $name, $value)
{
if (!in_array($name, ['token', 'exceptions', 'async', 'api_url'])) {
throw TeleBotObjectException::inaccessibleVariable($name, self::class);
}

return $this->bot()->$name = $value;
}

public function __isset(string $key)
{
return isset($this->bot()->$key);
}

public function __unset(string $key)
{
throw TeleBotObjectException::inaccessibleUnsetVariable($key, self::class);
}

/**
* Get bot by name.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Laravel/TeleBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@
* @method static void callHandler($handler, Update $update, bool $force = false) Run update handler.
* @method static Update|False handleUpdate(Update $update = null) Handle given update
* @method static BotCommand[] getLocalCommands() Get local bot instance commands registered by commands handlers
*/
*
* @method static mixed getConfig() Get config that was used to create this bot instance
*/
class TeleBot extends Facade
{
/**
Expand Down
60 changes: 47 additions & 13 deletions src/TeleBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,49 +98,56 @@
* @method false|PromiseInterface|true logOut() Use this method to log out from the cloud Bot API server before launching the bot locally. You must log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates. After a successful call, you can immediately log in on a local server, but will not be able to log in back to the cloud Bot API server for 10 minutes. Returns True on success. Requires no parameters.
* @method false|PromiseInterface|MessageId copyMessage(array $parameters = []) Use this method to copy messages of any kind. The method is analogous to the method forwardMessages, but the copied message doesn't have a link to the original message. Returns the MessageId of the sent message on success.
* @method false|PromiseInterface|true unpinAllChatMessages(array $parameters = []) Use this method to clear the list of pinned messages in a chat. If the chat is not a private chat, the bot must be an administrator in the chat for this to work and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel. Returns True on success.
*
* @property string $token Your telegram bot token.
* @property string $api_url API URL which will be used by library's HTTP client.
* @property bool $exceptions By default, bot throws TeleBotRequestException on telegram request errors. You may set this parameter false. In this case bot methods will return false instead of throwing exception.
* @property bool $async If you set this parameter true, bot methods will return Guzzle's A+ `PromiseInterface` object, which you can handle mannualy.
*/
class TeleBot
{
use HandlesUpdates;
use HasTelegramMethods;

/**
* Bot config.
*
* Config that was used to create this bot instance
* @var mixed
*/
protected $input_config = [];

/**
* Actual bot config.
* @var array
*/
protected $config = [];

/**
* Guzzle HTTP client.
*
* @var Client
*/
protected $client;

/**
* Async trigger.
*
* @var bool
*/
protected $async;

/**
* Exception trigger.
*
* @var bool
*/
protected $exceptions;

/**
* Create new instance of Telegram bot.
*
* @param array|string $config Bot config. Path telegram bot API token as string, or array of parameters
*
* @throws TeleBotObjectException
*/
public function __construct($config)
{
$this->input_config = $config;

if (is_string($config)) {
$config = ['token' => $config];
}
Expand Down Expand Up @@ -175,31 +182,58 @@ public function __call(string $method, array $arguments)
return $method->execute($this->client, $exceptions, $async);
}

public function __get(string $name)
{
return $this->config[$name];
}

public function __set(string $name, $value)
{
if (!in_array($name, ['token', 'exceptions', 'async', 'api_url'])) {
throw TeleBotObjectException::inaccessibleVariable($name, self::class);
}

return $this->config[$name] = $value;
}

public function __isset(string $key)
{
return isset($this->config[$key]);
}

public function __unset(string $key)
{
throw TeleBotObjectException::inaccessibleUnsetVariable($key, self::class);
}

/**
* Get config that was used to create this bot instance
* @return mixed
*/
public function getConfig()
{
return $this->input_config;
}

/**
* Call next method asynchronously (bot method will return guzzle promise).
*
* @param bool $async
*
* @return self
*/
public function async(bool $async = true)
{
$this->async = $async;

return $this;
}

/**
* Throw exceptions on next method (bot method will throw `TeleBotRequestException` on request error).
*
* @param bool $exceptions
*
* @return self
*/
public function exceptions(bool $exceptions = true)
{
$this->exceptions = $exceptions;

return $this;
}
}
21 changes: 21 additions & 0 deletions tests/Feature/HandleUpdatesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use WeStacks\TeleBot\Exception\TeleBotMehtodException;
use WeStacks\TeleBot\Exception\TeleBotObjectException;
use WeStacks\TeleBot\Objects\BotCommand;
use WeStacks\TeleBot\Objects\Update;
use WeStacks\TeleBot\TeleBot;
Expand Down Expand Up @@ -102,4 +103,24 @@ public function testNoUpdates()
{
$this->assertFalse($this->bot->handleUpdate());
}

public function testGetConfig()
{
$this->assertEquals(getenv('TELEGRAM_BOT_TOKEN'), $this->bot->getConfig());
}

public function testUpdateConfigOnGo()
{
$this->assertTrue($this->bot->exceptions);

$this->bot->exceptions = false;
$this->assertFalse($this->bot->exceptions);

$this->bot->exceptions = true;
$this->assertTrue($this->bot->exceptions);

$this->assertTrue(isset($this->bot->exceptions));
$this->expectException(TeleBotObjectException::class);
unset($this->bot->exceptions);
}
}
19 changes: 19 additions & 0 deletions tests/Feature/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use WeStacks\TeleBot\Tests\Helpers\StartCommandHandler;
use WeStacks\TeleBot\Tests\Helpers\TelegramNotification;
use WeStacks\TeleBot\Tests\Helpers\TestNotifiable;
use WeStacks\TeleBot\BotManager;

class LaravelTest extends TestCase
{
Expand Down Expand Up @@ -57,6 +58,24 @@ public function testBotManagerBots()
TeleBot::bot('some_wrong_bot');
}

public function testBotManagerConfigs()
{
/** @var BotManager */
$manager = TeleBot::getFacadeRoot();
$this->assertTrue($manager->exceptions);

$manager->exceptions = false;
$this->assertFalse($manager->exceptions);

$manager->exceptions = true;
$this->assertTrue($manager->exceptions);

$this->assertTrue(isset($manager->exceptions));

$this->expectException(TeleBotObjectException::class);
unset($manager->exceptions);
}

public function testBotMenagerDefaultWrong()
{
$this->expectException(TeleBotObjectException::class);
Expand Down

0 comments on commit d0b7e05

Please sign in to comment.