Giter Site home page Giter Site logo

Comments (18)

irazasyed avatar irazasyed commented on May 16, 2024 1

@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.

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

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.

from telegram-bot-sdk.

HamoonDBA avatar HamoonDBA commented on May 16, 2024

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

from telegram-bot-sdk.

defunctl avatar defunctl commented on May 16, 2024

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');

from telegram-bot-sdk.

HamoonDBA avatar HamoonDBA commented on May 16, 2024

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

from telegram-bot-sdk.

defunctl avatar defunctl commented on May 16, 2024

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

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

@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();

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

@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.

from telegram-bot-sdk.

HamoonDBA avatar HamoonDBA commented on May 16, 2024

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");

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

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.

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

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.

from telegram-bot-sdk.

HamoonDBA avatar HamoonDBA commented on May 16, 2024

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 !

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

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.

from telegram-bot-sdk.

HamoonDBA avatar HamoonDBA commented on May 16, 2024

@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

from telegram-bot-sdk.

irazasyed avatar irazasyed commented on May 16, 2024

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

from telegram-bot-sdk.

maralbe avatar maralbe commented on May 16, 2024

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

from telegram-bot-sdk.

saeedalzdh-zz avatar saeedalzdh-zz commented on May 16, 2024

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

from telegram-bot-sdk.

glenwell avatar glenwell commented on May 16, 2024

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

from telegram-bot-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.