The goto operator can be used to jump to another section in the program. The target point is specified by a case-sensitive label followed by a colon, and the instruction is given as goto followed by the desired target label. This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one. You also cannot jump into any sort of loop or switch structure. You may jump out of these, and a common use is to use a goto in place of a multi-level break.
Example #1 goto example
goto a;
echo 'Foo';
a:
echo 'Bar';
?>
The above example will output:
Example #2 goto loop example
goto0
The above example will output:
Example #3 This will not work
goto1
The above example will output:
Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2
goto2
goto3
goto4
goto5
goto6
goto7
goto8
goto9
goto0
goto1
goto2
goto3
goto4
goto5
goto6
goto7
goto8
goto9
goto0
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:
- Retrieve updates with and methods.
- Supports all types and methods according to Telegram Bot API 6.3 (November 2022).
- Supports supergroups.
- Handle commands in chat with other bots.
- Manage Channel from the bot admin interface.
- Full support for inline bots.
- Inline keyboard.
- Messages, InlineQuery and ChosenInlineQuery are stored in the Database.
- Conversation feature.
This code is available on GitHub. Pull requests are welcome.
Instructions
Create your first bot
Message Choose a bot to change group messages settings. 3 with the following text: Choose a bot to change group messages settings. 4
If you don't know how to message by username, click the search field on your Telegram app and type Choose a bot to change group messages settings. 3, where you should be able to initiate a conversation. Be careful not to send it to the wrong contact, because some users have similar usernames to Choose a bot to change group messages settings. 6.
Choose a bot to change group messages settings. 3 replies with:
Alright, a new bot. How are we going to call it? Please choose a name for your bot.
Type whatever name you want for your bot.
Choose a bot to change group messages settings. 3 replies with:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot.
Type whatever username you want for your bot, minimum 5 characters, and must end with Choose a bot to change group messages settings. 9. For example: 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED 0
Choose a bot to change group messages settings. 3 replies with:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api
Note down the 'token' mentioned above.
Optionally set the bot privacy:
Send 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED 2 to Choose a bot to change group messages settings. 3.
Choose a bot to change group messages settings. 3 replies with:
Choose a bot to change group messages settings.
Type (or select) 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED 5 (change to the username you set at step 5 above, but start it with 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED 6)
Choose a bot to change group messages settings. 3 replies with:
'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED
Type (or select) 'Enable' - your bot will only receive messages that either start with the '/' symbol or mention the bot by username. 'Disable' - your bot will receive all messages that people send to groups. Current status is: ENABLED 8 to let your bot receive all messages sent to a group.
Choose a bot to change group messages settings. 3 replies with:
Success! The new status is: DISABLED. /help
Require this package with Composer
Install this package through Composer. Edit your project's Success! The new status is: DISABLED. /help 0 file to require Success! The new status is: DISABLED. /help 1.
Create composer.json file
{ "name": "yourproject/yourproject", "type": "project", "require": { "php": ">=7.3", "longman/telegram-bot": "*" } }
and run Success! The new status is: DISABLED. /help 2
or
run this command in your command line:
composer require longman/telegram-bot
Choose how to retrieve Telegram updates
The 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 server
For 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 Success! The new status is: DISABLED. /help 3 placeholder).
Longman\TelegramBot\Request::setCustomBotApiUri( $api_base_uri = '//your-bot-api-server', // Default: //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 Success! The new status is: DISABLED. /help 4 mode, you won't need the Success! The new status is: DISABLED. /help 5 method, since you can then access your files directly from the absolute path returned by Success! The new status is: DISABLED. /help 6.
Webhook installation
Note: 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 = '//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 Success! The new status is: DISABLED. /help 7.
Now, create hook.php with the following contents:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 0
Self Signed Certificate
Upload the certificate and add the path as a parameter in set.php:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 1
Unset Webhook
Edit unset.php with your bot credentials and execute it.
getUpdates installation
For best performance, the MySQL database should be enabled for the Success! The new status is: DISABLED. /help 8 method!
Create getUpdatesCLI.php with the following contents:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 2
Next, give the file permission to execute:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 3
Lastly, run it!
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 4
getUpdates without database
If you choose to / or are obliged to use the Success! The new status is: DISABLED. /help 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:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 5
Filter 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 .
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 6
Alternatively, 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:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 7
The 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.
Support
Types
All types are implemented according to Telegram API 6.3 (November 2022).
Inline Query
Full support for inline query according to Telegram API 6.3 (November 2022).
Methods
All methods are implemented according to Telegram API 6.3 (November 2022).
Send Message
Messages longer than 4096 characters are split up into multiple messages.
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 8
Send Photo
To 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:
Good. Now let's choose a username for your bot. It must end in `bot`. Like this, for example: TetrisBot or tetris_bot. 9
If 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:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 0
To send a remote photo, use the direct URL instead:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 1
sendAudio, 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
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 2
getUserProfilePhoto
Retrieve the user photo. (see WhoamiCommand.php for a full example)
getFile and downloadFile
Get the file path and download it. (see WhoamiCommand.php for a full example)
Send message to all active chats
To do this you have to enable the MySQL connection. Here's an example of use (check for parameter usage):
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 3
You can also broadcast a message to users, from the private chat with your bot. Take a look at the below.
Utils
MySQL 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:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 4
You can set a custom prefix to all the tables while you are enabling MySQL:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 5
You can also store inline query and chosen inline query data in the database.
External Database connection
It is possible to provide the library with an external MySQL PDO connection. Here's how to configure it:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 6
Channels Support
All methods implemented can be used to manage channels. With you can manage your channels directly with your bot private chat.
Commands
Predefined Commands
The 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 Commands
Maybe 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:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 7
Commands Configuration
With this method you can set some command specific parameters, for example:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 8
Admin Commands
Enabling this feature, the bot admin can perform some super user commands like:
- List all the chats started with the bot /chats
- Clean up old database entries /cleanup
- Show debug information about the bot /debug
- Send message to all chats /sendtoall
- Post any content to your channels /sendtochannel
- Inspect a user or a chat with /whois
Take a look at all default admin commands stored in the src/Commands/AdminCommands/ folder.
Set Admins
You can specify one or more admins with this option:
Done! Congratulations on your new bot. You will find it at telegram.me/telesample_bot. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. Use this token to access the HTTP API: 123456789:AAG90e14-0f8-40183D-18491dDE For a description of the Bot API, see this page: //core.telegram.org/bots/api 9
Telegram user id can be retrieved with the /whoami command.
Channel Administration
To enable this feature follow these steps:
- Add your bot as channel administrator, this can be done with any Telegram client.
- Enable admin interface for your user as explained in the admin section above.
- Enter your channel name as a parameter for the /sendtochannel command:
Choose a bot to change group messages settings. 0
- If you want to manage more channels:
Choose a bot to change group messages settings. 1
- Enjoy!
Upload and Download directory path
To use the Upload and Download functionality, you need to set the paths with:
Choose a bot to change group messages settings. 2
Documentation
Take a look at the repo Wiki for further information and tutorials! Feel free to improve!
Assets
All project assets can be found in the assets repository.
Example bot
We'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 library
Here's a list of projects that feats this library, feel free to add yours!
- Inline Games (@inlinegamesbot)
- Super-Dice-Roll (@superdiceroll_bot)
- tg-mentioned-bot
- OSMdeWikiBot (@OSM_de)
- pass-generator-webbot
- Chess Quiz Bot
Troubleshooting
If you like living on the edge, please report any bugs you find on the PHP Telegram Bot issues page.
Contributing
See CONTRIBUTING for more information.
Security
See SECURITY for more information.
Donate
All 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🙏
- Patreon.com/phptelegrambot
- OpenCollective.com/php-telegram-bot
- Ko-fi.com/phptelegrambot
- Tidelift.com/longman/telegram-bot
- Liberapay.com/PHP-Telegram-Bot
- PayPal.me/noplanman (account of @noplanman)
- 166NcyE7nDxkRPWidWtG1rqrNJoD5oYNiV
- 0x485855634fa212b0745375e593fAaf8321A81055
For enterprise
Available 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.
License
Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.