Skip to content
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

Using ctx.reply in conversation.external terminates bot #68

Open
receter opened this issue Jan 2, 2023 · 4 comments
Open

Using ctx.reply in conversation.external terminates bot #68

receter opened this issue Jan 2, 2023 · 4 comments

Comments

@receter
Copy link

receter commented Jan 2, 2023

I am fetching some data in a conversation and would need to call ctx.reply inside of the conversation.external function. If doing so afterwards any new update will terminate the bot.

If doing it outside of conversation.external it does work, but in my use case this is not really an option as I want to update the message several times while the API is fetching.

I have created a minimal example project which can be found here: https://github.com/receter/grammy-conversations-reply-external

@KnorpelSenf
Copy link
Member

Is this a duplicate of #57? It seems to me like it is

@receter
Copy link
Author

receter commented Jan 3, 2023

Yes, the symptoms are not exactly the same, but it seems to be related. I have issue #57 as well with an api that I am manually binding to the context via middleware. It is available in ctx.myapi at first but as soon as the conversation continues ctx.myapi is undefined.

@receter
Copy link
Author

receter commented Jan 3, 2023

Here is something I observed fot the ctx issue: The first time the conversation is played, my api object in the context is:

MyAPI {
  basePath: '...',                
  ...
  axios: <ref *1> [Function: wrap] {    
    ...
  },
  configuration: Configuration {
    ...
  }
}

And in the next update, when I log it at the same line it looks like it got serialized:

{
  basePath: '...',
  axios: 'configuration',
  configuration: {
    ...
  }
}

@KnorpelSenf
Copy link
Member

I think this can be fixed by installing your API inside a conversation.run rather than in some middleware. See https://grammy.dev/plugins/conversations.html#working-with-plugins for this.

This means you should have something like

await conversation.run(async (ctx, next) => {
  ctx.myapi = ... // set your custom context property here
  await next()
})

in your conversation, rather than doing this in your middleware upfront.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: To do
Development

No branches or pull requests

2 participants