skip to Main Content

I wrote an bot on GAS to read from / write to a google sheet from telegram. I use the webhook method. I can address the bot with /read <args> and /write <args>. However, the less tech-savvy are not able to understand the principle of arguments…
So I want to make a conversational bot, like botfather. eg, if /read or /write are sent, then the bot ask in pm for the arguments, one by one.
The "reply in PM" part is easy, but I don’t see how I can catch replies for arguments?

As far as I understand, the function doPost is called each time a command is sent to the bot which poses issues of interactivity, and requiring a slash-something command to run the script instead of a plain text reply (and as the GAS data is volatile, the ugly work-around of storing a variable to know the bot should do the 2nd action is impossible too).

  • Is it feasible with webhook method?
  • Should I switch to polling / getUpdates method? I’m guessing it could work that way, with a time-out to exit the script in case of no reply, but I’m also guessing the PM chat should be set to ‘no privacy’ mode. or maybe it is intrinsic to the bot?

Any help appreciated!

2

Answers


  1. Chosen as BEST ANSWER

    So, GAS does offer a not-so volatile storage: CacheService.getScriptCache() (see also https://developers.google.com/apps-script/reference/cache/cache)

    The principle is simple: store the variable you want persistent in the cache (optionally, set a time out too - the default is 10min), and get the values when the script is called again. I found out that project that helped me understand how to use it: https://github.com/Milleus/tessara/blob/master/Code.gs

    In a few words: the syntax is cache.put(key, value) to store the value, and value=cache.get(key) to fetch it back.

    The beauty of it is that all required variables (if more than one) can be stored in an object, the key can be a unique identifier for the "user", and the value is the stringified object.

    In my case, I was able to build a fully conversational telegram bot (exactly like BotFather, but for my usage). Using the id from the person sending messages to the bot as the key, the script can run in "parallel" for multiple users at once.


  2. The doGet and doPost are the functions that get called when you follow a web app url. You can use them as an api if you use something like UrlFetchApp to access them or even an Http Request if you can handle the oauth.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search