hi
can someone give me a sample webhook laravel project, like this: https://irazasyed.github.io/telegram-bot-sdk/usage/webhook-updates/
i will appreciate that
@moein-rahimi What part is it that you are having problems with?
The documents are very good, they give all the info you might need....but if there's an area you're not sure off, perhaps we can help with that.
Is it the creating of a bot? Setting up the webhook? Responding to the inbound call from telegram? Polling telegram for new updates?
Quite a range of topics there.
@jonnywilliamson my problem is this page : https://irazasyed.github.io/telegram-bot-sdk/usage/webhook-updates/
setting webhooks and getting updates via webhooks.
this is my controller :
public function setWebHook()
{
$response = \Telegram::setWebhook('https://safe-shore-1435.herokuapp.com/<token>/webhook');
//return $response;
}
public function updateWebHook()
{
$update = \Telegram::getWebhookUpdates();
dd($update);
//$chat_id = $update->getMessage()->getChat()->getId();
//\Telegram::sendMessage($chat_id, "Thanks", false, null, null);
//echo $chat_id;
}
and my route
Route::post('/webhook', 'Telegram@setWebHook');
Route::get('/test', 'Telegram@updateWebHook');
Let's get some info first.
Are you using the latest dev version from the repo? If so, there has been a large change in the way we call each of the methods on the API. We now use an array to contain all the parameters, rather than pass each parameter individually.
Also, do you want to do the polling method (ie you go and ask for the updates) or the push method (telegram sends you the updates automatically when they happen) which I think is much easier?
You're demo code shows the polling method, but I bet you probably want the push method.
If you're using this latest dev version, here's how it would look. I'm going to use the routes.php file instead of using controllers to keep it simple, but its very easy to move each of these to a controller method.
//routes.php
Route::get('/set', function () {
$res = Telegram::setWebhook([
'url' => 'https://safe-shore-1435.herokuapp.com/<token>/webhook'
]);
dd($res);
});
Route::post('<token>/webhook', function () {
/** @var \Telegram\Bot\Objects\Update $update */
$update = Telegram::commandsHandler(true);
return 'ok';
});
Then all you would need to do is call the set url ONCE to set the webhook.
ie open your browser and visit https://safe-shore-1435.herokuapp.com/set
Once that is done, you can open your phone, find your bot on telegram and send it this command
/help
(Or set this up with something like postman to post to your URL manually yourself)
You should get a message back instantly,
If you're using the OLD version of this API (to be fair its the one currently published but it will be obsolete soon) then your code would look like this:
//routes.php
Route::get('/set', function () {
$res = Telegram::setWebhook('https://safe-shore-1435.herokuapp.com/<token>/webhook');
dd($res);
});
Route::post('<token>/webhook', function () {
/** @var \Telegram\Bot\Objects\Update $update */
$update = Telegram::commandsHandler(true);
return 'ok';
});
the first get me a invalid url provides when i vistited /set url.
the second one return me a true but when i type /help it doesnt respond.
thanks for reply
You'll have to do some debugging. The above code works, so it's an error in your setup/url etc.
Use postman extension on chrome to send post requests to your URL and see if it is responding ok.
I sent this via postman : https://safe-shore-1435.herokuapp.com/153172554:AAElVtw4WJS4noPYXsnOtKLKLGJ0oeiUhTI/webhook
and i get this:
TokenMismatchException in VerifyCsrfToken.php line 53:
Open app\Http\Middleware\VerifyCsrfToken.php
Add your URL to the array like this
protected $except = [
'153172554:AAElVtw4<snip-dontpostyourtoken>snOtKL/webhook'
];
when i send a get rquest to this url i get MethodNotAllowedHttpException and when i post it i get TokenMismatchException
https://safe-shore-1435.herokuapp.com/153172554:AAElVtw4WJS4noPYXsnOtKLKLGJ0oeiUhTI/webhook
You can't GET to a POST url. Don't do that.
You WON'T get a TokenMismatchException if you properly added the POST url to the $except array in app\Http\Middleware\VerifyCsrfToken.php.
Slow down and do it properly.
ok now when i send /help it return this : /help - Help command, Get a list of commands
how to interact with the user?
for example when he sends something, the bot says HI!

Do some reading of the docs.
ok, i took a look at the docs and i get to this:
$update = \Telegram::commandsHandler(true);
$update->sendMessage('CHAT_ID', 'Hello World');
return $updates;
but it doesn't work
No you need to REGISTER a command that your bot will reply to.
The /help command has been done already for you as an example.
Please read https://irazasyed.github.io/telegram-bot-sdk/usage/commands/ this page carefully.
@moein-rahimi Closing issue since i believe @jonnywilliamson has given enough info to help you with the issue. Please consider going through the docs fully. Any other issues, Open a new one. Thanks!
This is my code: VerifyCsrfToken.php
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array
*/
protected $except = [
'*:*/webhook',
];
}
Most helpful comment
Let's get some info first.
Are you using the latest dev version from the repo? If so, there has been a large change in the way we call each of the methods on the API. We now use an array to contain all the parameters, rather than pass each parameter individually.
Also, do you want to do the polling method (ie you go and ask for the updates) or the push method (telegram sends you the updates automatically when they happen) which I think is much easier?
You're demo code shows the polling method, but I bet you probably want the push method.
If you're using this latest dev version, here's how it would look. I'm going to use the
routes.phpfile instead of using controllers to keep it simple, but its very easy to move each of these to a controller method.Then all you would need to do is call the
seturl ONCE to set the webhook.ie open your browser and visit
https://safe-shore-1435.herokuapp.com/setOnce that is done, you can open your phone, find your bot on telegram and send it this command
/help(Or set this up with something like postman to post to your URL manually yourself)
You should get a message back instantly,
If you're using the OLD version of this API (to be fair its the one currently published but it will be obsolete soon) then your code would look like this: