I’m trying to make a bot that adds an user to the group. The bot receives a token and validates it, with the ones he has on an online database. It must be made in Laravel.
I’m doing with xampp’s localhost and Visual Studio Code.
There has been some progress, I made a webpage with some views in Laravel, there are three, one is to validate the token by manually inputting your email, phone number and the token, the other is to made the validation via photo, but that is not currently necessary, it is only an idea.
The last view is the bot log with its arrays.
I’ve successfully managed to link the bot to the aplication, and when I click submit on the first view, the one that lets you manually input your data, the bot successfuly sends the group chat a message, with the inputted data.
What I wanted to do next, is to make the bot reply to the user that starts a conversation with it, I wanted to make it so, that the bot automatically replies to certain words, and also receives the Token, and store it somewhere so it can be compared with what it has on the database.
I have almost no idea how to go about this, and I would be very thankful if you could shed some light for me.
I have read the documentation but it is somewhat confusing, I'm a newbie on it. How do I make it so that it replies to messages, or even receives it so it can compare with the tables he has on the DB? Do I need some online ftp repository?
Thank you!
👋 Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.
Hi,
To receive messages from a user or group you need to get the messages from the Telegram API. You can check for new messages every second or use a webhook. I would suggest to use the webhook. You make a Laravel route with a hard to guess url and send this to your bot with botfather. Everytime someone sends something to your bot, the Telegram API will send this to your webhook.
From the webhook you can make commands in Laravel who will respond to specific commands or any words.
Second thing you need is to keep track of your users and their Telegram ID. So I have a user database and I verify or connect my users with their Telegram ID. What I specific do:
If someone sends a message to my bot I receive that message via the Webhook and first I check in my database if I know this user by its Telegram ID. If yes, I send him a reply whatever is needed. So if a registered user sends a command /restartme and If I know the user, a restart process is starting for this user. All the admins will get a message from this action. Admins are special Telegram users who have a 'IsTelegramAdmin' mark in the user table.
I hope this makes sense.
My Webhook route example:
Route::post('/yournottobeabletoguessspecificwebhookurl/webhook', function () {
$update = Telegram::commandsHandler(true);
return 'ok';
});
Change the 'yournottobeabletoguessspecificwebhookurl' to something only you know and tell the botfather what URL your bot is accessible by.
Last but not least the basic help command who will respond to a known user or an unknown user:
public function handle()
{
Telegram::setAsyncRequest(false);
$update = $this->getUpdate();
$userid = $update->getMessage()->from->id;
$user = User::where('telegramid', $userid)->first();
if($user) {
$TelegramName = $user->firstname;
$text = 'Hey '.$TelegramName.', here is your personal menu:'.chr(10).chr(10);
$text .= '/status - bla'.chr(10);
$text .= '/detail - bla'.chr(10);
$text .= '/showkeyboard - Show keyboard'.chr(10).chr(10);
if (User::where('telegramid', $userid)->where('istelegramadmin', true)->exists()){
$text .= 'Additional Admin commands:'.chr(10);
$text .= '/restartme - bla'.chr(10).chr(10);
}
$text .= 'End of /help menu'.chr(10);
$this->replyWithMessage(compact('text'));
} else {
$text = 'Hey stranger, thank you for visiting me.'.chr(10).chr(10);
$text .= 'I do not know you. Please register first on'.chr(10);
$text .= 'https://www.yoururl.com'.chr(10).chr(10);
$text .= 'Please come and visit me.'.chr(10);
$this->replyWithMessage(compact('text'));
}
}
Good luck with your bot!
Hi,
To receive messages from a user or group you need to get the messages from the Telegram API. You can check for new messages every second or use a webhook. I would suggest to use the webhook. You make a Laravel route with a hard to guess url and send this to your bot with botfather. Everytime someone sends something to your bot, the Telegram API will send this to your webhook.
From the webhook you can make commands in Laravel who will respond to specific commands or any words.
Second thing you need is to keep track of your users and their Telegram ID. So I have a user database and I verify or connect my users with their Telegram ID. What I specific do:
- Let a user register on my website
- I generate a unique long 64-bit hash for that user and place it in the usertable
- I generate a QR code which contain a url with the unique hash to point to the user
- The user scan's the QR code and is redirected to the Telegram API where he sends a /start
- From the webhook I receive the incomming hash so now I know which user it is and I know its Telegram ID. I save this Telegram ID in the user table;
If someone sends a message to my bot I receive that message via the Webhook and first I check in my database if I know this user by its Telegram ID. If yes, I send him a reply whatever is needed. So if a registered user sends a command /restartme and If I know the user, a restart process is starting for this user. All the admins will get a message from this action. Admins are special Telegram users who have a 'IsTelegramAdmin' mark in the user table.
I hope this makes sense.
My Webhook route example:
Route::post('/yournottobeabletoguessspecificwebhookurl/webhook', function () { $update = Telegram::commandsHandler(true); return 'ok'; });Change the 'yournottobeabletoguessspecificwebhookurl' to something only you know and tell the botfather what URL your bot is accessible by.
Last but not least the basic help command who will respond to a known user or an unknown user:
public function handle() { Telegram::setAsyncRequest(false); $update = $this->getUpdate(); $userid = $update->getMessage()->from->id; $user = User::where('telegramid', $userid)->first(); if($user) { $TelegramName = $user->firstname; $text = 'Hey '.$TelegramName.', here is your personal menu:'.chr(10).chr(10); $text .= '/status - bla'.chr(10); $text .= '/detail - bla'.chr(10); $text .= '/showkeyboard - Show keyboard'.chr(10).chr(10); if (User::where('telegramid', $userid)->where('istelegramadmin', true)->exists()){ $text .= 'Additional Admin commands:'.chr(10); $text .= '/restartme - bla'.chr(10).chr(10); } $text .= 'End of /help menu'.chr(10); $this->replyWithMessage(compact('text')); } else { $text = 'Hey stranger, thank you for visiting me.'.chr(10).chr(10); $text .= 'I do not know you. Please register first on'.chr(10); $text .= 'https://www.yoururl.com'.chr(10).chr(10); $text .= 'Please come and visit me.'.chr(10); $this->replyWithMessage(compact('text')); } }Good luck with your bot!
Thank you for your ansewer, I've made great progress!
I was able to successfully set the webhook and change the domain name, now the Laravel interface that I used to make the bot send messages is working online.
However, I have copied your basic help command to serve as template into my TelegramBotController, but the bot does not seem to respond, could you please enlighten me on what I must be doing wrong?
Thank you!
Can you confirm that you can receive messages in Laravel from the Telegram API in your bot?
The help command in Laravel is already in place and should always work by default. If you do not receive any responses I think your website/bot is not receiving messages yet. Please check next steps:
php artisan vendor:publish —provider="Telegram\Bot\Laravel\TelegramServiceProvider"protected $except = [
‘/<yournottobeabletoguessspecificwebhookurl>/webhook/’,
Route::post('/<yournottobeabletoguessspecificwebhookurl>/webhook', function () {
$updates = Telegram::getWebhookUpdates();
$chat_id = $updates->getMessage()->getChat()->getId();
$response = Telegram::sendMessage([
'chat_id' => $chat_id,
'text' => 'Hello World'
]);
$messageId = $response->getMessageId();
return 'ok';
});
This piece of code will send Hello World back to any user who is sending any text to your bot channel. Please make sure you are able to see Hello World first before you debug the /help command.
Can you confirm that you can receive messages in Laravel from the Telegram API in your bot?
The help command in Laravel is already in place and should always work by default. If you do not receive any responses I think your website/bot is not receiving messages yet. Please check next steps:
- If you are using irazasyed Telegram Bot prior version 3.0 you need to register the service provider and Facade. From version 3.0 it is automatically done;
2a. Did you added the Telegram config with command:
php artisan vendor:publish —provider="Telegram\Bot\Laravel\TelegramServiceProvider"
2b. Edit app/config/telegram.php and add your bot and all the needed config parameters. (Here you can also add your custom commands and override the default /help command);- Edit your .ENV for the right env config including your API key;
- Exclude your webhook URL from the CSRF verification. The route example I gave you is a POST and therefore CSRF protection is in place. To disable this: Open /app/Http/Middleware/VerifyCsrfToken.php and write:
protected $except = [ ‘/<yournottobeabletoguessspecificwebhookurl>/webhook/’,
- To test your bot with webhook connection you can add the following to your route:
Route::post('/<yournottobeabletoguessspecificwebhookurl>/webhook', function () { $updates = Telegram::getWebhookUpdates(); $chat_id = $updates->getMessage()->getChat()->getId(); $response = Telegram::sendMessage([ 'chat_id' => $chat_id, 'text' => 'Hello World' ]); $messageId = $response->getMessageId(); return 'ok'; });This piece of code will send Hello World back to any user who is sending any text to your bot channel. Please make sure you are able to see Hello World first before you debug the /help command.
I've followed all your instructions, I think it is working, but the problem is, the bot simply doesn't respond, even the /help command does not work.
I think the problem is the Webhook, is it simply the address where the application is stored?
I've read an extensive documentation on how to set Webhooks but I still can't fully comprehend how they work and how to set them, I do not want to use 3rd party services as I own a domain.
Thank you for your patience!
I’m doing with xampp’s localhost and Visual Studio Code.
Is your development machine local?
Ie, can telegram access your development machine? If it's not accessible from the internet then Telegram can't deliver any commands/data to your machine to process.
I’m doing with xampp’s localhost and Visual Studio Code.
Is your development machine local?
Ie, can telegram access your development machine? If it's not accessible from the internet then Telegram can't deliver any commands/data to your machine to process.
Hello!
My development is online, I think I was able to set the webhook properly, this is the return I get
{"ok":true,"result":true,"description":"Webhook is already set"}
However, my bot still doesn't reply to any commands or texts I send him, I've been following this guide too: https://botman.io/2.0/driver-telegram
I decided to use botman as well, do you think it's efficient?
Setting your webhook does not confirm that your bot is accessible from the internet. By setting the webhook you only told Telegram API what your public URL is to send bot messages to.
You need to understand the two options you have to be able to answer the /help command:
Is your webhook URL accessible from the internet? Did you test it with the specific route code I wrote earlier?
Route::post('/<yournottobeabletoguessspecificwebhookurl>/webhook', function () {
$updates = Telegram::getWebhookUpdates();
$chat_id = $updates->getMessage()->getChat()->getId();
$response = Telegram::sendMessage([
'chat_id' => $chat_id,
'text' => 'Hello World'
]);
$messageId = $response->getMessageId();
return 'ok';
});
You need to confirm that you can receive messages from Telegram first!!
Setting your webhook does not confirm that your bot is accessible from the internet. By setting the webhook you only told Telegram API what your public URL is to send bot messages to.
You need to understand the two options you have to be able to answer the /help command:
- Pull the queued messages from the Telegram API every x seconds (semi automatic);
- Wait for the messages to be delivered by the telegram API at your webhook url (automatic).
Is your webhook URL accessible from the internet? Did you test it with the specific route code I wrote earlier?
Route::post('/<yournottobeabletoguessspecificwebhookurl>/webhook', function () { $updates = Telegram::getWebhookUpdates(); $chat_id = $updates->getMessage()->getChat()->getId(); $response = Telegram::sendMessage([ 'chat_id' => $chat_id, 'text' => 'Hello World' ]); $messageId = $response->getMessageId(); return 'ok'; });You need to confirm that you can receive messages from Telegram first!!
I managed to do everything, all I need now is to find a way to generate invitation links that revoke themselves at some time, or something similar