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
2.99k stars 660 forks source link

Get chat_id in a command through the method: handle ($arguments) #265

Closed frencisdrame closed 7 years ago

frencisdrame commented 7 years ago

Hello. I can't get chat_id to use it in a command through the method: handle ($arguments). I tried to call through:

$chatId = $this->getUpdate()->getMessage()->getChat()->getId(); //NO $chatId = $this->update->getMessage()->getChat()->getId(); $chatId = $arguments[0]; //NO

But is emply: PHP Fatal error: Uncaught Telegram\Bot\Exceptions\TelegramOtherException: Bad Request: chat_id is empty in /app/vendor/irazasyed/telegram-bot-sdk/src/Exceptions/TelegramResponseException.php:58

kettari commented 7 years ago

Hi,

1) First thing I recommend to debug: check what kind of inbound message you receive from Telegram service. Is there Message object in the input at all?

I myself print every in- and outbound object that goes through the API to the log so it's clear what is being processed.

2) Print_r() to the log objects Update and Message. Make sure they are present.

3) Use test scripts published on the Telegram bot API page to test your application to be sure what you are processing: https://core.telegram.org/bots/webhooks Find on the page section "Testing your bot with updates"

Hope this helps.

frencisdrame commented 7 years ago

Thanks, I will treasure your suggestions, but I was unable to understand how to retrieve "chat_id" passed when called command via the object $telegrams->commandsHandler (true); Naturally i can send and receive messages via $this->ReplyWithMessage (['text' => $text) but if, for example, I want to use $telegrams=>sendMessage (['chat_id' => $chatId, 'text' = > $text]); can I get chat_id in any way?

kettari commented 7 years ago

@frencisdrame, As you know user MUST initiate conversation with the bot — it's requirement of the Telegram Bot API. So when you get this first message from the user, store user_id + chat_id combination somewhere on your side.

When you want to send something to the user, look at your data and retrieve chat_id from it.

frencisdrame commented 7 years ago

@kettari I know that, but this is not a problem, I'm explained badly.

kettari commented 7 years ago

When you instantiate Telegram API SDK, it knows nothing about chats and users. It has no way to guess where to send messages.

So if you know that, what is your question? :)

frencisdrame commented 7 years ago

The question was, how to retrieve chat_id within a command, when it is given control to a command in response to a user's message. And 'as if once recovered the message that activated the command, it was no longer possible to recover chat_id. I tried to explode $arguments, as follows:          $argument_array = explode (' ', $arguments);          foreach ($argument_array as $key => $value) {              $this->replyWithMessage (['text' => 'Argument'. $key. ':'. $value]);

with the following response Argument0: ... $value is null in short, but the messages are still sent in response to the bot

kettari commented 7 years ago

chat_id is always avalaible if you responding to user message via update-message-chat-id

I suggested "first message" if you want your bot to be active, not just reactive.

kettari commented 7 years ago

Argument is just a part of text message from the user after the command in your example. It doesn't have any objects (and ids) — plain text only.

If I remember correct (3 months passed I coded this fragment in my bot) if user sends "/hello world" then you got the substring "world" in the argument.

frencisdrame commented 7 years ago

Thanks @kettari Now its more clearer, also what contains $arguments

frencisdrame commented 7 years ago

The problem solution was: $chatId = $this->getTelegram()->getWebhookUpdates()->getMessage()->getChat()->getId();

RhysLees commented 3 years ago

The problem solution was: $chatId = $this->getTelegram()->getWebhookUpdates()->getMessage()->getChat()->getId();

this has changed to:

$chatId = $this->getTelegram()->getWebhookUpdate()->getMessage()->getChat()->getId();

Metraaa7 commented 10 months ago

Solution of problem: $chatId = $this->chat->chat_id;