{"id":21565864,"url":"https://github.com/prevailexcel/telbot","last_synced_at":"2025-03-18T05:19:12.895Z","repository":{"id":203360234,"uuid":"708949857","full_name":"PrevailExcel/telbot","owner":"PrevailExcel","description":"For a quick script","archived":false,"fork":false,"pushed_at":"2023-10-24T17:15:55.000Z","size":10851,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T12:11:19.539Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PrevailExcel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-23T17:56:26.000Z","updated_at":"2023-10-23T17:58:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"0994ad29-bff2-4666-b3a7-e820cf1ba28c","html_url":"https://github.com/PrevailExcel/telbot","commit_stats":null,"previous_names":["prevailexcel/telbot"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrevailExcel%2Ftelbot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrevailExcel%2Ftelbot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrevailExcel%2Ftelbot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrevailExcel%2Ftelbot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PrevailExcel","download_url":"https://codeload.github.com/PrevailExcel/telbot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244160064,"owners_count":20408022,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T10:22:11.432Z","updated_at":"2025-03-18T05:19:12.867Z","avatar_url":"https://github.com/PrevailExcel.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Telegram Based Notification System\n\u003e System Agnostic Scheduled Notification System powered by a Telegram Bot Service\n\n## How it works\n- When command is ran, it retrives data from the API\n- It checks if there's an existing `totalUsers` saved, if there's none, it saves it.\n- It checks if the returned (current) `totalUsers` is higher than the saved one.\n- If it is differennt, it get's the difference and then gets from the response the latest number of users according to the difference of the `totalUsers` from `latestRegistered`. \n- If `old_total` is *140011* and `current_total` is *140016*, the differene is *5*; therefore it gets the first 5 user objects from `latestRegistered` array.\n- It then parse this information to the BotService that pushes it to the telegram channel specified.\n- If there is no difference, it means there is no new registration yet, so the script is ended.\n\n## Code Structure\nThis system is built with **Laravel Framework (8.0)** and **Botman Framework (2.0)**.\nThe core files are presented below\n#### Services `/app/Services/`\n- NotificationService\n- TelegramService\n#### Commands `/app/Console/Commands/`\n- NotificationCommand\n#### Kernel `/app/Console/`\n- Kernel\n\n##### NotificationService:\nThis class takes care of calling the api endpoint to get current data and then processes it and passes it to the telegram service.\n```php\nclass NotificationService\n{\n    /**\n     * This class takes care of calling the api endpoint to get current data \n     * and then processes it and passes it to the telegram service. \n    */\n    public function run()\n    {\n        $url = env('API_URL', 'https://api-mystland.up.railway.app/api/getUsers');\n        $response = Http::get($url);    \n        $old_total = Cache::get('old_total');\n    \n        if ($response-\u003eok()) {\n            $current_total = $response-\u003ejson('totalUsers');\n\n            // If old_total is not available, cache it forever.\n            if (!$old_total)\n                $old_total = Cache::forever('old_total', $current_total);\n    \n            //Compare the old total and new returned total\n            if ($current_total \u003e $old_total) {\n    \n                //if it's greater, return the difference and call the telegram service\n                $difference = $current_total - $old_total;\n    \n                //process data for parsing\n                $users = [];\n                $users['list'] = array_slice($response-\u003ejson('lastRegistered'), 0, $difference);\n                $users['active'] = $response-\u003ejson('activeUsers');\n                $users['total'] = $response-\u003ejson('totalUsers');\n\n                //Send code to telegram service\n                $telegramService = new TelegramService();\n                return $telegramService-\u003ehandle(collect($users));\n            }\n        }\n        return true;\n    }\n}\n```\n\n##### TelegramService:\nThis class takes initiates the bot and sends out the notifications.\n```php\n\n//Title message: Showing number of new users, active users and total users\n$message = OutgoingMessage::create('You have \u003cb\u003e' . count($users['list']) . \"\u003c/b\u003e New Users \\n \\nActive Users: \u003cb\u003e\" . $users['active'] . \"\u003c/b\u003e \\nTotal Users: \u003cb\u003e\" . $users['total'] . \"\u003c/b\u003e \\n\");\n$botman-\u003esay($message, $channels, \\BotMan\\Drivers\\Telegram\\TelegramDriver::class, [\"parse_mode\" =\u003e \"HTML\"]);\n\n// Create message for each user and send to channel\nforeach ($users['list'] as $user) {\n\n    // Build message object\n    $confirmed = $user['emailConfirmed'] ? 'Yes' : 'No';\n    $referal = $user['isReferral'] ? 'Yes' : 'No';\n    $date = Carbon::parse($user['registrationTimestamp'])-\u003eformat('d M Y');\n\n    $messageString = \"NEW USER \\n \\nUsername: \u003cb\u003e\" . $user['login'] . \"\u003c/b\u003e \\nWallet: \u003cb\u003e\" . $user['wallet'] . \"\u003c/b\u003e \\nReferral Code: \u003cb\u003e\" . $user['referralCode'] . \"\u003c/b\u003e \\nDate: \u003cb\u003e\" . $date . \"\u003c/b\u003e \\nConfirmed Email: \u003cb\u003e\" . $confirmed . \"\u003c/b\u003e \\nUser was referred: \u003cb\u003e\" . $referal . \"\u003c/b\u003e\";\n\n    $message = OutgoingMessage::create($messageString);\n    $botman-\u003esay($message, $channels, \\BotMan\\Drivers\\Telegram\\TelegramDriver::class, [\"parse_mode\" =\u003e \"HTML\"]);\n}\n```\n\n##### NotificationCommand:\nThis is the command that will be called to run script. Command is registered, decribed and handled here.\n```php\n\nprotected $signature = 'notice:board';\n    \n    ***\n    \npublic function handle()\n{\n    $this-\u003einfo('Starting script.');\n    $notify = new NotificationService();\n    $this-\u003einfo('Script initiated.');\n    $notify-\u003erun();\n\n    $this-\u003einfo('Notification script ran successfully.');\n}\n```\n\n##### Kernel:\nThis is the command is scheduled.\n```php\nprotected function schedule(Schedule $schedule)\n{        \n    //Run this command every minute\n    $schedule-\u003ecommand('notice:board')-\u003eeveryMinute();\n    //-\u003eeveryFiveMinutes();\n    //-\u003eeveryThirtyMinutes();\n    // and more\n}\n```\n\n## How to Install and Run Script\nPHP 8.1 or above must be installed on the system. \n#### Prerequisites to installing system\n- -OS - Ubuntu or any Linux distro\n- PHP - 8.1 above\n- Web Server - Apache2 (prefered) or Nginx\n- Composer - PHP Dependency manager\n- Git (Optional) - depends on how this script is delivered finally.\n\n\n## Installation\nThis system can be installed like any other laravel system.\n#### If delivered via zip file\n- Unzip source code into root folder, eg `/var/www/html/notification`.\n- Open `.env` and add endpoint, telegram bot token and channel id as specified.\n- To test if it's ready to go, run this command to execute the command: `php artisan notice:board`.\n\n#### If delivered via Github Repo\n- Pull source code into root folder, eg `/var/www/html/notification`.\n- Copy and paste `.env.example` file and rename to `.env`.\n- Open `.env` and add endpoint, telegram bot token and channel id as specified.\n- Run this command via terminal inside the root folder: `composer install`.\n- Then run this command via terminal: `php artisan key:generate`.\n- To test if it's ready to go, run this command to execute the command: `php artisan notice:board`.\n\n## Installing on Other Platforms\nIf system is to be hosted on any other system (Cloud or Shared Hosting), The process is very sismilar, differences will be based on individual platforms.\nIf in doubt, get instructions on how to install laravel on that particular platform.\nOr you can reach to the developer for guidance.\n\n### Env Confirguration\nAdd the correct values for the following keys:\n\n| Key | Value |\n| ------ | ------ |\n| API_URL | \"https://api-mystland.up.railway.app/api/getUsers\" |\n| TELEGRAM_TOKEN | \"**************************\" |\n| TELEGRAM_CHANNEL_ID | \"-100************\" |\n\n## Extention\nThis system can be extended as user sees fit. \nThe codes are modular with proper seperation of concerns. \nIt is well documented and commented.\nWritten with well optimized code.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprevailexcel%2Ftelbot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprevailexcel%2Ftelbot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprevailexcel%2Ftelbot/lists"}