PaulSonOfLars / gotgbot

Autogenerated Go wrapper for the telegram API. Inspired by the python-telegram-bot library.
MIT License
509 stars 114 forks source link

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

Closed liuerfire closed 2 months ago

liuerfire commented 2 months ago

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 commented 2 months ago

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 commented 2 months ago

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!)