I’m building a bot on the Telegram Bot API. For a specific function, my bot should remember the variables when re-sending the data with the webhook.
To make it short; every time I send a message, the PHP-file is loaded (running on a Google App Engine), which is evoked by the Telegram Bot API.
The bot “reloads” every time the API sends data to the webhook, all variables are reset of course. I was thinking of using a $_SESSION
-Variable, but that also doesn’t work.
Except for saving the data in a MySQL-database or writing it into a file, are there any other possibilities to temporarily store data somewhere?
Thank you!
2
Answers
You could use
file_get_contents
and file_put_contents
to save your State into a File and load it whenever you need it again.
Unless the Telegram API webhooks respects cookies, you will need to persist your state somewhere. It seems in this case, saving to a file is the best bet.
Google App Engine provides a way to use PHP streams to make this easy to read and write to Google Cloud Storage. https://cloud.google.com/appengine/docs/php/googlestorage/
One way to do this is to put any variables you need to persist in an array, then encode as JSON before saving. Then when you read the file, just decode the JSON as an associative array and go on your way.