Skip to content

Commit

Permalink
Additions and improvements
Browse files Browse the repository at this point in the history
Added MessageId and InaccessibleMessage classes.

Minor updates to README.md

get_full_name() custom (not standard) method of User class has been prefixed with an underline to distinguish it from the standard methods.
  • Loading branch information
amirrh6 committed Nov 17, 2024
1 parent 9f1d070 commit 62a1f39
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 68 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Getting updates:
- [X] [setWebhook](https://core.telegram.org/bots/api#setwebhook)
- [X] [deleteWebhook](https://core.telegram.org/bots/api#deletewebhook)
- [X] [getWebhookInfo](https://core.telegram.org/bots/api#getwebhookinfo)
- [X] [WebhookInfo](https://core.telegram.org/bots/api#webhookinfo)
- [ ] [WebhookInfo](https://core.telegram.org/bots/api#webhookinfo)

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

Expand All @@ -75,8 +75,8 @@ Available types:
- [X] [Chat](https://core.telegram.org/bots/api#chat)
- [X] [ChatFullInfo](https://core.telegram.org/bots/api#chatfullinfo)
- [X] [Message](https://core.telegram.org/bots/api#message)
- [ ] [MessageId](https://core.telegram.org/bots/api#messageid)
- [ ] [InaccessibleMessage](https://core.telegram.org/bots/api#inaccessiblemessage)
- [X] [MessageId](https://core.telegram.org/bots/api#messageid)
- [X] [InaccessibleMessage](https://core.telegram.org/bots/api#inaccessiblemessage)
- [MaybeInaccessibleMessage](https://core.telegram.org/bots/api#maybeinaccessiblemessage) : InaccessibleMessage | Message
- [ ] [MessageEntity](https://core.telegram.org/bots/api#messageentity)
- [ ] [TextQuote](https://core.telegram.org/bots/api#textquote)
Expand Down Expand Up @@ -230,13 +230,15 @@ Available methods:
- [X] [sendMessage](https://core.telegram.org/bots/api#sendmessage)
- [ ] [forwardMessage](https://core.telegram.org/bots/api#forwardmessage)
- [ ] [forwardMessages](https://core.telegram.org/bots/api#forwardmessages)
- [X] [copyMessage](https://core.telegram.org/bots/api#copymessage)
- [X] [copyMessage](https://core.telegram.org/bots/api#copymessage) *
- [ ] [copyMessages](https://core.telegram.org/bots/api#copymessages)
- [X] [sendPhoto](https://core.telegram.org/bots/api#sendphoto)
- [ ] ...
- [X] [answerCallbackQuery](https://core.telegram.org/bots/api#answercallbackquery)
- [ ] ...

\* Experimental bulk (concurrent) version is available. These methods are named like this: `copyMessage()` ---> `_bulkCopyMessage()`

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---

Updating messages:
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 25 additions & 17 deletions src/methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private static function jsonEncodeNonPrimaryFields(object $params)
return $result;
}

// -------------------------------------------------------------------

/**
* Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects.
* @return Update[]
Expand Down Expand Up @@ -161,6 +163,8 @@ static function getWebhookInfo(string $token, $options = []): WebhookInfo
return new WebhookInfo($body_decoded->result);
}

// -------------------------------------------------------------------

/**
* A simple method for testing your bot's authentication token. Requires no parameters. Returns basic information about the bot in form of a User object.
*/
Expand Down Expand Up @@ -346,18 +350,19 @@ static function sendPhoto(
}

/**
* Use this method to edit text and game messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent.
* Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
* Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via @BotFather and accept the terms. Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter.
* @throws ClientException
*/
static function editMessageText(
static function answerCallbackQuery(
string $token,
EditMessageTextParams $params,
AnswerCallbackQueryParams $params,
$options = [],
): Message|true {
): true {
$client = new Client(['base_uri' => '', ...$options]);

$response = $client->post(
static::$telegramApiUrl . $token . '/editMessageText',
static::$telegramApiUrl . $token . '/answerCallbackQuery',
[
'json' => $params,
],
Expand All @@ -370,27 +375,24 @@ static function editMessageText(
throw new Exception('Could not decode the response!');
}

if (is_bool($body_decoded->result)) {
return true;
} else {
return new Message($body_decoded->result);
}
return true;
}

// -------------------------------------------------------------------

/**
* Use this method to send answers to callback queries sent from inline keyboards. The answer will be displayed to the user as a notification at the top of the chat screen or as an alert. On success, True is returned.
* Alternatively, the user can be redirected to the specified Game URL. For this option to work, you must first create a game for your bot via @BotFather and accept the terms. Otherwise, you may use links like t.me/your_bot?start=XXXX that open your bot with a parameter.
* Use this method to edit text and game messages. On success, if the edited message is not an inline message, the edited Message is returned, otherwise True is returned. Note that business messages that were not sent by the bot and do not contain an inline keyboard can only be edited within 48 hours from the time they were sent.
* @throws ClientException
*/
static function answerCallbackQuery(
static function editMessageText(
string $token,
AnswerCallbackQueryParams $params,
EditMessageTextParams $params,
$options = [],
): true {
): Message|true {
$client = new Client(['base_uri' => '', ...$options]);

$response = $client->post(
static::$telegramApiUrl . $token . '/answerCallbackQuery',
static::$telegramApiUrl . $token . '/editMessageText',
[
'json' => $params,
],
Expand All @@ -403,6 +405,12 @@ static function answerCallbackQuery(
throw new Exception('Could not decode the response!');
}

return true;
if (is_bool($body_decoded->result)) {
return true;
} else {
return new Message($body_decoded->result);
}
}

// -------------------------------------------------------------------
}
35 changes: 0 additions & 35 deletions src/non_implemented_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,6 @@ public function __construct($init_data)
}
}

/**
* TODO:
* This object represents a unique message identifier.
*/
#[\AllowDynamicProperties]
class MessageId
{
public function __construct($init_data)
{
$arr = get_object_vars($init_data);
foreach ($arr as $key => $value) {
if (!($value instanceof \stdClass)) {
$this->$key = $init_data->$key;
}
}
}
}

/**
* TODO:
* This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.
Expand Down Expand Up @@ -488,23 +470,6 @@ public function __construct($init_data)
}
}

/**
* TODO:
*/
#[\AllowDynamicProperties]
class InaccessibleMessage
{
public function __construct($init_data)
{
$arr = get_object_vars($init_data);
foreach ($arr as $key => $value) {
if (!($value instanceof \stdClass)) {
$this->$key = $init_data->$key;
}
}
}
}

/**
* TODO:
*/
Expand Down
58 changes: 57 additions & 1 deletion src/primary_types.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public function __construct($init_data)
}
}

public function get_full_name(): string
public function _get_full_name(): string
{
$full_name = $this->first_name;
if ($this->last_name != null) {
Expand Down Expand Up @@ -1420,6 +1420,62 @@ public function __construct($init_data)
}
}

/**
* This object represents a unique message identifier.
*/
class MessageId extends CustomJsonSerialization
{
/**
* Unique message identifier
*/
public int $message_id;

public function __construct($init_data)
{
$arr = get_object_vars($init_data);
foreach ($arr as $key => $value) {
if (!($value instanceof \stdClass)) {
$this->$key = $init_data->$key;
}
}
}
}

/**
* This object describes a message that was deleted or is otherwise inaccessible to the bot.
*/
class InaccessibleMessage extends CustomJsonSerialization
{
/**
* Chat the message belonged to
*/
public Chat $chat;

/**
* Unique message identifier inside the chat
*/
public int $message_id;

/**
* Always 0. The field can be used to differentiate regular and inaccessible messages.
*/
public int $date;

public function __construct($init_data)
{
$arr = get_object_vars($init_data);
foreach ($arr as $key => $value) {
if (!($value instanceof \stdClass)) {
$this->$key = $init_data->$key;
}
}

if (property_exists($init_data, 'chat')) {
$this->chat = new Chat($init_data->chat);
}
}
}

/**
* Describes reply parameters for the message that is being sent.
*/
Expand Down

0 comments on commit 62a1f39

Please sign in to comment.