skip to Main Content

I am using PHP 7.4.1 and Laravel Framework 6.20.16.

I am trying to implement the following library: telegram-bot-sdk and the following version "irazasyed/telegram-bot-sdk": "^2.0",

After installing the sdk and getting my private token from telegram’s @botfather. I am trying to use the sdk.

I created a route and a controller:

route

Route::get('telegramHello', 'TelegramController@getHello');

controller

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use TelegramBotApi as Telegram;
use AppHelpersTelegramResponse as Response;

class TelegramController extends Controller
{
    public function getHello() {
        $api = new Telegram(); // ----> HERE I GET THE ERROR

        $response = $api->getMe();
        return Response::handleResponse($response);
    }
//...

When opening my route I get the following exception:

enter image description here

The thing I do not understand is that I have created the config telegram.php and loading my correct token from my .env file:

enter image description here

In my .env file it looks like the following:

enter image description here

Any suggestions what I am doing wrong?

I appreciate your replies!

2

Answers


  1. Chosen as BEST ANSWER

    The two comments above helped me the most:

    1. Use "" for your TELEGRAM_BOT_TOKEN
    2. Instead of using your own named .env variable use TELEGRAM_BOT_TOKEN

    I hope this works also for others that have this problem.


  2. Use Facade, not original API class. Your config is correct, you just using wrong class.

    <?php
    
    namespace AppHttpControllers;
    
    use IlluminateHttpRequest;
    use TelegramBotLaravelFacadesTelegram;
    use AppHelpersTelegramResponse as Response;
    
    class TelegramController extends Controller
    {
        public function getHello() {
    
            $response = Telegram::getMe();
            return Response::handleResponse($response);
        }
    //...
    

    Also i may recommend you using westacks/telebot instead of irazasyed/telegram-bot-sdk. I created it as irazasyed’s was poorly documented and really buggy at a lot of places.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search