Skip to content

Deferring interactions #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DTrombett opened this issue Dec 29, 2023 · 3 comments
Open

Deferring interactions #30

DTrombett opened this issue Dec 29, 2023 · 3 comments

Comments

@DTrombett
Copy link

First of all, thank you very much for sharing this example with Cloudflare Workers, it is a great start for building interaction-based apps.

Now, sometimes we may need to do additional operations in our command, which would exceed the limit of 3 seconds for an interaction response. To prevent the interaction from failing, we can defer it by sending a deferred channel message with source payload and then we can do our asynchronous magic under the hood and edit the response when we're ready.
Sadly, Cloudflare seems to stop any further asynchronous action after a response has been sent.

So, do you have any idea about how to defer an interaction or is it just not possible?
Thank you so much!

@DTrombett
Copy link
Author

For anyone wondering, I solved this problem by using context.waitUntil(). Would love to see an example of this pretty common use case in the repository!

@nicholasmireles
Copy link

Can you explain how you used the waitUntil? Looking to use it for my bot as well that has a slow backend API call.

@imgta
Copy link

imgta commented Sep 6, 2024

Can you explain how you used the waitUntil? Looking to use it for my bot as well that has a slow backend API call.

This is how I implemented it in response to a user interacting with a button:

router.post('/', async (request, env, ctx) => {
  if (type === InteractionType.MESSAGE_COMPONENT) {
      const response = new JsonResponse({
          type: InteractionResponseType.UPDATE_MESSAGE,
          data: { content, components: [] }, // set empty components array to remove buttons
      });

      ctx.waitUntil(cloudflareWorker(env, interaction, data));

      return response;
  }
}

I also made sure to edit the interaction message within my cloudflareWorker function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants