skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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.

    $path = 'gs://my_bucket/my_vars.json';
    
    // read the variables - you'll want to check for existence, etc.
    $myVarsJson = file_get_contents($path);
    $myVarsArr = json_decode($myVarsJson,true);
    
    // now you can access and modify the values of $myVarsArray
    
    // DO SOMETHING...
    
    // then save them
    $myVarsJson = json_encode($myVarsArr);
    file_put_contents($path);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search