0

I'm trying to create a script to operate my bot via Bot API, I'm using simple PHP file on my server, which has a set Webhook so that Telegram refers to this file each time when message is received. But the problem is that I'm unable to get a new message while script is already running, if I'm trying to get and assign an updated message to vary the workflow in the process I'm getting only the old message (which was present at the moment of starting script)

$update = file_get_contents('php://input');///get new data
$update = json_decode($update, TRUE);///decode data
$message = $update["message"]["text"];///assign message
switch($message) {////vary actions accordingly to first message
case "number1":
////send smth to user and wait for answer
$update = file_get_contents('php://input'); ////get new data with updated message
$update = json_decode($update, TRUE); ////decode
$message = $update["message"]["text"]; ////assign
switch($message) {////vary further actions accordingly to new message
    case "number2":
    ////further actions
    }
}
...
VItazzz
  • 11
  • 1

1 Answers1

1

You cannot get data { ('php://input') } multiple times from telegram. You may ask why?

Let me describe what happens: 0- Someone wants to sends a message to your bot VIA Telegram client

1- Telegram servers grab that message and runs your pre-defined script.(script that you give Telegram when setting webhook.

2- One time and only one time when you use ('php://input') whole data (include that message) would be given to your script and NOW Telegram has nothing more to give you.

3- Your script runs and process that message in some miliseconds or less(in this extra little time no one can type and enter something more at client side)

4- Your script finishes it JOB.

5- Next time when user enters something ( 1 second later or 1 year later!), Telegram will call your scripts again with new message.

As you see only the first call of ('php://input') has useful data (because telegram has data for you and call your bot script. Other call of ('php://input') give nothing to you.

AS AN EXPERIENCE: Only get data for one time at top of your code(before processing the message) and then process it. If you need former messages you can save them in database as you recive.

Tibebes. M
  • 6,940
  • 5
  • 15
  • 36
Seyfi
  • 1,832
  • 1
  • 20
  • 35