The Show
Example #1
The above example will output: Example #2
The above example will output: Example #3 This will not work
The above example will output: Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2
Telegram announced official support for a Bot API, allowing integrators of all sorts to bring automated interactions to the mobile platform. This Bot aims to provide a platform where one can simply write a bot and have interactions in a matter of minutes. The Bot can:
This code is available on GitHub. Pull requests are welcome. InstructionsCreate your first bot
Optionally set the bot privacy:
Require this package with ComposerInstall this package through Composer. Edit your project's 0 file to require 1.Create composer.json file { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } } and run 2or run this command in your command line: composer require longman/telegram-bot Choose how to retrieve Telegram updatesThe bot can handle updates with or method: WebhookgetUpdatesDescriptionTelegram sends the updates directly to your hostYou have to fetch Telegram updates manuallyHost with httpsRequiredNot requiredMySQLNot required() RequiredUsing a custom Bot API serverFor advanced users only! As from Telegram Bot API 5.0, users can to handle updates. This means, that the PHP Telegram Bot needs to be configured to serve that custom URI. Additionally, you can define the URI where uploaded files to the bot can be downloaded (note the 3 placeholder).Longman\TelegramBot\Request::setCustomBotApiUri( $api_base_uri = 'https://your-bot-api-server', // Default: https://api.telegram.org $api_base_download_uri = '/path/to/files/{API_KEY}' // Default: /file/bot{API_KEY} ); Note: If you are running your bot in 4 mode, you won't need the 5 method, since you can then access your files directly from the absolute path returned by 6.Webhook installationNote: For a more detailed explanation, head over to the example-bot repository and follow the instructions there. In order to set a you need a server with HTTPS and composer support. (For a you need to add some extra code) Create set.php with the following contents: <?php // Load composer require __DIR__ . '/vendor/autoload.php'; $bot_api_key = 'your:bot_api_key'; $bot_username = 'username_bot'; $hook_url = 'https://your-domain/path/to/hook.php'; try { // Create Telegram API object $telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username); // Set webhook $result = $telegram->setWebhook($hook_url); if ($result->isOk()) { echo $result->getDescription(); } } catch (Longman\TelegramBot\Exception\TelegramException $e) { // log telegram errors // echo $e->getMessage(); } Open your set.php via the browser to register the webhook with Telegram. You should see 7.Now, create hook.php with the following contents: 0Self Signed CertificateUpload the certificate and add the path as a parameter in set.php: 1Unset WebhookEdit unset.php with your bot credentials and execute it. getUpdates installationFor best performance, the MySQL database should be enabled for the 8 method!Create getUpdatesCLI.php with the following contents: 2Next, give the file permission to execute: 3Lastly, run it! 4getUpdates without databaseIf you choose to / or are obliged to use the 8 method without a database, you can replace the { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }0 line above with: 5Filter Update❗Note that by default, Telegram will send any new update types that may be added in the future. This may cause commands that don't take this into account to break! It is suggested that you specifically define which update types your bot can receive and handle correctly. You can define which update types are sent to your bot by defining them when setting the or passing an array of allowed types when using . 6Alternatively, Update processing can be allowed or denied by defining a custom update filter. Let's say we only want to allow messages from a user with ID { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }1, we can do the following before handling the request: 7The reason for denying an update can be defined with the { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }2 parameter. This text gets written to the debug log. SupportTypesAll types are implemented according to Telegram API 6.3 (November 2022). Inline QueryFull support for inline query according to Telegram API 6.3 (November 2022). MethodsAll methods are implemented according to Telegram API 6.3 (November 2022). Send MessageMessages longer than 4096 characters are split up into multiple messages. 8Send PhotoTo send a local photo, add it properly to the { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }3 parameter using the file path: 9If you know the { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }4 of a previously uploaded file, just use it directly in the data array: 0To send a remote photo, use the direct URL instead: 1sendAudio, sendDocument, sendAnimation, sendSticker, sendVideo, sendVoice and sendVideoNote all work in the same way, just check the for the exact usage. See the ImageCommand.php for a full example. Send Chat Action 2getUserProfilePhotoRetrieve the user photo. (see WhoamiCommand.php for a full example) getFile and downloadFileGet the file path and download it. (see WhoamiCommand.php for a full example) Send message to all active chatsTo do this you have to enable the MySQL connection. Here's an example of use (check for parameter usage): 3You can also broadcast a message to users, from the private chat with your bot. Take a look at the below. UtilsMySQL storage (Recommended)If you want to save messages/users/chats for further usage in commands, create a new database ( { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }6), import structure.sql and enable MySQL support BEFORE { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }7 method: 4You can set a custom prefix to all the tables while you are enabling MySQL: 5You can also store inline query and chosen inline query data in the database. External Database connectionIt is possible to provide the library with an external MySQL PDO connection. Here's how to configure it: 6Channels SupportAll methods implemented can be used to manage channels. With you can manage your channels directly with your bot private chat. CommandsPredefined CommandsThe bot is able to recognise commands in a chat with multiple bots (/command@mybot). It can also execute commands that get triggered by events, so-called Service Messages. Custom CommandsMaybe you would like to develop your own commands. There is a guide to help you create your own commands. Also, be sure to have a look at the example commands to learn more about custom commands and how they work. You can add your custom commands in different ways: 7Commands ConfigurationWith this method you can set some command specific parameters, for example: 8Admin CommandsEnabling this feature, the bot admin can perform some super user commands like:
Take a look at all default admin commands stored in the src/Commands/AdminCommands/ folder. Set AdminsYou can specify one or more admins with this option: 9Telegram user id can be retrieved with the /whoami command. Channel AdministrationTo enable this feature follow these steps:
0
1
Upload and Download directory pathTo use the Upload and Download functionality, you need to set the paths with: 2DocumentationTake a look at the repo Wiki for further information and tutorials! Feel free to improve! AssetsAll project assets can be found in the assets repository. Example botWe're busy working on a full A-Z example bot, to help get you started with this library and to show you how to use all its features. You can check the progress of the example-bot repository). Projects with this libraryHere's a list of projects that feats this library, feel free to add yours!
TroubleshootingIf you like living on the edge, please report any bugs you find on the PHP Telegram Bot issues page. ContributingSee CONTRIBUTING for more information. SecuritySee SECURITY for more information. DonateAll work on this bot consists of many hours of coding during our free time, to provide you with a Telegram Bot library that is easy to use and extend. If you enjoy using this library and would like to say thank you, donations are a great way to show your support. Donations are invested back into the project👍 Thank you for keeping this project alive🙏
For enterpriseAvailable as part of the Tidelift Subscription. The maintainers of { "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }8 and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more. LicensePlease see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under. Bagaimana cara menjalankan program PHP?Untuk menjalankan file php, silahkan buka web browser seperti mozilla firefox, chrome, internet explorer atau web browser lain. Kemudian ketik url http://localhost/tutorial/ lalu enter, jika semua sudah benar maka akan tampil seperti gambar di bawah ini.
Langkah langkah membuat website dengan PHP?Langkah 1: Persiapan.. Langkah 2: Membuat File index. php.. Langkah 3: Membuat File style.css.. Langkah 4: Membuat File home.php.. Langkah 5: Membuat File about. php.. Langkah 6: Membuat File contact.php.. Langkah 7: Mencoba Website di Browser.. Langkah langkah membuat CRUD?Membuat CRUD di Halaman CRUD. Membuat Sistem Registrasi. Bagian ini nanti Anda akan membuat sebuah form registrasi lengkap dengan database di dalamnya. ... . Membuat File Config. ... . Membuat Form Registrasi. ... . Membuat Sistem Login. ... . Membuat Form Login. ... . 6. Membuat Halaman Utama. ... . 7. Membuat Script Logout.. PHP digunakan untuk apa saja?Seperti disebut sebelumnya, PHP adalah bahasa pemrograman yang ada di sisi server. Bahasa ini digunakan untuk mengelola konten dinamis, database, session tracking, dan bahkan keseluruhan situs e-commerce. Sebetulnya, kamu bisa membuat web dengan memakai HTML saja.
|