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

Problem in setting up web hook at Telegram #23

Closed HamoonDBA closed 9 years ago

HamoonDBA commented 9 years ago

I've designed a robot for Telegram. After setting up web hook and sending messages to this robot, the robot response me as repetitious, as if the response of robot is on loop. My code is simple, but I don’t know why telegram sends duplicate massages for my web hook.

irazasyed commented 9 years ago

Can you post your code? Without that it would be hard to tell where the problem could be.

Also do you use it with Laravel or just standalone? Maybe share your log as well.

HamoonDBA commented 9 years ago

Yes this is my code:

    $updates = Telegram::getWebhookUpdates();
    $response = json_decode(file_get_contents('php://input'));
    $chat_id = $response->message->chat->id;
    $response = Telegram::sendMessage($chat_id, "Thanks", false, null, null);
    Telegram::getMessageId();
    return response()->json(["status" => "success"]);

Now if a user send Message to my Bot ,"Thanks" Message always send to the user!! I don't know why but I'm sure Telegram call my webhook as repetitious I use it with Laravel 5.1

defunctl commented 9 years ago

Try this, it works for me:

$telegram = new Telegram();  
$update = $telegram->getWebhookUpdates();  
$message = new Message($update->get('message'));  
$telegram->sendMessage($message['chat']['id'], 'Hello, this is a bot');
HamoonDBA commented 9 years ago

Thanks @defunctl But Can you tell me what is the Message class ? I don't have it

defunctl commented 9 years ago

It's an object of this package. https://github.com/irazasyed/telegram-bot-sdk/blob/master/src/Objects/Message.php

irazasyed commented 9 years ago

@HaMo0n It seems like you're using Laravel?

Here's how i would do that:

$update = Telegram::getWebhookUpdates();
$chat_id = $update->getMessage()->getChat()->getId();
Telegram::sendMessage($chat_id, "Thanks", false, null, null);
return response()->json(["status" => "success"]);

@defunctl The library will automatically map all the relations to the appropriate object class, So you don't have to map it again.

So when you make a call to getWebhookUpdates() method, The response is first mapped to the Update class, then since the response would content message key in its object, the library will then map it to the Message class automatically. Hence, You can get chat id by just doing:

$chat_id = $update->getMessage()->getChat()->getId();
irazasyed commented 9 years ago

@HaMo0n If your script returns any Error and status code other than 200 which it does based on your code, Then Telegram servers assume the update wasn't accepted and failed from your end. So they end up retrying for a few times, due to which you're getting that message multiple times. FYI.

HamoonDBA commented 9 years ago

thanks @irazasyed But the problem still remains :( new Function after change:

    $update = Telegram::getWebhookUpdates();
    $chat_id = $update->getMessage()->getChat()->getId();
    $command = $this->analyzeText($update->getMessage()->getText());
    switch ($command) {
        case "start":
            $this->startFirstChat($chat_id);
            break;
        default :
            $this->sendMessage($chat_id, "Thanks");
            break;
    }
    return (new Response(["status" => "success"], 200))
              ->header('Content-Type', "Application/json");
irazasyed commented 9 years ago

It seems like you're working on commands area, Why don't you use the built in commands system that i pushed recently?

Check this comment i made here, It's in development, So you need to follow all the instructions there and try.

irazasyed commented 9 years ago

BTW, Laravel has CSRF Middleware protection for any POST routes ON by default. You need to make sure you add your route to the except array inside app/Http/Middleware/VerifyCsrfToken.php file (Laravel 5 has it). If you don't do that, then it'll always fail because the middleware is checking for a valid token which obviously is not present.

HamoonDBA commented 9 years ago

I commented VerifyCsrfToken because of this.

Edit: I run api manually and this error was shown to me:

          ErrorException in Telegram.php line 653:
          Undefined offset: 0

you know last day It was Ok and worked !

irazasyed commented 9 years ago

Well, It's a problem from your end as i tested and I'm using this with my bots too. It's working absolutely fine. Now how you've implemented is something i have no idea about. You're giving me parts of the codes and based on that, I've suggested you on how to go about it.

HamoonDBA commented 9 years ago

@irazasyed. This package works fine on my local host. I thinks the host has a problem. I'm working on package for solve this problem. Thank you

irazasyed commented 9 years ago

Okay! Let me know if you find a problem with this SDK. Please create a new issue ticket. Thanks!

irazasyed commented 8 years ago

@shankar96 Not sure why you're asking about node.js in a php project?

But anyway, Telegram tries to resend the message until you respond it with header status code 200, once you do that, their system thinks you accepted the update. Just do that and it'll stop it.

maralbe commented 7 years ago

hi im pretty new in creating a telegram bot.. can you give me such an example.. how to set webhook in laravel using this sdk.. and how to get message from my users that using my bot..

thank you

saeedalzdh-zz commented 7 years ago

I have the same problem and have no idea why ?!

exception 'ErrorException' with message 'Undefined offset: 0' in /var/www/shokraneh/vendor/irazasyed/telegram-bot-sdk/src/Api.php:1057

try { $update = \Telegram::getWebhookUpdate(); \Storage::put('done.txt', $update) } catch (\Exception $e) { \Storage::put('error.txt', $e); return response()->json(["status" => "success"]); }

Help please

glenwell commented 5 years ago

I have the same problem and have no idea why ?!

exception 'ErrorException' with message 'Undefined offset: 0' in /var/www/shokraneh/vendor/irazasyed/telegram-bot-sdk/src/Api.php:1057

try { $update = \Telegram::getWebhookUpdate(); \Storage::put('done.txt', $update) } catch (\Exception $e) { \Storage::put('error.txt', $e); return response()->json(["status" => "success"]); }

Help please

Make sure you are using v3.0.0