grammY icon indicating copy to clipboard operation
grammY copied to clipboard

feat: `ctx.send` and `SendData`

Open KnorpelSenf opened this issue 1 year ago • 2 comments

We want to introduce SendData: an object that specifies all necessary information in order to send a message. This can be a string, or { text: string } or { photo: photo }, etc. Optionally, more parameters can be contained.

This lets us add ctx.send. In grammY 2.0, you can then call ctx.send("message") and ctx.send({ photo }) instead of ctx.reply("message") and ctx.replyWithPhoto(photo).

Similar objects will be introduced for editing and deleting messages.

Similar objects will be introduced for calling arbitrary API methods.

This has many advantages:

  • ctx.reply has nothing to do with replies so the name is misleading
  • it is currently impossible to write a function that generates message data—if you want modularity, you must pass around the context object, which is bad
  • menus have no way to edit their own content when they are being navigated to, and send data will make this much better

Naming ideas:

  • send data
  • send template
  • send spec objects
  • send blueprints

KnorpelSenf avatar Dec 15 '24 21:12 KnorpelSenf

I created something similar before: https://github.com/rayz1065/grammy-edit-or-reply.

I'm not quite satisfies with the interface I came up with, it works but the types are a little to strict which gets in the way when it comes to modularity. Also, supporting custom keyboards was out of scope since it needed to work with editing as well. For the same reason, it works only with a limited amount of send methods that support editing (i.e. it has photo, animation, audio, document, video, and text messages but not for example contacts).

The main issue with an edit function is that it's currently not possible to edit a message that contains a media to a message that doesn't contain a media, which is a problem if we want to edit a media message to a very long text.

rayz1065 avatar Feb 13 '25 09:02 rayz1065

General send/edit methods is a great idea, agree. Found myself doing the similar thing for my projects: https://github.com/telegum/grammy-plugins/blob/main/packages/messages/src/plugin.ts

evermake avatar Mar 04 '25 16:03 evermake