1

I'm trying to get the title of a group chat but it always pass me Undefined array key "chat"
This is the code I'm using:

$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$title = $update['chat']['title'];

I'm probably doing something wrong with the bot APIs, this is the result of /getChat?chat_id=telegram_id

{"ok":true,"result":{"id":TELEGRAM_ID,"title":"NAME","type":"supergroup",...}}

Here's a minimal example:
$update = file_get_contents('php://input');
$update = json_decode($update, TRUE);
$title = $update['chat']['title'];
$chatid = $update['message']['chat']['id'];
if ($chatType == 'supergroup'){
     sendMessage($chatid, "The chat is: $title")
}

function sendMessage($chatid, $text){
    $url = "api.telegram.org/botBOT_TOKEN/sendMessage?chat_id=$chatid&parse_mode=HTML&text=" . urlencode($text);
    file_get_contents($url);
}
  • Check the content of `$update`, if that key is missing. We don't know how you populated it or what it's supposed to contain, so there's not much else we can say – ADyson Mar 04 '22 at 18:10
  • I edited the question, is it more clear now? –  Mar 04 '22 at 18:18
  • There is no `chat` key in the results you show. Maybe modify your question and show more of the result. Those `...` are what, your way of saying there's more? – GetSet Mar 04 '22 at 18:19
  • But in the [Telegram APIs](https://core.telegram.org/bots/api#chat) `title` is under `chat` –  Mar 04 '22 at 18:23
  • chat `title` you can only get if your bot was launched in supergroups, channels and group chats – emrdev Mar 04 '22 at 18:26
  • Yeah, I'm only executing in a supergroup, as shown in the results of /getChat –  Mar 04 '22 at 18:27
  • @Seba Oh, didn't notice. Then add your complete json to the question – emrdev Mar 04 '22 at 18:29
  • Show us a [mre] of the issue, please – ADyson Mar 04 '22 at 18:29
  • I just added a minimal reproducible example –  Mar 04 '22 at 18:36
  • `this is the result of /getChat?chat_id=telegram_id` ...it's not clear, how does that result relate to the PHP code you show afterwards. You're getting `$result` from `php://input` which is basically the incoming data of the request to PHP. What are you sending to your PHP? Do `var_dump($result);` and show us what it contains. – ADyson Mar 04 '22 at 20:15

0 Answers0