0

My question is a little complicated so I would rather to go for some explanations before asking the main questions.

I'm making a Content Management System for managing my Telegram Bot and get the latest updates by that.

Basically what I have done till now, is getting the updates with Telegram Bot API and show them in a little Chat Box like this:

print screen

For example as you can see in this pic, a user called Pouya has sent a message to the Bot that I'm working with.

Now in order to reply and send message to this, I coded this:

if (session_status() == PHP_SESSION_NONE) {
    session_start();
    $_SESSION['messages'] = array();  
}
$request_params = array();
if (isset($_POST['send'])){
        $pm = $_POST['message'];
        array_push($_SESSION['messages'], $pm); 
        $request_params = [
                'chat_id' => $id,
                'text' => $_SESSION['messages']
        ];
        $request_url = 'https://api.telegram.org/bot' . $botToken . '/sendMessage?' . http_build_query($request_params);
        file_get_contents($request_url);
        print_r($request_params);
}

<div class="box-footer">
    <form action="" method="post">
        <div class="input-group">
        <input type="text" name="message" placeholder="Write your direct message" class="form-control">
        <span class="input-group-btn">
            <input name="send" type="submit" class="btn btn-danger btn-flat"/>
        </span>
        </div>
    </form>
</div>

As you can see I have saved the $pm into a session called $_SESSION['messages']. So by this way I can call it again in the Chat Box and there would be no need of using Database & etc:

$num3 = count($request_params["text"]);
for($z=0;$z<$num3;$z++){
    echo '<div class="direct-chat-text">';
    echo $request_params["text"][$z];   
    echo '</div>';
}

So everything looks nice and clean but there are two problems with this code:

1- The reply message does not sent into Telegram account

(However if I change this:

$request_params = [ 'chat_id' => $id, 'text' => $_SESSION['messages'] ];

to this:

$request_params = [ 'chat_id' => $id, 'text' => $pm ];

it will send message correctly).

2- And second problem is that the message does not even shown in the Chat Box after submitting the form.

I guess the main thing that cause these problems is that the $_SESSION['messages'] does not contain any value at all and its empty!

So what is your idea about this... Please share your suggestions about this. Thanks :)

UPDATE 1:

file_get_contents($request_url); contains this as example:

https://api.telegram.org/bot423495534:asdsadsadsad/sendmessage?chat_id=108132368&text=hi
  • What’s in `file_get_contents($request_url)`? Maybe try taking a look in that? – Zoe Edwards Dec 21 '17 at 14:36
  • @ThomasEdwards Please see **UPDATE 1** at the end of question. thx –  Dec 21 '17 at 14:42
  • Okay, so what is the contents of that URL? i.e. `$response = file_get_contents($request_url)` – print out `$response`. – Zoe Edwards Dec 21 '17 at 14:45
  • @ThomasEdwards Nothing.. here is the print_r($request_params): `Array ( [chat_id] => 108132368 [text] => )` –  Dec 21 '17 at 14:47
  • In fact the problem is `$_SESSION['messages']` does not send any data. –  Dec 21 '17 at 14:48
  • That’s the contents of `$request_params` – read carefully. `$response = file_get_contents($request_url);` – then print out `$response`. It’ll likely be a JSON string. – Zoe Edwards Dec 21 '17 at 14:48
  • @ThomasEdwards Well I just tried to print_r($response); but I get no result (I mean blank screen) –  Dec 21 '17 at 14:55
  • If I visit the [link you provided](https://api.telegram.org/bot423495534:asdsadsadsad/sendmessage?chat_id=108132368&text=hi), I get an unauthorised message, presumedly because you’ve changed the token, which is fine. If you do this with a real token, you’ll get a response. That response should then show up as the return value of `file_get_contents`. If that isn’t happening, something else is going on with your code that I can’t see from here. – Zoe Edwards Dec 21 '17 at 15:03
  • It's obvious that when `$request_params` is not complete yet, the `$reponse` would be empty as well. Because it gets no contents from the `$requested_url` –  Dec 21 '17 at 15:03
  • `$request_params` is your code, not Telegram’s. Their API can’t magically inject anything into it, they don’t know or care it exists. All they can do is return a body in the response, which you then have to read, and then what you do with that is up to you. If you need the message to be in the session, then you have to do that yourself, it doesn’t do it for you. The contents of the response will need to be decoded `$data = json_decode($response);`, then you can use `$data['message']`. – Zoe Edwards Dec 21 '17 at 15:06
  • @TomasEdwards You're talking about sending messages directy using Telegram API. And not trying to send message within a chat box which is a completely different area. –  Dec 21 '17 at 15:06
  • All the problems come to the `$_SESSION['messages']` that does not return value even after `array_push($_SESSION['messages'], $pm); `. When I changed `$request_params` values to `['chat_id' => $id,'text' =>$pm];` it works fine! –  Dec 21 '17 at 15:07
  • Ah, got it – sorry real struggle to work out what you’re asking. I’ll reply now. – Zoe Edwards Dec 21 '17 at 15:11

1 Answers1

0

$_SESSION['messages'] is an array and won’t convert to a string, likely erroring.

Try using implode implode(" ", $_SESSION['messages']) to add a space between each message, or anything you’d prefer.

$request_params = [
    'chat_id' => $id,
    'text' => implode(" ", $_SESSION['messages'])
];
Zoe Edwards
  • 12,999
  • 3
  • 24
  • 43