Skip to content

Commit

Permalink
Implements new Bot API v3.1 changes. Close #38
Browse files Browse the repository at this point in the history
  • Loading branch information
Camilo Sperberg committed Jun 30, 2017
1 parent b03e4fe commit bb3ad1f
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/InternalFunctionality/TelegramRawData.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function getResult(): array
}

/**
* Hack: for some requests Telegram sends back an array, integer or a boolean value, convert it to boolean here
* Hack: for some requests Telegram sends back an array, integer, string or a boolean value, convert it to boolean here
* @return bool
*/
public function getResultBoolean(): bool
Expand All @@ -68,11 +68,20 @@ public function getResultBoolean(): bool
}

/**
* Hack: for some requests Telegram send back an array, integer or a boolean value, convert it to int here
* Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to int here
* @return int
*/
public function getResultInt(): int
{
return (int)$this->decodedData['result'];
}

/**
* Hack: for some requests Telegram send back an array, integer, string or a boolean value, convert it to string here
* @return string
*/
public function getResultString(): string
{
return (string)$this->decodedData['result'];
}
}
44 changes: 44 additions & 0 deletions src/Telegram/Methods/DeleteChatPhoto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to delete a chat photo. Photos can't be changed for private chats. The bot must be an administrator
* in the chat for this to work and must have the appropriate admin rights. Returns True on success
*
* Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off
* in the target group
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#deletechatphoto
*/
class DeleteChatPhoto extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @channelusername)
* @var string
*/
public $chat_id = '';

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
];
}
}
41 changes: 41 additions & 0 deletions src/Telegram/Methods/ExportChatInviteLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultString;

/**
* Use this method to export an invite link to a supergroup or a channel. The bot must be an administrator in the chat
* for this to work and must have the appropriate admin rights. Returns exported invite link as String on success
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#exportchatinvitelink
*/
class ExportChatInviteLink extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @channelusername)
* @var string
*/
public $chat_id = '';

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultString($data->getResultString(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
];
}
}
54 changes: 54 additions & 0 deletions src/Telegram/Methods/PinChatMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to pin a message in a supergroup. The bot must be an administrator in the chat for this to work and
* must have the appropriate admin rights. Returns True on success
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#pinchatmessage
*/
class PinChatMessage extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @var string
*/
public $chat_id = '';

/**
* Identifier of a message to pin
* @var int
*/
public $message_id = 0;

/**
* Optional. Pass True, if it is not necessary to send a notification to all group members about the new pinned
* message
* @var bool
*/
public $disable_notification = false;

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
'message_id',
];
}
}
46 changes: 46 additions & 0 deletions src/Telegram/Methods/SetChatDescription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to change the description of a supergroup or a channel. The bot must be an administrator in the chat
* for this to work and must have the appropriate admin rights. Returns True on success
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#setchatdescription
*/
class SetChatDescription extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @var string
*/
public $chat_id = '';

/**
* New chat description, 1-255 characters
* @var string
*/
public $description;

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
];
}
}
52 changes: 52 additions & 0 deletions src/Telegram/Methods/SetChatPhoto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to set a new profile photo for the chat. Photos can't be changed for private chats. The bot must be
* an administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success
*
* Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off
* in the target group
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#setchatphoto
*/
class SetChatPhoto extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @channelusername)
* @var string
*/
public $chat_id = '';

/**
* New chat photo, uploaded using multipart/form-data
* @var InputFile
*/
public $photo;

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
'photo',
];
}
}
50 changes: 50 additions & 0 deletions src/Telegram/Methods/SetChatTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to change the title of a chat. Titles can't be changed for private chats. The bot must be an
* administrator in the chat for this to work and must have the appropriate admin rights. Returns True on success
*
* Note: In regular groups (non-supergroups), this method will only work if the ‘All Members Are Admins’ setting is off
* in the target group
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#setchattitle
*/
class SetChatTitle extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @var string
*/
public $chat_id = '';

/**
* New chat title, 1-255 characters
* @var string
*/
public $title = '';

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
'title',
];
}
}
40 changes: 40 additions & 0 deletions src/Telegram/Methods/UnpinChatMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Methods;

use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TelegramMethods;
use unreal4u\TelegramAPI\Abstracts\TelegramTypes;
use unreal4u\TelegramAPI\InternalFunctionality\TelegramRawData;
use unreal4u\TelegramAPI\Telegram\Types\Custom\ResultBoolean;

/**
* Use this method to unpin a message in a supergroup chat. The bot must be an administrator in the chat for this to
* work and must have the appropriate admin rights. Returns True on success
*
* Objects defined as-is july 2017
*
* @see https://core.telegram.org/bots/api#unpinchatmessage
*/
class UnpinChatMessage extends TelegramMethods
{
/**
* Unique identifier for the target chat or username of the target supergroup or channel (in the format
* @var string
*/
public $chat_id = '';

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new ResultBoolean($data->getResultBoolean(), $logger);
}

public function getMandatoryFields(): array
{
return [
'chat_id',
];
}
}
26 changes: 26 additions & 0 deletions src/Telegram/Types/Custom/ResultString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);

namespace unreal4u\TelegramAPI\Telegram\Types\Custom;

use unreal4u\TelegramAPI\Abstracts\CustomType;
use Psr\Log\LoggerInterface;

/**
* Some API calls respond with int types
*/
class ResultString extends CustomType
{
public $data = '';

public function __construct(string $result, LoggerInterface $logger = null)
{
$this->logger = $logger;
$this->data = $result;
}

public function __toString()
{
return (string)$this->data;
}
}

0 comments on commit bb3ad1f

Please sign in to comment.