Sending Requests
The framework creates a 1:1 mapping of the methods Telegram provides that are directly accessible from the main instance. The only difference is in the parameters that telegram marks as Optional: these are specifiable via the last parameter (present in almost all methods) as an associative array.
For example:
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Types\Message\Message;
$bot = new Nutgram($_ENV['TOKEN']);
// Send a message to a specific user
/** @var Message $message */
$message = $bot->sendMessage('Hi!', ['chat_id' => 111222333]);
// Send a message to a channel
/** @var Message $message */
$message = $bot->sendMessage('Hi channel!', ['chat_id' => '@mychannel']);
Uploading media
For any method that require an InputFile
, you can pass a resource file
descriptor to the right method, and the framework will take care of how uploading it, like in the following example.
If you already have the Telegram file_id
, you can simply specify it.
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Types\Message\Message;
$bot = new Nutgram($_ENV['TOKEN']);
// Send a photo to a specific user
$photo = fopen('image.png', 'r+'); // open the file
/** @var Message $message */
$message = $bot->sendPhoto($photo, ['chat_id' => 111222333]); // pass the resource
fclose($photo); // close the file!
$video = fopen('funnyvideo.mp4', 'r+');
/** @var Message $message */
$message = $bot->sendPhoto($video, ['chat_id' => 111222333]);
fclose($video);
// send a sticker via file_id
$fileId = $bot->message()->sticker->file_id;
/** @var Message $message */
$message = $bot->sendSticker($fileId, ['chat_id' => 111222333]);
Downloading media
As opposed to uploading, there are some additional methods available that allow you to download files:
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Types\Media\File;
$bot = new Nutgram($_ENV['TOKEN']);
$fileId = $bot->message()->sticker->file_id;
// get the File object
/** @var File $message */
$file = $bot->getFile($fileId);
$bot->downloadFile($file, 'path/to/file');
// OR, via helper method
$bot->getFile($fileId)->save('file/or/directory');
Formatting options
The framework give you some helper constants to format your text messages:
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Attributes\ParseMode;
use SergiX44\Nutgram\Telegram\Types\Message\Message;
$bot = new Nutgram($_ENV['TOKEN']);
// Send a message formatting in markdown
/** @var Message $message */
$message = $bot->sendMessage('*Hi!*', [
'chat_id' => 111222333,
'parse_mode' => ParseMode::MARKDOWN,
]);
// Send a message formatting in html
/** @var Message $message */
$message = $bot->sendMessage('<i>Hi!</i>', [
'chat_id' => 111222333,
'parse_mode' => ParseMode::HTML,
]);
Available methods
Method | Return Type | Telegram Docs |
---|---|---|
getMe() | ?User | #getme |
logOut() | ?bool | #logout |
close() | ?bool | #close |
sendMessage(string $text, ?array $opt = []) | ?Message | #sendmessage |
forwardMessage(string|int $chat_id, string|int $from_chat_id, int $message_id, array $opt = []) | ?Message | #forwardmessage |
copyMessage(string|int $chat_id, string|int $from_chat_id, int $message_id, array $opt = []) | ?MessageId | #copymessage |
sendPhoto($photo, array $opt = []) | ?Message | #sendphoto |
sendAudio($audio, array $opt = []) | ?Message | #sendaudio |
sendDocument($document, array $opt = []) | ?Message | #senddocument |
sendVideo($video, array $opt = []) | ?Message | #sendvideo |
sendAnimation($video, array $opt = []) | ?Message | #sendanimation |
sendVoice($voice, array $opt = []) | ?Message | #sendvoice |
sendVideoNote($video_note, array $opt = []) | ?Message | #sendvideonote |
sendMediaGroup(array $media, array $opt = []) | ?array | #sendmediagroup |
sendLocation(float $latitude, float $longitude, ?array $opt = []) | ?Message | #sendlocation |
editMessageLiveLocation(float $latitude, float $longitude, ?array $opt = []) | Message|bool|null | #editmessagelivelocation |
stopMessageLiveLocation(?array $opt = []) | Message|bool|null | #stopmessagelivelocation |
sendVenue(float $latitude, float $longitude, string $title, string $address, ?array $opt = []) | ?Message | #sendvenue |
sendContact(string $first_name, string $phone_number, ?array $opt = []) | ?Message | #sendcontact |
sendPoll(string $question, array $options, ?array $opt = []) | ?Message | #sendpoll |
sendDice(?array $opt = []) | ?Message | #senddice |
sendChatAction(string $action, ?array $opt = []) | ?bool | #sendchataction |
getFile(string $file_id) | ?File | #getfile |
kickChatMember(string|int $chat_id, int $user_id, ?array $opt = []) | ?bool | #kickchatmember |
unbanChatMember(string|int $chat_id, int $user_id, ?array $opt = []) | ?bool | #unbanchatmember |
restrictChatMember(string|int $chat_id, int $user_id, ChatPermissions $permissions, ?array $opt = []) | ?bool | #restrictchatmember |
promoteChatMember(string|int $chat_id, int $user_id, ?array $opt = []) | ?bool | #promotechatmember |
setChatAdministratorCustomTitle(string|int $chat_id, int $user_id, string $custom_title, ?array $opt = []) | ?bool | #setchatadministratorcustomtitle |
setChatPermissions(string|int $chat_id, ChatPermissions $permissions, ?array $opt = []) | ?bool | #setchatpermissions |
exportChatInviteLink(string|int $chat_id) | ?string | #exportchatinvitelink |
createChatInviteLink(string|int $chat_id, ?array $opt = []) | ?ChatInviteLink | #createchatinvitelink |
editChatInviteLink(string|int $chat_id, string $invite_link, ?array $opt = []) | ?ChatInviteLink | #editchatinvitelink |
revokeChatInviteLink(string|int $chat_id, string $invite_link) | ?ChatInviteLink | #revokechatinvitelink |
setChatPhoto(string|int $chat_id, $photo) | ?bool | #setchatphoto |
deleteChatPhoto(string|int $chat_id) | ?bool | #deletechatphoto |
setChatTitle(string|int $chat_id, string $title) | ?bool | #setchattitle |
setChatDescription(string|int $chat_id, ?string $description = null) | ?bool | #setchatdescription |
pinChatMessage(string|int $chat_id, int $message_id, ?array $opt = []) | ?bool | #pinchatmessage |
unpinChatMessage(string|int $chat_id, int $message_id) | ?bool | #unpinchatmessage |
unpinAllChatMessages(string|int $chat_id) | ?bool | #unpinallchatmessages |
leaveChat(string|int $chat_id) | ?bool | #leavechat |
getChat(string|int $chat_id) | ?Chat | #getchat |
getChatAdministrators(string|int $chat_id) | ?array | #getchatadministrators |
getChatMembersCount(string|int $chat_id) | ?int | #getchatmemberscount |
getChatMember(string|int $chat_id, int $user_id) | ?ChatMember | #getchatmember |
setChatStickerSet(string|int $chat_id, string $sticker_set_name) | ?bool | #setchatstickerset |
deleteChatStickerSet(string|int $chat_id) | ?bool | #deletechatstickerset |
answerCallbackQuery(?array $opt = []) | ?bool | #answercallbackquery |
setMyCommands(array $commands = []) | ?bool | #setmycommands |
getMyCommands() | ?array | #getmycommands |
editMessageText(string $text, ?array $opt = []) | Message|bool|null | #editmessagetext |
editMessageCaption(?array $opt = []) | Message|bool|null | #editmessagecaption |
editMessageMedia(array $media, ?array $opt = []) | Message|bool|null | #editmessagemedia |
editMessageReplyMarkup(?array $opt = []) | Message|bool|null | #editmessagereplymarkup |
stopPoll(string|int $chat_id, int $message_id, ?array $opt = []) | ?Poll | #stoppoll |
deleteMessage(string|int $chat_id, int $message_id) | ?bool | #deletemessage |
sendSticker($sticker, array $opt = []) | ?Message | #sendsticker |
getStickerSet(string $name) | ?StickerSet | #getstickerset |
createNewStickerSet(string $name, string $title, ?array $opt = []) | ?bool | #createnewstickerset |
setStickerPositionInSet(string $sticker, int $position) | ?bool | #setstickerpositioninset |
deleteStickerFromSet(string $sticker) | ?bool | #deletestickerfromset |
setStickerSetThumb(string $name, ?array $opt = []) | ?bool | #setstickersetthumb |
answerInlineQuery(array $results, ?array $opt = []) | ?bool | #answerinlinequery |
sendInvoice(string $title, string $description, string $payload, string $provider_token, string $start_parameter, string $currency, array $prices, ?array $opt = []) | ?Message | #sendinvoice |
answerShippingQuery(bool $ok, ?array $opt = []) | ?bool | #answershippingquery |
answerPreCheckoutQuery(bool $ok, ?array $opt = []) | ?bool | #answerprecheckoutquery |
setPassportDataErrors(int $user_id, array $errors) | ?bool | #setpassportdataerrors |
sendGame(string $game_short_name, ?array $opt = []) | ?Message | #sendgame |
setGameScore(int $score, ?array $opt = []) | ?bool | #setgamescore |
getGameHighScores(?array $opt = []) | ?array | #getgamehighscores |