Closed frencisdrame closed 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.
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?
@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.
@kettari I know that, but this is not a problem, I'm explained badly.
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? :)
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
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.
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.
Thanks @kettari Now its more clearer, also what contains $arguments
The problem solution was: $chatId = $this->getTelegram()->getWebhookUpdates()->getMessage()->getChat()->getId();
The problem solution was: $chatId = $this->getTelegram()->getWebhookUpdates()->getMessage()->getChat()->getId();
this has changed to:
$chatId = $this->getTelegram()->getWebhookUpdate()->getMessage()->getChat()->getId();
Solution of problem: $chatId = $this->chat->chat_id;
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