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:
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