irazasyed / telegram-bot-sdk

🤖 Telegram Bot API PHP SDK. Lets you build Telegram Bots easily! Supports Laravel out of the box.
https://telegram-bot-sdk.com
BSD 3-Clause "New" or "Revised" License
3.02k stars 669 forks source link

Laravel: No Responce #432

Closed JBird608 closed 7 years ago

JBird608 commented 7 years ago

Hi all,

I have been trying to get this package to work for a little time now, so far I have been able to publish a message to Telegram, however I am unable to get a responce from anything.

I have a controller set up for the two different types of request TelegramController.

<?php

namespace App\Http\Controllers;

use Telegram\Bot\Api;
use Telegram\Bot\Laravel\Facades\Telegram;

class TelegramController extends Controller
{

    public function without()
    {

        $telegram = new Api(env('TELEGRAM_BOT_TOKEN'));

        $response = $telegram->getMe();

        return $response;
    }

    public function with()
    {

        $response = Telegram::getMe();

        $botId = $response->getId();
        $firstName = $response->getFirstName();
        $username = $response->getUsername();

        return $response;
    }
}

When calling the method without I get the JSON responce, however if I call the with I get nothing.

Does anyone know how to solve this problem so I can use the Facade?

Cheers, Joe.

alighlm commented 7 years ago

Hi Joe, It's because you have set Asynchronous request true in telegram.php in config folder. So Telegram::getMe() will call the request but doesn't wait to receive a response from the server. so you will face a free response. you can set setAsyncRequest false,

irazasyed commented 7 years ago

Hi,

Please refer this project: https://github.com/irazasyed/telegram-bot-laravel-starter

You don't have to initialize the Api class if you set up the service provider in your app.php file and then set bot token in .env file.