├── .gitignore ├── LICENCE.md ├── README.md ├── composer.json ├── composer.lock ├── core ├── Actions │ ├── CommandNotFoundAction.php │ ├── MeAction.php │ ├── SaveAction.php │ └── StartAction.php ├── Base │ └── Commander.php ├── Repository │ ├── FileStorageRepository.php │ └── RepositoryInterface.php └── config.php └── index.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | .DS_Store 3 | data.json -------------------------------------------------------------------------------- /LICENCE.md: -------------------------------------------------------------------------------- 1 | # The MIT License (MIT) 2 | 3 | Copyright (c) 2018 Taras Ziabukhin 4 | 5 | > Permission is hereby granted, free of charge, to any person obtaining a copy 6 | > of this software and associated documentation files (the "Software"), to deal 7 | > in the Software without restriction, including without limitation the rights 8 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | > copies of the Software, and to permit persons to whom the Software is 10 | > furnished to do so, subject to the following conditions: 11 | > 12 | > The above copyright notice and this permission notice shall be included in 13 | > all copies or substantial portions of the Software. 14 | > 15 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | > THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Really simple telegram bot on php 2 | This is example, how to build simple telegram bot on php, without additional SDK. 3 | 4 | ### Repository implements prepared for local storage as .json file 5 | 6 | In this realization used my library: [simple-telegram-bot](https://github.com/Tahrz/simple-telegram-bot/tree/master) 7 | 8 | > DEMO for article: [Easy to build telegram bot on PHP](https://medium.com/@taras.ziabukhin/really-simple-telegram-bot-php-c23097315912) 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "authors": [ 3 | { 4 | "name": "Taras Ziabukhin", 5 | "email": "taras.ziabukhin@gmail.com", 6 | "role": "Developer" 7 | } 8 | ], 9 | "require": { 10 | "php": "^8.0", 11 | "tahrz/simple-telegram-bot": "^2.0" 12 | }, 13 | "repositories": [ 14 | { 15 | "type": "git", 16 | "url": "https://github.com/tahrz/simple-telegram-bot" 17 | } 18 | ], 19 | "autoload": { 20 | "psr-4": { 21 | "Core\\": "core/" 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /composer.lock: -------------------------------------------------------------------------------- 1 | { 2 | "_readme": [ 3 | "This file locks the dependencies of your project to a known state", 4 | "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", 5 | "This file is @generated automatically" 6 | ], 7 | "content-hash": "6666d436cd203dafc6dfb06c2f35e9cb", 8 | "packages": [ 9 | { 10 | "name": "tahrz/simple-telegram-bot", 11 | "version": "2.0.0", 12 | "source": { 13 | "type": "git", 14 | "url": "https://github.com/tahrz/simple-telegram-bot", 15 | "reference": "ccaebb8fba45bc635f62d84558aef310ccfc8b34" 16 | }, 17 | "require": { 18 | "php": "^8.0" 19 | }, 20 | "require-dev": { 21 | "phpunit/phpunit": "^9" 22 | }, 23 | "type": "library", 24 | "autoload": { 25 | "psr-4": { 26 | "SimpleTelegramBot\\": "src/", 27 | "Tests\\": "tests/" 28 | } 29 | }, 30 | "license": [ 31 | "MIT" 32 | ], 33 | "authors": [ 34 | { 35 | "name": "Taras Ziabukhin", 36 | "email": "taras.ziabukhin@gmail.com", 37 | "role": "Developer" 38 | } 39 | ], 40 | "description": "Packadge with connection service and simple helpers", 41 | "keywords": [ 42 | "PHP", 43 | "PHP telegram bot", 44 | "simple telegram bot library", 45 | "telegram bot api", 46 | "webhook" 47 | ], 48 | "time": "2023-10-29T21:27:56+00:00" 49 | } 50 | ], 51 | "packages-dev": [], 52 | "aliases": [], 53 | "minimum-stability": "stable", 54 | "stability-flags": [], 55 | "prefer-stable": false, 56 | "prefer-lowest": false, 57 | "platform": { 58 | "php": "^8.0" 59 | }, 60 | "platform-dev": [], 61 | "plugin-api-version": "2.3.0" 62 | } 63 | -------------------------------------------------------------------------------- /core/Actions/CommandNotFoundAction.php: -------------------------------------------------------------------------------- 1 | sendWithoutResponse($update->message->chat->id, 'unsupported command!'); 23 | } 24 | } -------------------------------------------------------------------------------- /core/Actions/MeAction.php: -------------------------------------------------------------------------------- 1 | checkOnExistUserData($update)) { 26 | $user = $repo->getDataByUserName($update->message->chat->username); 27 | $messageHelper->sendWithoutResponse($update->message->chat->id, 'You are: ' . $user->firstName . ' ' . $user->lastName); 28 | } else { 29 | $messageHelper->sendWithoutResponse($update->message->chat->id, 'I can`t remember you'); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /core/Actions/SaveAction.php: -------------------------------------------------------------------------------- 1 | checkOnExistUserData($update)) { 26 | $messageHelper->sendWithoutResponse($update->message->chat->id, 'Done.'); 27 | $repo->saveDate($update); 28 | } else { 29 | $messageHelper->sendWithoutResponse($update->message->chat->id, 'Already saved.'); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /core/Actions/StartAction.php: -------------------------------------------------------------------------------- 1 | sendWithoutResponse($update->message->chat->id, 'Hi! For saving use /save, for getting data about you, use /me'); 23 | } 24 | } -------------------------------------------------------------------------------- /core/Base/Commander.php: -------------------------------------------------------------------------------- 1 | update = $update; 41 | $this->connectionService = $connectionService; 42 | } 43 | 44 | /** 45 | * @param string $command 46 | * @param string $action 47 | */ 48 | public function command(string $command, string $action): void 49 | { 50 | $this->commands[$command] = $action; 51 | } 52 | 53 | /** 54 | * @return object 55 | * @throws ReflectionException 56 | */ 57 | public function run(): void 58 | { 59 | if (isset($this->commands[$this->update->message->text])) { 60 | (new $this->commands[$this->update->message->text])( 61 | $this->update, 62 | $this->connectionService 63 | ); 64 | } 65 | 66 | (new CommandNotFoundAction())($this->update, $this->connectionService); 67 | } 68 | } -------------------------------------------------------------------------------- /core/Repository/FileStorageRepository.php: -------------------------------------------------------------------------------- 1 | message->chat->username] = [ 21 | 'firstName' => $update->message->chat->first_name, 22 | 'lastName' => $update->message->chat->last_name, 23 | ]; 24 | 25 | $data = fopen(DATA_FILE, 'w'); 26 | fwrite($data, json_encode(array_merge($oldData ?? [], $newData))); 27 | 28 | return fclose($data); 29 | } 30 | 31 | /** 32 | * @param object $update 33 | * @return bool 34 | */ 35 | public function checkOnExistUserData(object $update): bool 36 | { 37 | $data = json_decode(file_get_contents(DATA_FILE)); 38 | $userName = $update->message->chat->username; 39 | 40 | return array_key_exists($userName, (array)$data); 41 | } 42 | 43 | /** 44 | * @param string $userName 45 | * @return object 46 | */ 47 | public function getDataByUserName(string $userName): object 48 | { 49 | $data = json_decode(file_get_contents(DATA_FILE)); 50 | 51 | return $data->$userName; 52 | } 53 | } -------------------------------------------------------------------------------- /core/Repository/RepositoryInterface.php: -------------------------------------------------------------------------------- 1 | command('/start', \Core\Actions\StartAction::class); 23 | $commander->command('/save', \Core\Actions\SaveAction::class); 24 | $commander->command('/me', \Core\Actions\MeAction::class); 25 | 26 | /** 27 | * Run commander 28 | */ 29 | $commander->run(); --------------------------------------------------------------------------------