botman / botman

A framework agnostic PHP library to build chat bots
https://botman.io
MIT License
6.06k stars 812 forks source link

Can't get response in Telegram #1351

Closed adolfoholzer closed 1 year ago

adolfoholzer commented 1 year ago

Description:

I have some conversations and they work OK when I use them with the web driver but when I change to Telegram driver I can't get any response from the server.

I connected my local server with ngrok and my laravel receives the request but when I try to start the same conversations I use in web they don't show anything.

Any idea?

Steps To Reproduce:

filippotoso commented 1 year ago

I have the same enviroment and it works correctly.

Have you set correctly the Telegram token in the /config/botman/telegram.php file?

adolfoholzer commented 1 year ago

Affirmative.

TELEGRAM_TOKEN={TOKEN} in my .env file.

'token' => env('TELEGRAM_TOKEN') in my config/botman/telegram.php file

In config/services.php file:

'botman' => [
   [...]
   'telegram_token' => env('TELEGRAM_TOKEN'),
   [...]
]

In web routes:

Route::match(['get', 'post'], '/botman', ['App\Http\Controllers\BotController', 'handle']);
Route::get('/botman/tinker', ['App\Http\Controllers\BotController', 'tinker']);

In my BotController:

public function handle(BotService $service) {

    $botman = app('botman');

    Log::notice('botman loaded'); // THIS REGISTERS OK

    $botman->hears('botman', function (BotMan $bot) {
        Log::warning('por decir botmaaan'); // THIS ALSO REGISTERS OK
        $bot->reply('im botmaaan');
        Log::critical('ya dijo botmaaan'); // SAME AS LAST
    });

}

All the conversations works just as expected in web but no answer in telegram. Do you see some wrong?

Thanks for taking time to reply.

filippotoso commented 1 year ago

This setup works perfectly:

https://github.com/botman/botman/discussions/1308#discussioncomment-3008604

adolfoholzer commented 1 year ago

Still not working for me. Thanks anyway.

filippotoso commented 1 year ago

If the suggested code doesn't work it means something wrong in the communication between your app and Telegram.

adolfoholzer commented 1 year ago

Messages from Telegram bot arrives ok to my app and Botman listens them, but when I try to say, reply or startConversation I cannot comunicate anything to users.

TOKEN ok. Telegram Driver installed. Ngrok tunnel working fine.

What a mistery.

filippotoso commented 1 year ago

Is the throw_http_exceptions setting enabled in config/botman/telegram.php?

If not, try enabling it and check for exceptions when you send a message to Telegram.

https://github.com/botman/driver-telegram/blob/master/src/TelegramDriver.php#L405

adolfoholzer commented 1 year ago

{"exception":"[object] (BotMan\Drivers\Telegram\Exceptions\TelegramConnectionException(code: 0): Status Code: 500 Description: No description from Telegram Error Code: No error code from Telegram Parameters: No parameters from Telegram URL: https://api.telegram.org/botTELEGRAM-TOKEN-HIDDEN/sendMessage

I tried to send via URL the message and it works fine. Still not working from telegram driver. Ideas?

filippotoso commented 1 year ago

Set a try / catch and dump the json of the response:

try {
    // call
} catch (\Exception $e) {
    dump($e->getResponse()->getBody()->getContents());
}
adolfoholzer commented 1 year ago

This setup works perfectly:

https://github.com/botman/botman/discussions/1308#discussioncomment-3008604

Already tried this from zero and it doesn't work either.