-
-
Notifications
You must be signed in to change notification settings - Fork 960
Getting started with php telegram bot and webhooks
First thing you must know is that library requires you create commands and do everything from there, which means it controls how you create the bots in a right way.
Now lets see what is a command? a command in this library is a class that library calls when someone calls its $usage in your bot, for example when someone sends /start to your bot library calls StartCommand class from your registered directory or if it doesn't exists it looks at its own default classes.
Then before you start calling handle() method you must give the library your commands directory path.
Next thing you must know is that in this library there is class called conversation. Conversation is a way to remember the user status and some additional info.
For example lets say you have a bot which contains two menus, and you want your bot remembers in what state a user is now, so when he/she taps on a button your bot directly executes that command instead of starting from the first command.
As you can see conversation needs a way to store its data. So for using this feature you must create a database for this library and give it its credentials before calling the handle method.
First download the repository with composer.
In repository home page there is two code samples, one for set webhook url, and one for handling received updates from telegram. Create two files in your project root directory named setwebhook.php
and handleUpdates.php
, and insert that sample codes to this files.
Next open handleUpdates.php
file and edit the code like this:
$telegram = new Longman\TelegramBot\Telegram($bot_api_key, $bot_username);
//#1- Our commands paths
$telegram->addCommandsPaths([
__DIR__ . '/path/to/my/commands/directory/',
]);
//#2- Enable requests limiter, to prevent attack like requests
//This prevents library from processing repetitive requests
$telegram->enableLimiter();
//#3- A database for the library
$mysql_credentials = [
'host' => 'localhost',
'user' => 'root',
'password' => '',
'database' => 'mybotdbname',
];
$telegram->enableMySql($mysql_credentials);
//Then call handle and catch the errors and etc..
Now in your project directory, copy vendor/longman/telegram-bot/src/Commands/UserCommands/StartCommand.php
to your own commands directory.
Now if a user sends /start to your bot, library calls execute() method in your own StartCommand, lets fill it with some useful data.
Open this url in your browser: https://github.com/php-telegram-bot/example-bot/tree/master/Commands, then open one of example commands and replace everything within your StartCommand execute() method with the contents of that example command. Now again send /start to your bot in telegram.
Now download the example-bot library into your local, and copy SurveyCommand.php to your project commands directory, and send /survey to your bot.
For more info about how to use keyboards or how to work with conversations, CallbackQueries and etc... look at example commands, search in google, search in open and closed issues with different keywords, or open an issue in case you can not find any results.