Skip to content

Polling and Long Polling

Abdi Urgessa edited this page Feb 19, 2024 · 2 revisions

Polling

Polling is a method used to fetch updates from the Telegram server by making repeated requests. However, in its simplest form, polling requests updates just once. This means your function is executed a single time, processes any available updates, and then stops.

Usage Example:

To initiate a single polling request, you can use the following code snippet. The parameters limit and timeout are optional and can be adjusted based on your needs.

const telesun = new Telesun.connectBot("YOUR_BOT_TOKEN").polling({limit: 100, timeout: 60});

In this context, limit refers to the maximum number of updates to be fetched in one request, and timeout specifies the maximum time (in seconds) the server will wait for new updates before responding.

Long Polling

Unlike simple polling, long polling is designed to keep the execution running for an extended period. In the context of Google Apps Script, this means that your script will continue to request updates from the Telegram server for up to 6 minutes, which is the maximum execution time allowed for scripts running on customer accounts. This is beneficial as it ensures your bot remains responsive to new updates without needing to restart the execution manually.

Usage Example:

To set up long polling, you can use the following approach. Just like with simple polling, the parameters timeout and sleep are optional.

const telesun = new Telesun.connectBot("YOUR_BOT_TOKEN").longPolling({timeout: 60, sleep: 1000});

Here, timeout specifies how long (in seconds) the server will wait for new updates before sending a response, and sleep determines the delay (in milliseconds) between each polling request. This setup helps manage the workload on your server and the Telegram API, ensuring efficient and continuous updates handling.

Note: It's crucial to remember that both polling and long polling methods are subject to the execution time limits imposed by Google Apps Script. This means careful consideration should be given to error handling and script efficiency to maintain a responsive and reliable bot experience.

Clone this wiki locally