Home php Laravel telegram bot sdk

Laravel telegram bot sdk

Author

Date

Category

There is a question: I make a telegram bot to order, on laravel, I use the laravel sdk ^ 2.0 (irazasyed author) package, I raised a server with a self-written certificate, I did everything according to the tutorials, they say I need to set up a web hook using a link in the address bar https://api.telegram.org/bot & lt; my botfather token & gt; / setWebhook? url = & lt; Domain + link, which is registered in the routes & gt;, to which I receive a json response: Webhook was set, in the controller the code is as follows:

use Illuminate \ Http \ Request;
use Telegram \ Bot \ Api;
use Telegram \ Bot \ Laravel \ Facades \ Telegram;
class TelegramController extends Controller
{
  public function webhook (Request $ request) {
    $ key = env ('TELEGRAM_TOKEN', 'my token');
    $ telegram = new Api ($ key);
    $ result = $ telegram- & gt; getWebhookUpdate ();
    $ text = isset ($ result ["message"] ["text"])? $ result ["message"] ["text"]: "";
    $ chat_id = isset ($ result ["message"] ["chat"] ["id"])? $ result ["message"] ["chat"] ["id"]: 0;
    $ username = isset ($ result ["message"] ["from"] ["username"])? $ result ["message"] ["from"] ["username"]: "";
    $ telegram- & gt; sendMessage ([
      'chat_id' = & gt; & lt; my chat id & gt ;,
      'text' = & gt; $ text
    ]);
  }
}

routes:

Route :: match (['get', 'post'], 'webhook / telegram', [
  'uses' = & gt; 'TelegramController @ webhook'
]);

I checked in the postman, if you manually send a request to send a message, then everything is ok, but if you send a message to the bot, then the reactions are 0, because I think that the problem is somewhere between the server and the bot (erratic entries in the laravel and server logs was not), I mean, the request from the bot goes ok, but it does not go to my server, and if it does, it’s not to the method, I tried to debug in the same way, whether something comes in there at all, the result is negative.
I ask for help with this issue, I tried everything.
Thanks in advance.


Answer 1, authority 100%

  1. The irazasyed library has a command php artisan telegram: webhook , using it you can more easily control the webhook directly from your application
  2. For the webhook, only the POST request type is required, in the routes there is no need to create a get request for this.
  3. Most likely the problem is that you have a self-signed certificate. For it to work with the Telegram API, you need to send the public key file along with the setWebhook method. If you will use the telegram: webhook command, then you need to specify the certificate_path parameter in the configuration of your bot (config / telegram.php ), in which specify the path to the file with the public key.

Unfortunately, the irazasyed library is badly documented and has many problems in the code, so it is rather uncomfortable for users who do not dig into it directly. It is for this reason that I wrote my own library and focused on documentation and examples westacks / telebot , if you are not yet very integrated with the irazasyed library, then you will be more comfortable familiarizing yourself with my library.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions