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

Add context.Context to the func signatures in gen_*.go #190

Closed
liuerfire opened this issue Aug 21, 2024 · 2 comments
Closed

Add context.Context to the func signatures in gen_*.go #190

liuerfire opened this issue Aug 21, 2024 · 2 comments

Comments

@liuerfire
Copy link
Contributor

For backwards compatibility, maybe add extra methods like

func (bot *Bot) SendMessageWithContext(ctx context.Context, chatId int64, text string, opts *SendMessageOpts) (*Message, error)

Why need this

For example, we have a web server, then need to send messages to users if they make some actions on our website. A typical code maybe like this:

func (api *API) handleAction(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    // do something with the request's context, like querying the db
    db.getUser(ctx, userid)
   
    // Everything is done and need to notify the user. 
    // However we lost the context for something like tracing, logging or timeout control 
    // since SendMessage doesn't accept `Context`
    bot.SendMessage(chatId int64, text string, opts *SendMessageOpts)
}

Hope I made my point clear 😄

@PaulSonOfLars
Copy link
Owner

Hey hey! thanks for opening.
Yes, of course - that makes complete sense. Was wondering when someone would ask for this to be honest 😅

I'd be happy to to add WithContext (or even WithCtx if thats preferred? not sure what the go ecosystem trends to).
Just need to come up with a clean(ish) way of representing that without blindly duplicating the code; shouldn't be too difficult!

@PaulSonOfLars
Copy link
Owner

Let me know how #192 looks to you!

I'd really appreciate it if you could try out the branch on your side, and run any tests you might need - this would be a really helpful datapoint so I can make sure everything is working as expected (and is as intuitive as possible!)

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

2 participants