Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem in setting up web hook at Telegram #23

Closed
HamoonDBA opened this issue Sep 2, 2015 · 18 comments
Closed

Problem in setting up web hook at Telegram #23

HamoonDBA opened this issue Sep 2, 2015 · 18 comments

Comments

@HamoonDBA
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link

defunctl commented Sep 2, 2015

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
Copy link
Author

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

@defunctl
Copy link

defunctl commented Sep 2, 2015

@irazasyed
Copy link
Owner

@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
Copy link
Owner

@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
Copy link
Author

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

@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
Copy link
Owner

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

@irazasyed
Copy link
Owner

@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
Copy link

maralbe commented Dec 13, 2016

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
Copy link

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants