tgbotlib/src/TgBotLib/Bot.php

1076 lines
48 KiB
PHP
Raw Normal View History

2023-02-12 13:43:38 -05:00
<?php
2023-02-14 20:55:34 -05:00
/** @noinspection PhpMissingFieldTypeInspection */
2023-02-12 13:43:38 -05:00
namespace TgBotLib;
2023-02-16 16:27:30 -05:00
use CURLFile;
use CurlHandle;
2023-02-14 21:00:59 -05:00
use TgBotLib\Exceptions\TelegramException;
2023-02-16 16:27:30 -05:00
use TgBotLib\Interfaces\ObjectTypeInterface;
use TgBotLib\Objects\Telegram\ChatInviteLink;
use TgBotLib\Objects\Telegram\File;
use TgBotLib\Objects\Telegram\Message;
2023-02-14 20:55:34 -05:00
use TgBotLib\Objects\Telegram\Update;
2023-02-14 21:15:07 -05:00
use TgBotLib\Objects\Telegram\User;
2023-02-16 16:27:30 -05:00
use TgBotLib\Objects\Telegram\UserProfilePhotos;
use TgBotLib\Objects\Telegram\WebhookInfo;
2023-02-14 20:55:34 -05:00
2023-02-12 13:43:38 -05:00
class Bot
{
/**
* @var string
*/
private $token;
2023-02-14 20:55:34 -05:00
/**
* @var string
*/
private $host;
/**
* @var bool
*/
private $ssl;
/**
* @var int
*/
private $last_update_id;
2023-02-16 16:27:30 -05:00
/**
* @var CurlHandle|null
*/
private $curl_handle;
2023-02-12 13:43:38 -05:00
/**
* Public Constructor
*
* @param string $token
*/
public function __construct(string $token)
2023-02-12 13:43:38 -05:00
{
$this->token = $token;
2023-02-14 20:55:34 -05:00
$this->host = 'api.telegram.org';
$this->ssl = true;
$this->last_update_id = 0;
2023-02-12 13:43:38 -05:00
}
/**
2023-02-14 20:55:34 -05:00
* Returns the bot's token
*
2023-02-12 13:43:38 -05:00
* @return string
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-12 13:43:38 -05:00
*/
public function getToken(): string
{
return $this->token;
}
2023-02-14 20:55:34 -05:00
/**
* Returns the host the library is using to send requests to
*
* @return string
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 20:55:34 -05:00
*/
public function getHost(): string
{
return $this->host;
}
/**
* Sets the host the library will use to send requests to
*
* @param string $host
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 20:55:34 -05:00
*/
public function setHost(string $host): void
{
$this->host = $host;
}
/**
* Returns whether the library is using SSL to send requests
*
* @return bool
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 20:55:34 -05:00
*/
public function isSsl(): bool
{
return $this->ssl;
}
/**
* Sets whether the library will use SSL to send requests
*
* @param bool $ssl
*/
public function setSsl(bool $ssl): void
{
$this->ssl = $ssl;
}
/**
* Returns the URL for the specified method using the current host and SSL settings
*
* @param string $method
* @return string
*/
private function getMethodUrl(string $method): string
{
return ($this->ssl ? 'https://' : 'http://') . $this->host . '/bot' . $this->token . '/' . $method;
}
/**
* Sends a request to the Telegram API and returns the result as an array (unparsed)
*
* @param string $method
* @param array $params
* @return array
2023-02-14 21:00:59 -05:00
* @throws TelegramException
2023-02-16 16:27:30 -05:00
* @noinspection DuplicatedCode
2023-02-14 20:55:34 -05:00
*/
2023-02-16 16:27:30 -05:00
public function sendRequest(string $method, array $params=[]): mixed
2023-02-14 20:55:34 -05:00
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->getMethodUrl($method),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
2023-02-14 20:55:34 -05:00
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: multipart/form-data'
]
2023-02-14 20:55:34 -05:00
]);
2023-02-14 20:55:34 -05:00
$response = curl_exec($ch);
print_r($response);
2023-02-14 20:55:34 -05:00
if ($response === false)
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
2023-02-14 20:55:34 -05:00
curl_close($ch);
$parsed = json_decode($response, true);
if($parsed['ok'] === false)
2023-02-14 21:00:59 -05:00
throw new TelegramException($parsed['description'], $parsed['error_code']);
2023-02-14 20:55:34 -05:00
return $parsed['result'];
}
2023-02-16 16:27:30 -05:00
/**
* Sends a request to the Telegram API and returns the result as an array (unparsed)
*
* @param string $method
* @param string $file_param
* @param string $file_path
* @param array $params
* @return array
* @throws TelegramException
*/
public function sendFileUpload(string $method, string $file_param, string $file_path, array $params=[]): mixed
{
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $this->getMethodUrl($method) . '?' . http_build_query($params),
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
$file_param => new CURLFile($file_path)
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Content-Type: multipart/form-data'
]
]);
$response = curl_exec($ch);
print_r($response);
if ($response === false)
throw new TelegramException('curl error: ' . curl_error($ch), curl_errno($ch));
curl_close($ch);
$parsed = json_decode($response, true);
if($parsed['ok'] === false)
throw new TelegramException($parsed['description'], $parsed['error_code']);
return $parsed['result'];
}
2023-02-14 20:55:34 -05:00
/**
* Use this method to receive incoming updates using long polling (wiki). Returns an Array of Update objects.
*
* @param array $options Optional parameters
2023-02-14 20:55:34 -05:00
* @return Update[]
2023-02-14 21:00:59 -05:00
* @throws TelegramException
* @link https://core.telegram.org/bots/api#getupdates
* @see https://en.wikipedia.org/wiki/Push_technology#Long_polling
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 20:55:34 -05:00
*/
2023-02-14 21:02:37 -05:00
public function getUpdates(array $options=[]): array
2023-02-14 20:55:34 -05:00
{
2023-02-14 21:02:37 -05:00
if(!isset($options['offset']))
$options['offset'] = $this->last_update_id + 1;
2023-02-14 20:55:34 -05:00
$results = array_map(function ($update) {
return Update::fromArray($update);
2023-02-14 21:02:37 -05:00
}, $this->sendRequest('getUpdates', $options));
2023-02-14 20:55:34 -05:00
if(count($results) > 0)
$this->last_update_id = $results[count($results) - 1]->getUpdateId();
return $results;
}
2023-02-14 21:00:59 -05:00
/**
* Use this method to specify a URL and receive incoming updates via an outgoing webhook. Whenever there is an
* update for the bot, we will send an HTTPS POST request to the specified URL, containing a JSON-serialized
* Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts.
* Returns True on success.
*
* If you'd like to make sure that the webhook was set by you, you can specify secret data in the parameter
* secret_token. If specified, the request will contain a header “X-Telegram-Bot-Api-Secret-Token” with the
* secret token as content.
*
2023-02-14 21:02:37 -05:00
* @param string $url HTTPS URL to send updates to. Use an empty string to remove webhook integration
* @param array $options Optional parameters
2023-02-14 21:00:59 -05:00
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#setwebhook
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 21:00:59 -05:00
*/
2023-02-14 21:02:37 -05:00
public function setWebhook(string $url, array $options=[]): bool
2023-02-14 21:00:59 -05:00
{
2023-02-14 21:03:13 -05:00
$this->sendRequest('setWebhook', array_merge($options, [
'url' => $url
]));
2023-02-14 21:00:59 -05:00
return true;
}
/**
* Use this method to remove webhook integration if you decide to switch back to getUpdates.
* Returns True on success.
*
* @param bool $drop_pending_updates Pass True to drop all pending updates
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#deletewebhook
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
public function deleteWebhook(bool $drop_pending_updates=false): bool
{
$this->sendRequest('deleteWebhook', [
'drop_pending_updates' => $drop_pending_updates
]);
return true;
}
/**
* Use this method to get current webhook status. Requires no parameters. On success, returns a WebhookInfo
* object. If the bot is using getUpdates, will return an object with the url field empty.
*
* @return WebhookInfo
* @throws TelegramException
* @link https://core.telegram.org/bots/api#getwebhookinfo
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
public function getWebhookInfo(): WebHookInfo
{
return WebhookInfo::fromArray($this->sendRequest('getWebhookInfo'));
}
2023-02-14 21:15:07 -05:00
/**
* 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.
*
* @return User
* @throws TelegramException
* @link https://core.telegram.org/bots/api#getme
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
2023-02-14 21:15:07 -05:00
*/
public function getMe(): User
{
return User::fromArray($this->sendRequest('getMe'));
}
/**
* 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.
*
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#logout
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
public function logout(): bool
{
$this->sendRequest('logout');
return true;
}
/**
* Use this method to close the bot instance before moving it from one local server to another. You need to
* delete the webhook before calling this method to ensure that the bot isn't launched again after server
* restart. The method will return error 429 in the first 10 minutes after the bot is launched. Returns True on
* success. Requires no parameters.
*
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#close
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
public function close(): bool
{
$this->sendRequest('close');
return true;
}
/**
* Use this method to send text messages. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $text Text of the message to be sent, 1-4096 characters after entities parsing
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendmessage
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendMessage(string|int $chat_id, string $text, array ...$options): Message
{
return Message::fromArray($this->sendRequest('sendMessage', array_merge($options, [
'chat_id' => $chat_id,
'text' => $text
])));
}
/**
* Use this method to forward messages of any kind. Service messages can't be forwarded. On success, the sent
* Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
* @param int $message_id Message identifier in the chat specified in from_chat_id
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#forwardmessage
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function forwardMessage(string|int $chat_id, string $from_chat_id, int $message_id, array $options=[]): Message
{
return Message::fromArray($this->sendRequest('forwardMessage', array_merge($options, [
'chat_id' => $chat_id,
'from_chat_id' => $from_chat_id,
'message_id' => $message_id
])));
}
/**
* Use this method to copy messages of any kind. Service messages and invoice messages can't be copied. A quiz
* poll can be copied only if the value of the field correct_option_id is known to the bot. The method is
* analogous to the method forwardMessage, but the copied message doesn't have a link to the original message.
* Returns the MessageId of the sent message on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $from_chat_id Unique identifier for the chat where the original message was sent (or channel username in the format @channelusername)
* @param int $message_id Message identifier in the chat specified in from_chat_id
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#copymessage
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function copyMessage(string|int $chat_id, string $from_chat_id, int $message_id, array $options=[]): Message
{
return Message::fromArray($this->sendRequest('copyMessage', array_merge($options, [
'chat_id' => $chat_id,
'from_chat_id' => $from_chat_id,
'message_id' => $message_id
])));
}
/**
* Use this method to send photos. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $photo Photo to send. Pass a file_id as String to send a photo that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a photo from the Internet, or upload a new photo using multipart/form-data. The photo must be at most 10 MB in size. The photo's width and height must not exceed 10000 in total. Width and height ratio must be at most 20.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendphoto
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendPhoto(string|int $chat_id, string $photo, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($photo))
{
return Message::fromArray($this->sendFileUpload('sendPhoto', 'photo', $photo, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendPhoto', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'photo' => $photo
])));
}
/**
* Use this method to send audio files, if you want Telegram clients to display them in the music player. Your
* audio must be in the .MP3 or .M4A format. On success, the sent Message is returned. Bots can currently send
* audio files of up to 50 MB in size, this limit may be changed in the future.
*
* For sending voice messages, use the sendVoice method instead.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $audio Audio file to send. Pass a file_id as String to send an audio file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an audio file from the Internet, or upload a new one using multipart/form-data.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendaudio
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendAudio(string|int $chat_id, string $audio, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($audio))
{
return Message::fromArray($this->sendFileUpload('sendAudio', 'audio', $audio, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendAudio', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'audio' => $audio
])));
}
/**
* Use this method to send general files. On success, the sent Message is returned. Bots can currently send
* files of any type of up to 50 MB in size, this limit may be changed in the future.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $document File to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#senddocument
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendDocument(string|int $chat_id, string $document, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($document))
{
return Message::fromArray($this->sendFileUpload('sendDocument', 'document', $document, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendDocument', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'document' => $document
])));
}
/**
* Use this method to send video files, Telegram clients support MPEG4 videos (other formats may be sent as
* Document). On success, the sent Message is returned. Bots can currently send video files of up to 50 MB in
* size, this limit may be changed in the future.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $video Video to send. Pass a file_id as String to send a video that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a video from the Internet, or upload a new video using multipart/form-data.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendvideo
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendVideo(string|int $chat_id, string $video, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($video))
{
return Message::fromArray($this->sendFileUpload('sendVideo', 'video', $video, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendVideo', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'video' => $video
])));
}
/**
* Use this method to send animation files (GIF or H.264/MPEG-4 AVC video without sound). On success, the sent
* Message is returned. Bots can currently send animation files of up to 50 MB in size, this limit may be
* changed in the future.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $animation Animation to send. Pass a file_id as String to send an animation that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get an animation from the Internet, or upload a new animation using multipart/form-data.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendanimation
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendAnimation(string|int $chat_id, string $animation, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($animation))
{
return Message::fromArray($this->sendFileUpload('sendAnimation', 'animation', $animation, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendAnimation', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'animation' => $animation
])));
}
/**
* Use this method to send audio files, if you want Telegram clients to display the file as a playable voice
* message. For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent
* as Audio or Document). On success, the sent Message is returned. Bots can currently send voice messages of
* up to 50 MB in size, this limit may be changed in the future.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $voice Audio file to send. Pass a file_id as String to send a file that exists on the Telegram servers (recommended), pass an HTTP URL as a String for Telegram to get a file from the Internet, or upload a new one using multipart/form-data.
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendvoice
2023-02-16 16:27:30 -05:00
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendVoice(string|int $chat_id, string $voice, array $options=[]): Message
{
2023-02-16 16:27:30 -05:00
if(file_exists($voice))
{
return Message::fromArray($this->sendFileUpload('sendVoice', 'voice', $voice, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendVoice', array_merge($options, [
'chat_id' => $chat_id,
2023-02-16 16:27:30 -05:00
'voice' => $voice
])));
}
/**
* As of v.4.0, Telegram clients support rounded square MPEG4 videos of up to 1 minute long. Use this method to
* send video messages. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param string $video_note Video note to send. Pass a file_id as String to send a video note that exists on the Telegram servers (recommended) or upload a new video using multipart/form-data. (Sending video notes by a URL is currently unsupported)
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendVideoNote(string|int $chat_id, string $video_note, array $options=[]): Message
2023-02-16 16:27:30 -05:00
{
if(file_exists($video_note))
{
return Message::fromArray($this->sendFileUpload('sendVideoNote', 'video_note', $video_note, array_merge($options, [
'chat_id' => $chat_id
])));
}
return Message::fromArray($this->sendRequest('sendVideoNote', array_merge($options, [
'chat_id' => $chat_id,
'video_note' => $video_note
])));
}
/**
* Use this method to send a group of photos, videos, documents or audios as an album. Documents and audio files
* can be only grouped on an album with messages of the same type. On success, an array of Messages that were
* sent is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param array $media A JSON-serialized array describing messages to be sent, must include 2-10 items
* @param array $options Optional parameters
* @return array
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendmediagroup
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendMediaGroup(string|int $chat_id, array $media, array $options=[]): array
2023-02-16 16:27:30 -05:00
{
return array_map(function ($message)
{
return Message::fromArray($message);
}, $this->sendRequest('sendMediaGroup', array_merge($options, [
'chat_id' => $chat_id,
'media' => array_map(function ($item)
{
if($item instanceof ObjectTypeInterface)
return $item->toArray();
return $item;
}, $media)
])));
}
/**
* Use this method to send point on the map. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param float $latitude Latitude of the location
* @param float $longitude Longitude of the location
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendLocation(string|int $chat_id, float $latitude, float $longitude, array $options=[]): Message
2023-02-16 16:27:30 -05:00
{
return Message::fromArray($this->sendRequest('sendLocation', array_merge($options, [
'chat_id' => $chat_id,
'latitude' => $latitude,
'longitude' => $longitude
])));
}
/**
* Use this method to edit live location messages. A location can be edited until its live_period expires or
* editing is explicitly disabled by a call to stopMessageLiveLocation. On success, if the edited message is
* not an inline message, the edited Message is returned, otherwise True is returned.
*
* @param float $latitude Latitude of new location
* @param float $longitude Longitude of new location
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @see https://core.telegram.org/bots/api#stopmessagelivelocation
* @noinspection PhpUnused
*/
public function editMessageLiveLocation(float $latitude, float $longitude, array $options=[]): Message
{
return Message::fromArray($this->sendRequest('editMessageLiveLocation', array_merge($options, [
'latitude' => $latitude,
'longitude' => $longitude
])));
}
/**
* Use this method to stop updating a live location message before live_period expires. On success, if the
* message is not an inline message, the edited Message is returned, otherwise True is returned.
*
* @param array $options
* @return Message
* @throws TelegramException
* @see https://core.telegram.org/bots/api#stopmessagelivelocation
* @noinspection PhpUnused
*/
public function stopMessageLiveLocation(array $options=[]): Message
{
return Message::fromArray($this->sendRequest('stopMessageLiveLocation', $options));
}
/**
* Use this method to send information about a venue. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param float $latitude Latitude of the venue
* @param float $longitude Longitude of the venue
* @param string $title Name of the venue
* @param string $address Address of the venue
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @see https://core.telegram.org/bots/api#sendvenue
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendVenue(string|int $chat_id, float $latitude, float $longitude, string $title, string $address, array $options=[]): Message
2023-02-16 16:27:30 -05:00
{
return Message::fromArray($this->sendRequest('sendVenue', array_merge($options, [
'chat_id' => $chat_id,
'latitude' => $latitude,
'longitude' => $longitude,
'title' => $title,
'address' => $address
])));
}
/**
* Use this method to send phone contacts. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param string $phone_number Contact's phone number
* @param string $first_name Contact's first name
* @param array $options Optional parameters
* @return Message
* @throws TelegramException
* @see https://core.telegram.org/bots/api#sendcontact
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendContact(string|int $chat_id, string $phone_number, string $first_name, array $options=[]): Message
2023-02-16 16:27:30 -05:00
{
return Message::fromArray($this->sendRequest('sendContact', array_merge($options, [
'chat_id' => $chat_id,
'phone_number' => $phone_number,
'first_name' => $first_name
])));
}
/**
* Use this method to send a native poll. On success, the sent Message is returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param string $question Poll question, 1-300 characters
* @param array $options A JSON-serialized list of answer options, 2-10 strings 1-100 characters each
* @param array $params Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendpoll
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendPoll(string|int $chat_id, string $question, array $options, array $params=[]): Message
2023-02-16 16:27:30 -05:00
{
return Message::fromArray($this->sendRequest('sendPoll', array_merge($params, [
'chat_id' => $chat_id,
'question' => $question,
'options' => $options
])));
}
/**
* Use this method to send an animated emoji that will display a random value. On success, the sent Message is
* returned.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param array $params Optional parameters
* @return Message
* @throws TelegramException
* @link https://core.telegram.org/bots/api#senddice
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendDice(string|int $chat_id, array $params=[]): Message
2023-02-16 16:27:30 -05:00
{
return Message::fromArray($this->sendRequest('sendDice', array_merge($params, [
'chat_id' => $chat_id
])));
}
/**
* Use this method when you need to tell the user that something is happening on the bot's side. The status is
* set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
* Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param string $action Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_voice or upload_voice for voice notes, upload_document for general files, choose_sticker for stickers, find_location for location data, record_video_note or upload_video_note for video notes.
* @param array $options Optional parameters
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#sendchataction
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function sendChatAction(string|int $chat_id, string $action, array $options=[]): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('sendChatAction', array_merge($options, [
'chat_id' => $chat_id,
'action' => $action
]));
return true;
}
/**
* Use this method to get a list of profile pictures for a user. Returns a UserProfilePhotos object.
*
* @param int $user_id Unique identifier of the target user
* @param array $options Optional parameters
* @return UserProfilePhotos
* @throws TelegramException
* @link https://core.telegram.org/bots/api#getuserprofilephotos
* @noinspection PhpUnused
*/
public function getUserProfilePhotos(int $user_id, array $options=[]): UserProfilePhotos
{
return UserProfilePhotos::fromArray($this->sendRequest('getUserProfilePhotos', array_merge($options, [
'user_id' => $user_id
])));
}
2023-02-16 16:27:30 -05:00
/**
* Use this method to get basic information about a file and prepare it for downloading. For the moment, bots
* can download files of up to 20MB in size. On success, a File object is returned. The file can then be
* downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken
* from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires,
* a new one can be requested by calling getFile again.
*
* @param string $file_id File identifier to get information about
* @return File
* @throws TelegramException
* @link https://core.telegram.org/bots/api#getfile
* @warning This function may not preserve the original file name and MIME type. You should save the file's MIME type and name (if available) when the File object is received.
* @noinspection PhpUnused
*/
public function getFile(string $file_id): File
{
return File::fromArray($this->sendRequest('getFile', [
'file_id' => $file_id
]));
}
/**
* Use this method to ban a user in a group, a supergroup or a channel. In the case of supergroups and channels,
* the user will not be able to return to the chat on their own using invite links, etc., unless unbanned first.
* The bot must be an administrator in the chat for this to work and must have the appropriate administrator
* rights. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target user
* @param array $options Optional parameters
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#banchatmember
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function banChatMember(string|int $chat_id, int $user_id, array $options=[]): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('banChatMember', array_merge($options, [
'chat_id' => $chat_id,
'user_id' => $user_id
]));
return true;
}
/**
* Use this method to unban a previously banned user in a supergroup or channel. The user will not return to the
* group or channel automatically, but will be able to join via link, etc. The bot must be an administrator for
* this to work. By default, this method guarantees that after the call the user is not a member of the chat,
* but will be able to join it. So if the user is a member of the chat they will also be removed from the chat.
* If you don't want this, use the parameter only_if_banned. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target group or username of the target supergroup or channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target user
* @param array $options Optional parameters
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#unbanchatmember
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function unbanChatMember(string|int $chat_id, int $user_id, array $options=[]): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('unbanChatMember', array_merge($options, [
'chat_id' => $chat_id,
'user_id' => $user_id
]));
return true;
}
/**
* Use this method to restrict a user in a supergroup. The bot must be an administrator in the supergroup for
* this to work and must have the appropriate administrator rights. Pass True for all permissions to lift
* restrictions from a user. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target user
* @param array $permissions A JSON-serialized object for new user permissions (https://core.telegram.org/bots/api#chatpermissions)
* @param array $options Optional parameters
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#restrictchatmember
* @see https://core.telegram.org/bots/api#chatpermissions
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function restrictChatMember(string|int $chat_id, int $user_id, array $permissions, array $options=[]): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('restrictChatMember', array_merge($options, [
'chat_id' => $chat_id,
'user_id' => $user_id,
'permissions' => $permissions
]));
return true;
}
/**
* Use this method to promote or demote a user in a supergroup or a channel. The bot must be an administrator in
* the chat for this to work and must have the appropriate administrator rights. Pass False for all boolean
* parameters to demote a user. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target user
* @param array $options Optional parameters
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#promotechatmember
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function promoteChatMember(string|int $chat_id, int $user_id, array $options=[]): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('promoteChatMember', array_merge($options, [
'chat_id' => $chat_id,
'user_id' => $user_id
]));
return true;
}
/**
* Use this method to set a custom title for an administrator in a supergroup promoted by the bot. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target user
* @param string $custom_title New custom title for the administrator; 0-16 characters, emoji are not allowed
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#setchatadministratorcustomtitle
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function setChatAdministratorCustomTitle(string|int $chat_id, int $user_id, string $custom_title): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('setChatAdministratorCustomTitle', [
'chat_id' => $chat_id,
'user_id' => $user_id,
'custom_title' => $custom_title
]);
return true;
}
/**
* Use this method to ban a channel chat in a supergroup or a channel. Until the chat is unbanned, the owner of
* the banned chat won't be able to send messages on behalf of any of their channels. The bot must be an
* administrator in the supergroup or channel for this to work and must have the appropriate administrator
* rights. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target sender chat
* @return bool
* @throws TelegramException
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function banChatSenderChat(string|int $chat_id, int $user_id): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('banChatSenderChat', [
'chat_id' => $chat_id,
'user_id' => $user_id
]);
return true;
}
/**
* Use this method to unban a previously banned channel chat in a supergroup or channel. The bot must be an
* administrator for this to work and must have the appropriate administrator rights. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param int $user_id Unique identifier of the target sender chat
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#unbanchatsenderchat
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function unbanChatSenderChat(string|int $chat_id, int $user_id): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('unbanChatSenderChat', [
'chat_id' => $chat_id,
'user_id' => $user_id
]);
return true;
}
/**
* Use this method to set default chat permissions for all members. The bot must be an administrator in the group or a supergroup for this to work and must have the can_restrict_members administrator rights. Returns True on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target supergroup (in the format @supergroupusername)
2023-02-16 16:27:30 -05:00
* @param array $permissions A JSON-serialized object for new default chat permissions (https://core.telegram.org/bots/api#chatpermissions)
* @param bool $use_independent_chat_permissions Pass True if chat permissions are set independently. Otherwise, the can_send_other_messages and can_add_web_page_previews permissions will imply the can_send_messages, can_send_audios, can_send_documents, can_send_photos, can_send_videos, can_send_video_notes, and can_send_voice_notes permissions; the can_send_polls permission will imply the can_send_messages permission.
* @return bool
* @throws TelegramException
* @link https://core.telegram.org/bots/api#setchatpermissions
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function setChatPermissions(string|int $chat_id, array $permissions, bool $use_independent_chat_permissions=false): bool
2023-02-16 16:27:30 -05:00
{
$this->sendRequest('setChatPermissions', [
'chat_id' => $chat_id,
'permissions' => $permissions,
'use_independent_chat_permissions' => $use_independent_chat_permissions
]);
return true;
}
/**
* Use this method to generate a new primary invite link for a chat; any previously generated primary link is
* revoked. The bot must be an administrator in the chat for this to work and must have the appropriate
* administrator rights. Returns the new invite link as String on success.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @return string
* @throws TelegramException
* @link https://core.telegram.org/bots/api#exportchatinvitelink
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function exportChatInviteLink(string|int $chat_id): string
2023-02-16 16:27:30 -05:00
{
return $this->sendRequest('exportChatInviteLink', [
'chat_id' => $chat_id
]);
}
/**
* Use this method to create an additional invite link for a chat. The bot must be an administrator in the chat
* for this to work and must have the appropriate administrator rights. The link can be revoked using the method
* revokeChatInviteLink. Returns the new invite link as ChatInviteLink object.
*
2023-02-16 21:55:04 -05:00
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
2023-02-16 16:27:30 -05:00
* @param array $options Optional parameters
* @return ChatInviteLink
* @link https://core.telegram.org/bots/api#createchatinvitelink
* @noinspection PhpUnused
*/
2023-02-16 21:55:04 -05:00
public function createChatInviteLink(string|int $chat_id, array $options=[]): ChatInviteLink
2023-02-16 16:27:30 -05:00
{
return ChatInviteLink::fromArray($this->sendRequest('createChatInviteLink', array_merge([
'chat_id' => $chat_id
], $options)));
}
2023-02-16 21:55:04 -05:00
/**
* Use this method to edit a non-primary invite link created by the bot. The bot must be an administrator in the
* chat for this to work and must have the appropriate administrator rights. Returns the edited invite link
* as a ChatInviteLink object.
*
* @param string|int $chat_id Unique identifier for the target chat or username of the target channel (in the format @channelusername)
* @param string $invite_link The invite link to edit
* @param array $options
* @return ChatInviteLink
* @throws TelegramException
*/
public function editChatInviteLink(string|int $chat_id, string $invite_link, array $options=[]): ChatInviteLink
{
return ChatInviteLink::fromArray($this->sendRequest('editChatInviteLink', array_merge([
'chat_id' => $chat_id,
'invite_link' => $invite_link
], $options)));
}
/**
* @param string|int $chat_id
* @param string $invite_link
* @return ChatInviteLink
* @throws TelegramException
*/
public function revokeChatInviteLink(string|int $chat_id, string $invite_link): ChatInviteLink
{
return ChatInviteLink::fromArray($this->sendRequest('revokeChatInviteLink', [
'chat_id' => $chat_id,
'invite_link' => $invite_link
]));
}
2023-02-16 16:27:30 -05:00
2023-02-12 13:43:38 -05:00
}