├── LICENSE ├── README.md ├── composer.json └── src ├── AnswerCallbackQuery.php ├── AnswerPreCheckoutQuery.php ├── Bot.php ├── CallbackQuery.php ├── Chat.php ├── ChatPhoto.php ├── Contact.php ├── DeleteMessage.php ├── Document.php ├── EditMessageText.php ├── File.php ├── FileId.php ├── FileUrl.php ├── GetFile.php ├── InlineKeyboardButton.php ├── InlineKeyboardMarkup.php ├── InputFile.php ├── KeyboardButton.php ├── LabeledPrice.php ├── Message.php ├── OrderInfo.php ├── PreCheckoutQuery.php ├── ReplyKeyboardMarkup.php ├── ReplyKeyboardRemove.php ├── ReplyMarkup.php ├── SendDocument.php ├── SendInvoice.php ├── SendMessage.php ├── SendPhoto.php ├── SetWebhook.php ├── ShippingAddress.php ├── SuccessfulPayment.php ├── Update.php └── User.php /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 FormaPro 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP Telegram Bot 2 | 3 | Telegram bot as it should be. 4 | 5 | ## Examples 6 | 7 | ### SetWebhook 8 | 9 | ```php 10 | setCertificate(file_get_contents('/path/to/self-signed-certifcate.pem')); 21 | 22 | $response = $bot->setWebhook($setWebhook); 23 | 24 | echo str($response); 25 | ``` 26 | 27 | ### GetWebhookInfo 28 | 29 | ```php 30 | getWebhookInfo(); 37 | 38 | echo str($response); 39 | ``` 40 | 41 | ### SendMessage 42 | 43 | ```php 44 | sendMessage(new SendMessage( 56 | $update->getMessage()->getChat()->getId(), 57 | 'Hi there! What can I do?' 58 | )); 59 | ``` 60 | 61 | ### SendPhoto 62 | 63 | **Note:** if you want to send gif with many pictures inside - 64 | use `sendDocument` 65 | 66 | ```php 67 | getMessage()->getChat()->getId(), 82 | file_get_contents($picture) // or just $picture if it's url 83 | ); 84 | 85 | // also you can set `caption` to image 86 | $sendPhoto->setCaption('Some caption under the picture'); 87 | 88 | $bot = new Bot('telegramToken'); 89 | $bot->sendPhoto($sendPhoto); 90 | ``` 91 | 92 | ### SendDocument 93 | 94 | **Note:** _sending by URL will currently only work for 95 | gif, pdf and zip files._ (from documentation) 96 | 97 | ```php 98 | getMessage()->getChat()->getId(), 122 | $document 123 | ); 124 | 125 | // also you can set `caption` to image 126 | $sendDocument->setCaption('Some caption under the document'); 127 | 128 | $bot = new Bot('telegramToken'); 129 | $bot->sendDocument($sendDocument); 130 | ``` 131 | 132 | ### SendInvoice 133 | 134 | ```php 135 | getMessage()->getChat()->getId(), 149 | 'Your title', 150 | 'Your description of invoice', 151 | json_encode($payload), 152 | $providerToken, 153 | '12314czasdq', // unique id 154 | 'UAH', 155 | [new LabeledPrice('PriceLabel_1', 3001)] // amount; here 30.01 UAH 156 | ); 157 | 158 | $bot = new Bot('telegramToken'); 159 | $bot->sendInvoice($sendInvoice); 160 | ``` 161 | 162 | ### ReplyKeyboardMarkup 163 | 164 | ```php 165 | getMessage()->getChat()->getId(), 'Choose an option.'); 183 | $sendMessage->setReplyMarkup($keyboard); 184 | 185 | $bot = new Bot('telegramToken'); 186 | $bot->sendMessage($sendMessage); 187 | ``` 188 | 189 | Request Contacts: 190 | 191 | ```php 192 | setRequestContact(true); 206 | $keyboard = new ReplyKeyboardMarkup([[$button]]); 207 | $keyboard->setOneTimeKeyboard(true); 208 | 209 | $sendMessage = new SendMessage($update->getMessage()->getChat()->getId(), 'Please, share your contact info with us.'); 210 | $sendMessage->setReplyMarkup($keyboard); 211 | 212 | $bot = new Bot('telegramToken'); 213 | $bot->sendMessage($sendMessage); 214 | ``` 215 | 216 | ### InlineKeyboardButton 217 | 218 | Url: 219 | 220 | ```php 221 | getMessage()->getChat()->getId(), 'Click on inline button.'); 237 | $sendMessage->setReplyMarkup($keyboard); 238 | 239 | $bot = new Bot('telegramToken'); 240 | $bot->sendMessage($sendMessage); 241 | ``` 242 | 243 | CallbackQuery: 244 | 245 | ```php 246 | getMessage()->getChat()->getId(), 'Click on inline button.'); 262 | $sendMessage->setReplyMarkup($keyboard); 263 | 264 | $bot = new Bot('telegramToken'); 265 | $bot->sendMessage($sendMessage); 266 | ``` 267 | 268 | ## AnswerCallbackQuery 269 | 270 | ```php 271 | getCallbackQuery()) { 282 | $bot = new Bot('telegramToken'); 283 | $bot->answerCallbackQuery(new AnswerCallbackQuery($callbackQuery->getId())); 284 | } 285 | ``` 286 | 287 | ## Developed by Forma-Pro 288 | 289 | Forma-Pro is a full stack development company which interests also spread to open source development. 290 | Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience. 291 | Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability. 292 | 293 | If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com 294 | 295 | ## License 296 | 297 | It is released under the [MIT License](LICENSE). 298 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "formapro/telegram-bot", 3 | "type": "library", 4 | "description": "PHP Telegram Bot", 5 | "keywords": ["telegram", "bot", "client"], 6 | "license": "MIT", 7 | "require": { 8 | "php": "^7.1.3", 9 | "formapro/values": "^0.5.1", 10 | "guzzlehttp/guzzle": "^6.3" 11 | }, 12 | "autoload": { 13 | "psr-4": { "Formapro\\TelegramBot\\": "src/" } 14 | }, 15 | "extra": { 16 | "branch-alias": { 17 | "dev-master": "1.x-dev" 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/AnswerCallbackQuery.php: -------------------------------------------------------------------------------- 1 | setCallbackQueryId($callbackQueryId); 14 | } 15 | 16 | public function setCallbackQueryId(string $callbackQueryId): void 17 | { 18 | set_value($this, 'callback_query_id', $callbackQueryId); 19 | } 20 | 21 | public function getCallbackQueryId(): string 22 | { 23 | return get_value($this, 'callback_query_id'); 24 | } 25 | 26 | public function setText(?string $text): void 27 | { 28 | set_value($this, 'text', $text); 29 | } 30 | 31 | public function getText(): ?string 32 | { 33 | return get_value($this, 'text'); 34 | } 35 | 36 | public function setUrl(?string $url): void 37 | { 38 | set_value($this, 'url', $url); 39 | } 40 | 41 | public function getUrl(): ?string 42 | { 43 | return get_value($this, 'url'); 44 | } 45 | 46 | public function setCacheTime(?int $cacheTime): void 47 | { 48 | set_value($this, 'cache_time', $cacheTime); 49 | } 50 | 51 | public function getCacheTime(): int 52 | { 53 | return get_value($this, 'cache_time', 0); 54 | } 55 | 56 | public function setShowAlert(?bool $showAlert): void 57 | { 58 | set_value($this, 'show_alert', $showAlert); 59 | } 60 | 61 | public function getShowAlert(): bool 62 | { 63 | return get_value($this, 'show_alert', false); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/AnswerPreCheckoutQuery.php: -------------------------------------------------------------------------------- 1 | token = $token; 20 | $this->httpClient = $httpClient ?? new \GuzzleHttp\Client(); 21 | } 22 | 23 | public function setWebhook(SetWebhook $setWebhook): ResponseInterface 24 | { 25 | $parts = [ 26 | [ 27 | 'name' => 'url', 28 | 'contents' => $setWebhook->getUrl(), 29 | ], 30 | ]; 31 | 32 | if ($setWebhook->getCertificate()) { 33 | $parts[] = [ 34 | 'name' => 'certificate', 35 | 'contents' => $setWebhook->getCertificate(), 36 | 'filename' => 'self-signed-cert.pem', 37 | ]; 38 | } 39 | 40 | return $this->httpClient->post($this->getMethodUrl('setWebhook'), [ 41 | 'multipart' => $parts, 42 | ]); 43 | } 44 | 45 | public function getWebhookInfo(): ResponseInterface 46 | { 47 | return $this->httpClient->post($this->getMethodUrl('getWebhookInfo')); 48 | } 49 | 50 | public function sendMessage(SendMessage $sendMessage): Message 51 | { 52 | $response = $this->httpClient->post($this->getMethodUrl('sendMessage'), [ 53 | 'json' => get_values($sendMessage), 54 | ]); 55 | 56 | $json = json_decode((string) $response->getBody(), true); 57 | 58 | if (isset($json['ok']) && $json['ok']) { 59 | $message = new Message(); 60 | set_values($message, $json['result']); 61 | 62 | return $message; 63 | } 64 | 65 | throw new \LogicException('Unexpected response: ' . (string)$response->getBody()); 66 | } 67 | 68 | public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface 69 | { 70 | if (strpos($sendPhoto->getPhoto(), 'http') === 0) { 71 | return $this->httpClient->post($this->getMethodUrl('sendPhoto'), [ 72 | 'json' => get_values($sendPhoto), 73 | ]); 74 | } 75 | $values = get_values($sendPhoto); 76 | 77 | $data[] = [ 78 | 'name' => 'photo', 79 | 'contents' => $values['photo'], 80 | 'filename' => 'picture.jpg', 81 | ]; 82 | unset($values['photo']); 83 | 84 | foreach ($values as $name => $value) { 85 | $data[] = [ 86 | 'name' => $name, 87 | 'contents' => $value, 88 | ]; 89 | } 90 | 91 | return $this->httpClient->post($this->getMethodUrl('sendPhoto'), [ 92 | 'multipart' => $data, 93 | ]); 94 | } 95 | 96 | public function sendDocument(SendDocument $sendDocument): Message 97 | { 98 | $doc = $sendDocument->getDocument(); 99 | 100 | if ($doc instanceof FileId || $doc instanceof FileUrl) { 101 | $data = get_values($sendDocument); 102 | $data['document'] = (string)$doc; 103 | 104 | $response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [ 105 | 'json' => $data, 106 | ]); 107 | 108 | $json = json_decode((string)$response->getBody(), true); 109 | if (isset($json['ok']) && $json['ok']) { 110 | $message = new Message(); 111 | set_values($message, $json['result']); 112 | 113 | return $message; 114 | } 115 | 116 | throw new \LogicException('Unexpected response: ' . (string)$response->getBody()); 117 | } 118 | 119 | if ($doc instanceof InputFile) { 120 | $data[] = [ 121 | 'name' => 'document', 122 | 'contents' => $doc->getContent(), 123 | 'filename' => $doc->getFileName(), 124 | ]; 125 | 126 | $values = get_values($sendDocument); 127 | foreach ($values as $name => $value) { 128 | $data[] = [ 129 | 'name' => $name, 130 | 'contents' => $value, 131 | ]; 132 | } 133 | 134 | $response = $this->httpClient->post($this->getMethodUrl('sendDocument'), [ 135 | 'multipart' => $data, 136 | ]); 137 | 138 | $json = json_decode((string)$response->getBody(), true); 139 | if (isset($json['ok']) && $json['ok']) { 140 | $message = new Message(); 141 | set_values($message, $json['result']); 142 | 143 | return $message; 144 | } 145 | 146 | throw new \LogicException('Unexpected response: ' . (string) $response->getBody()); 147 | } 148 | 149 | throw new \LogicException(sprintf('Unexpected document: %s' . get_class($doc))); 150 | } 151 | 152 | public function sendInvoice(SendInvoice $sendInvoice) 153 | { 154 | return $this->httpClient->post($this->getMethodUrl('sendInvoice'), [ 155 | 'json' => get_values($sendInvoice), 156 | ]); 157 | } 158 | 159 | public function answerCallbackQuery(AnswerCallbackQuery $answerCallbackQuery): ResponseInterface 160 | { 161 | return $this->httpClient->post($this->getMethodUrl('answerCallbackQuery'), [ 162 | 'json' => get_values($answerCallbackQuery), 163 | ]); 164 | } 165 | 166 | public function answerPreCheckoutQuery(AnswerPreCheckoutQuery $answerPreCheckoutQuery): ResponseInterface 167 | { 168 | return $this->httpClient->post($this->getMethodUrl('answerPreCheckoutQuery'), [ 169 | 'json' => get_values($answerPreCheckoutQuery), 170 | ]); 171 | } 172 | 173 | public function editMessageText(EditMessageText $editMessageText): ?Message 174 | { 175 | $response = $this->httpClient->post($this->getMethodUrl('editMessageText'), [ 176 | 'json' => get_values($editMessageText), 177 | ]); 178 | 179 | $json = json_decode((string)$response->getBody(), true); 180 | 181 | if (isset($json['ok']) && $json['ok']) { 182 | $message = new Message(); 183 | set_values($message, $json['result']); 184 | 185 | return $message; 186 | } 187 | 188 | throw new \LogicException('Unexpected response: ' . (string) $response->getBody()); 189 | } 190 | 191 | public function deleteMessage(DeleteMessage $deleteMessage): bool 192 | { 193 | $response = $this->httpClient->post($this->getMethodUrl('deleteMessage'), [ 194 | 'json' => get_values($deleteMessage), 195 | ]); 196 | 197 | $response = json_decode((string)$response->getBody(), true); 198 | 199 | if (isset($response['ok']) && $response['ok']) { 200 | return true; 201 | } 202 | 203 | return false; 204 | } 205 | 206 | /** 207 | * @see https://core.telegram.org/bots/api#getfile 208 | */ 209 | public function getFile(GetFile $getFile): File 210 | { 211 | $response = $this->httpClient->post($this->getMethodUrl('getFile'), [ 212 | 'json' => get_values($getFile), 213 | ]); 214 | 215 | $json = json_decode((string) $response->getBody(), true); 216 | 217 | if (isset($json['ok']) && $json['ok']) { 218 | $file = new File(); 219 | set_values($file, $json['result']); 220 | 221 | if ($path = $file->getFilePath()) { 222 | $file->setFileUrl(sprintf('https://api.telegram.org/file/bot%s/%s', $this->token, $path)); 223 | } 224 | 225 | return $file; 226 | } 227 | 228 | throw new \LogicException('Unexpected response: ' . (string) $response->getBody()); 229 | } 230 | 231 | private function getMethodUrl(string $method): string 232 | { 233 | return sprintf('https://api.telegram.org/bot%s/%s', $this->token, $method); 234 | } 235 | 236 | public function getToken(): string 237 | { 238 | return $this->token; 239 | } 240 | } 241 | -------------------------------------------------------------------------------- /src/CallbackQuery.php: -------------------------------------------------------------------------------- 1 | fileUrl = $fileUrl; 33 | } 34 | 35 | public function getFileUrl(): ?string 36 | { 37 | return $this->fileUrl; 38 | } 39 | } -------------------------------------------------------------------------------- /src/FileId.php: -------------------------------------------------------------------------------- 1 | fileId = $fileId; 14 | } 15 | 16 | public function getFileId(): string 17 | { 18 | return $this->fileId; 19 | } 20 | 21 | public function __toString() 22 | { 23 | return $this->fileId; 24 | } 25 | } -------------------------------------------------------------------------------- /src/FileUrl.php: -------------------------------------------------------------------------------- 1 | url = $url; 14 | } 15 | 16 | public function getUrl(): string 17 | { 18 | return $this->url; 19 | } 20 | 21 | public function __toString() 22 | { 23 | return $this->url; 24 | } 25 | } -------------------------------------------------------------------------------- /src/GetFile.php: -------------------------------------------------------------------------------- 1 | 64) { 32 | throw new \Exception('Too long callback data, maximum 64 bytes allowed.'); 33 | } 34 | 35 | set_value($this, 'callback_data', $callbackData); 36 | } 37 | 38 | public function getCallbackData(): ?string 39 | { 40 | return get_value($this, 'callback_data'); 41 | } 42 | 43 | public function setPay(bool $pay): void 44 | { 45 | set_value($this, 'pay', $pay); 46 | } 47 | 48 | public function isPay(): bool 49 | { 50 | return get_value($this, 'pay', false); 51 | } 52 | 53 | public static function withUrl(string $text, string $url): self 54 | { 55 | $button = new self($text); 56 | $button->setUrl($url); 57 | 58 | return $button; 59 | } 60 | 61 | public static function withTextAsCallbackData(string $text): self 62 | { 63 | $button = new self($text); 64 | $button->setCallbackData($text); 65 | 66 | return $button; 67 | } 68 | 69 | public static function withCallbackData(string $text, string $callbackData): self 70 | { 71 | $button = new self($text); 72 | $button->setCallbackData($callbackData); 73 | 74 | return $button; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/InlineKeyboardMarkup.php: -------------------------------------------------------------------------------- 1 | $row) { 23 | foreach ($row as $button) { 24 | $this->addButton($rowNumber, $button); 25 | } 26 | } 27 | } 28 | 29 | public function addButton(int $row, InlineKeyboardButton $button): void 30 | { 31 | add_object($this, sprintf('inline_keyboard.%d', $row), $button); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/InputFile.php: -------------------------------------------------------------------------------- 1 | fileName = $fileName; 16 | $this->content = $content; 17 | } 18 | 19 | public function getFileName(): string 20 | { 21 | return $this->fileName; 22 | } 23 | 24 | public function getContent(): string 25 | { 26 | return $this->content; 27 | } 28 | 29 | public function __toString() 30 | { 31 | return $this->content; 32 | } 33 | } -------------------------------------------------------------------------------- /src/KeyboardButton.php: -------------------------------------------------------------------------------- 1 | $row) { 28 | foreach ($row as $button) { 29 | $this->addButton($rowNumber, $button); 30 | } 31 | } 32 | 33 | set_value($this, 'one_time_keyboard', $oneTimeKeyboard); 34 | set_value($this, 'resize_keyboard', $resizeKeyboard); 35 | set_value($this, 'selective', $selective); 36 | } 37 | 38 | public function addButton(int $row, KeyboardButton $button): void 39 | { 40 | add_object($this, sprintf('keyboard.%d', $row), $button); 41 | } 42 | 43 | public function setOneTimeKeyboard(bool $bool): void 44 | { 45 | set_value($this, 'one_time_keyboard', $bool); 46 | } 47 | 48 | public function isOneTimeKeyboard(): bool 49 | { 50 | return get_value($this, 'one_time_keyboard', false); 51 | } 52 | 53 | public function isResizeKeyboard(): bool 54 | { 55 | return get_value($this, 'resize_keyboard', false); 56 | } 57 | 58 | public function isSelective(): bool 59 | { 60 | return get_value($this, 'selective', false); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/ReplyKeyboardRemove.php: -------------------------------------------------------------------------------- 1 | document = $document; 28 | } 29 | 30 | public function getChatId(): int 31 | { 32 | return get_value($this, 'chat_id'); 33 | } 34 | 35 | /** 36 | * @return FileId|FileUrl|InputFile 37 | */ 38 | public function getDocument() 39 | { 40 | return $this->document; 41 | } 42 | 43 | public function getCaption(): string 44 | { 45 | return get_value($this, 'caption'); 46 | } 47 | 48 | public function setCaption(string $caption): void 49 | { 50 | set_value($this, 'caption', $caption); 51 | } 52 | 53 | public function setParseMode(string $parseMode): void 54 | { 55 | set_value($this, 'parse_mode', $parseMode); 56 | } 57 | 58 | public function getParseMode(): ?string 59 | { 60 | return get_value($this, 'parse_mode'); 61 | } 62 | 63 | public function setReplyMarkup(ReplyMarkup $replyMarkup): void 64 | { 65 | set_value($this, 'reply_markup', json_encode(get_values($replyMarkup))); 66 | } 67 | 68 | public static function withInputFile(int $chatId, InputFile $file): self 69 | { 70 | return new static($chatId, $file); 71 | } 72 | 73 | public static function withFileUrl(int $chatId, FileUrl $file): self 74 | { 75 | return new static($chatId, $file); 76 | } 77 | 78 | public static function withFileId(int $chatId, FileId $file): self 79 | { 80 | return new static($chatId, $file); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/SendInvoice.php: -------------------------------------------------------------------------------- 1 | addPrice($price); 42 | } 43 | } 44 | 45 | public function getChatId(): int 46 | { 47 | return get_value($this, 'chat_id'); 48 | } 49 | 50 | public function getTitle(): string 51 | { 52 | return get_value($this, 'title'); 53 | } 54 | 55 | public function getDescription(): string 56 | { 57 | return get_value($this, 'description'); 58 | } 59 | 60 | public function getPayload(): string 61 | { 62 | return get_value($this, 'payload'); 63 | } 64 | 65 | public function getProviderToken(): string 66 | { 67 | return get_value($this, 'provider_token'); 68 | } 69 | 70 | public function getStartParameter(): string 71 | { 72 | return get_value($this, 'start_parameter'); 73 | } 74 | 75 | public function getCurrency(): string 76 | { 77 | return get_value($this, 'currency'); 78 | } 79 | 80 | /** 81 | * @return LabeledPrice[] 82 | */ 83 | public function getPrices(): array 84 | { 85 | return get_value($this, 'prices'); 86 | } 87 | 88 | private function addPrice(LabeledPrice $price): void 89 | { 90 | add_object($this, 'prices', $price); 91 | } 92 | 93 | public function setParseMode(string $parseMode): void 94 | { 95 | set_value($this, 'parse_mode', $parseMode); 96 | } 97 | 98 | public function getParseMode(): ?string 99 | { 100 | return get_value($this, 'parse_mode'); 101 | } 102 | 103 | public function setReplyMarkup(ReplyMarkup $replyMarkup): void 104 | { 105 | set_value($this, 'reply_markup', json_encode(get_values($replyMarkup))); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/SendMessage.php: -------------------------------------------------------------------------------- 1 |